Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท CLI Flags

Running Node.js files: node file

Reference for the node command: run a file, a package or standard input, how arguments and exit codes work, and how Node decides between ES modules and CommonJS.

The node command starts the runtime. Given a file name it loads that file as the program's entry point, runs it, and then keeps going as long as there is work left (open servers, pending timers). When nothing is left, the process exits with code 0. If you give it no file and stdin is a terminal, you get the interactive REPL.

The argument after the flags is the script; anything after the script goes to your program in process.argv. Flags placed before the script belong to Node itself.

Terminal
node app.js
node --watch app.js first second

Try it

The live examples start a child Node process with the same executable, so they show what the command does.

Node.js Arguments, exit codes and module type

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Node.js Reading the program from standard input

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Syntax

Terminal
node [options] [V8 options] [script.js | -e "script" | -] [arguments]
node inspect [script.js | host:port] [arguments]
Part Description
options Node flags such as --watch or --env-file=.env. They must come before the script name.
script Path to the entry file. The extension may be left off for CommonJS (node app), which resolves app.js, app.json, app/index.js, and so on.
- Read the program from standard input.
arguments Passed through to the program. process.argv[0] is the node executable, [1] the script path, the rest your arguments.
-- Ends Node's option parsing; useful when a script name starts with a dash.

Exit codes: 0 for success, 1 for an uncaught exception or other fatal error, 9 for an invalid option, and any value you set through process.exitCode or process.exit(). Signals show up as 128 + signal number in a shell.

Usage notes

  • CommonJS or ES module? .mjs is always an ES module, .cjs always CommonJS. For .js, the nearest package.json decides: "type": "module" or "commonjs". If there is no type, Node 22 and later inspect the file and run it as ESM when it contains import/export syntax; to be sure, set type explicitly.
  • Run a package directory. node . or node folder loads the folder's package.json main field (or index.js).
  • Arguments belong to the script. node app.js --watch passes --watch to your program; node --watch app.js gives the flag to Node. Parse program arguments with util.parseArgs().
  • Process lifetime. Node exits when the event loop is empty. A listening server or an active timer keeps it alive; call process.exit() or close handles to stop sooner, and prefer process.exitCode so pending output can flush.
  • Shebang lines (#!/usr/bin/env node) are ignored by the parser, so the same file works as an executable and under node file.
  • Environment. NODE_OPTIONS can add flags for every run; see NODE_OPTIONS.
  • Piping. echo 'console.log(1)' | node also works when stdin is not a terminal.