# scanf

> C like scanf/sscanf module for node.js.

Latest version **1.2.1** (published 2026-05-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install scanf
pnpm add scanf
yarn add scanf
bun add scanf
```

## Health

**Score 60/100 (C)** — status: active.

Positive: has types; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 1.2.1 |
| Published | 2026-05-14 |
| First published | 2015-04-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >= 4 |
| Dependencies | 0 |
| Unpacked size | 48 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 55 |
| Author | Lellansin |
| Maintainers | lellansin |
| Keywords | scanf, sscanf, readSync from stdin, shell input |

## Links

- npm: https://www.npmjs.com/package/scanf
- Repository: https://github.com/Lellansin/node-scanf
- Issues: https://github.com/Lellansin/node-scanf/issues
- npm.io page: https://npm.io/package/scanf

## Recent versions

- 1.2.1 (latest) — 2026-05-14
- 1.2.0 — 2023-09-08
- 1.1.2 — 2020-08-27
- 1.1.1 — 2019-12-20
- 1.1.0 — 2019-12-20
- 1.0.2 — 2018-06-19
- 1.0.1 — 2018-06-19
- 1.0.0 — 2017-10-19
- 0.7.3 — 2016-09-06
- 0.7.2 — 2016-05-04
- 0.7.0 — 2016-02-06
- 0.6.4 — 2015-09-18
- 0.6.3 — 2015-09-01
- 0.6.2 — 2015-08-17
- 0.6.1 — 2015-06-18
- … 11 more at https://npm.io/package/scanf/versions

## README

# node-scanf [![NPM Version](https://badge.fury.io/js/scanf.svg)](http://badge.fury.io/js/scanf) [![CI](https://github.com/Lellansin/node-scanf/actions/workflows/ci.yml/badge.svg)](https://github.com/Lellansin/node-scanf/actions/workflows/ci.yml)

Do you want simplely shell script input which have formats and sync return?

Do you want a `sscanf` like function to parse format strings?

`scanf` is a C like scanf/sscanf module for node.js which can help you with that.

## Documentation

*  [`Example`](#Example)
  *  [`Quickly start`](#quickly-start)
  *  [`C like format`](#c-like-format)
*  [`Return`](#return)
  *  [`Directly return`](#directly-return)
  *  [`Array return`](#array-return)
  *  [`Json return`](#json-return)
*  [`sscanf`](#sscanf)

## Installation
```
npm install scanf
```

## CI

The project is tested on GitHub Actions across the following platforms:

| OS       | Node.js versions              |
|----------|-------------------------------|
| Ubuntu   | 4.x ~ 22.x                    |
| Windows  | 4.x ~ 22.x                    |
| macOS    | 16.x ~ 22.x (arm64)           |

> macOS CI only covers Node.js 16+ because the latest macOS runner (`macos-latest`) runs on Apple Silicon (arm64), which does not provide prebuilt binaries for older Node.js versions. The Intel-based `macos-13` runner is no longer available.

## Format support now

* `%d` - integer
* `%f` - float
* `%s` - string
* `%S` - string of line
* `%x` - hex
* `%a` - [hex float](https://github.com/Lellansin/node-scanf/issues/24)
* `%o` - octal

## Example

### Quickly start
```javascript
var scanf = require('scanf');

console.log('Pleas input your name');
var name = scanf('%s');

console.log('Pleas input your age');
var age = scanf('%d');

console.log('your name [%s] type: [%s]', name, typeof name);
console.log('your age [%s] type: [%s]', age, typeof age);
```

REPL

```
Pleas input your name
> Barack\ Obama
Pleas input your age
> 24
your name [Barack Obama] type: [string]
your age [24] type: [number]
```


### C like format
```javascript
var scanf = require('scanf');

console.log('when are you born? \(Year-month-day\)');
var date = scanf('%d-%d-%d');

console.log('your birthday [%s]', date);
```

REPL

```
when are you born? (Year-month-day)
> 1990-01-01
your birthday [1990,1,1]
```

## Return

### Directly return

```javascript
var scanf = require('scanf');

var number = scanf('%d');

console.log('number', number);
```

REPL

```
>> 2015    
number 2015
```

### Array return

```javascript
var scanf = require('scanf');

var result = scanf('%s%d%d');

console.log('result', result);
```

REPL

```
>> Alan 24 180        
result [ 'Alan', 24, 180 ]
```

### Json return

```javascript
var scanf = require('scanf');

var result = scanf('%d %f %s %x %o', 'integer', 'float', 'string', 'hex', 'octal');

console.log('result', result);
```

REPL

```
>> 12 3.1415926 hello 1F 10    
result { 
  integer: 12,
  float: 3.1415926,
  string: 'hello',
  hex: 31,
  octal: 8 
}
```

## sscanf

REPL

```javascript
>> var sscanf = require('scanf').sscanf;
undefined
>> sscanf('12 34', '%d');
12
>> sscanf('Alan 20 180', '%s%d%d')
[ 'Alan', 20, 180 ]
>> sscanf('12 3.1415926 hello', '%d %f %s', 'month', 'pi', 'world');
{ month: 12, pi: 3.1415926, world: 'hello' }
>> sscanf('   14   ??  Ss     0:07.59 /usr/sbin/securityd -i', '%d %s %s %s %s %s', 'pid', 'tty', 'stat', 'time', 'exec', 'param');
{ pid: 14,
  tty: '??',
  stat: 'Ss',
  time: '0:07.59',
  exec: '/usr/sbin/securityd',
  param: '-i' }
```

you can see the ./tests files for more detail.

If there are some formats not support or go broken, you can contact author with email `lellansin@gmail.com`.

# Contributors

  - [lellansin](https://github.com/Lellansin)
  - [piranna](https://github.com/piranna)
  - [samchon](https://github.com/samchon)

# License

  MIT

---
_Source: https://npm.io/package/scanf · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
