---
title: "How do I get command-line arguments in Node.js?"
description: "Node.js puts command-line arguments in process.argv. Learn how to read them, and how to parse flags and options properly with the built-in util.parseArgs."
url: https://www.danny.engineering/answer/nodejs/how-do-i-get-command-line-arguments-in-nodejs
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T18:57:00.000Z
tags: ["Node.js"]
---

# How do I get command-line arguments in Node.js?

## The short answer


Arguments are in `process.argv`, an array of strings. The first two entries are the path to the Node.js binary and the path to your script, so the arguments you typed start at index 2. For anything beyond one or two positional values, use the built-in `parseArgs` from `node:util` instead of parsing by hand.


```javascript
// node greet.js Ada --shout
const args = process.argv.slice(2);
console.log(args); // [ 'Ada', '--shout' ]
```


## Why it happens


The operating system passes a program its command line as a list of strings. Node.js exposes that list unchanged as `process.argv`, including the executable and script path, which is why the slice is needed. Everything is a string: `--port 3000` gives you `'3000'`, not a number, and Node.js does not know which values are flags and which are positional.


That is fine for a quick script, but hand-written parsing tends to break on `--port=3000`, short flags like `-p`, or a flag that appears twice. `util.parseArgs` handles those cases and is stable in every supported Node.js release, so you no longer need a package for simple tools.


## How to fix it


### Use util.parseArgs for flags and options


Describe the options you accept, and `parseArgs` returns `values` for options and `positionals` for everything else.


```javascript
import { parseArgs } from 'node:util';

const { values, positionals } = parseArgs({
  options: {
    port: { type: 'string', short: 'p', default: '3000' },
    verbose: { type: 'boolean', short: 'v', default: false },
    tag: { type: 'string', multiple: true },
  },
  allowPositionals: true,
});

const port = Number(values.port);
console.log({ port, verbose: values.verbose, tags: values.tag, files: positionals });
```


```bash
node server.js -p 8080 --verbose --tag a --tag b input.txt
```


By default `parseArgs` runs in strict mode, so an unknown option or a missing value throws an error. Catch it and print a usage message rather than a stack trace.


```javascript
try {
  const { values } = parseArgs({ options: { port: { type: 'string' } } });
} catch (error) {
  console.error(error.message);
  console.error('Usage: node server.js --port <number>');
  process.exit(1);
}
```


### Read process.argv directly for one or two values


For a script that takes a single file name, destructuring is clear enough.


```javascript
const [inputPath] = process.argv.slice(2);

if (!inputPath) {
  console.error('Usage: node count-lines.js <file>');
  process.exit(1);
}
```


### Pass arguments through npm scripts


When you run a script with `npm run`, put `--` before your arguments. Without it, npm treats flags such as `--port` as options for npm itself.


```bash
npm run start -- --port 8080
```


### Reach for a library for large CLIs


If you need subcommands, generated help text or shell completion, a library such as Commander or yargs is still worth it. For a handful of flags, `parseArgs` keeps your tool dependency-free.


## Common pitfalls

- Forgetting the `slice(2)` and treating the script path as the first argument.
- Comparing a value to a number. Arguments are always strings, so convert with `Number()` and check the result with `Number.isNaN()`.
- Expecting `parseArgs` to convert types. `type` is only `'string'` or `'boolean'`; anything else is up to you.
- Missing the `--` in `npm run`, so your flags silently vanish.
- Putting Node.js options after the script name. `node app.js --inspect` passes `--inspect` to your script; use `node --inspect app.js` instead.

## Related reading

- [Node.js: util.parseArgs](https://nodejs.org/api/util.html#utilparseargsconfig)
- [Node.js: process.argv](https://nodejs.org/api/process.html#processargv)
