---
title: "How do you read command-line arguments in Node.js?"
description: "Read process.argv for raw arguments, or use util.parseArgs, built into Node.js, to parse flags and values without a dependency."
url: "https://bugsee.com/answers/nodejs/read-command-line-arguments/"
---

# How do you read command-line arguments in Node.js?

`process.argv` holds the raw arguments (the first two entries are `node` and the script path). For flags, use the built-in `util.parseArgs`.

```js
// node cli.js --port 8080 --verbose input.txt
import { parseArgs } from "node:util";

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

console.log(values.port, values.verbose, positionals);
// "8080" true [ "input.txt" ]
```

Values come back as strings, so convert numbers yourself. For a larger tool with subcommands and generated help text, a library such as commander is worth the dependency.

## Related questions

- [How do you check if a file exists in Node.js?](https://bugsee.com/answers/nodejs/check-if-a-file-exists/)
