Node.js Command Line Usage

Node.js Command Line Usage

Node.js comes with a built-in CLI (Command Line Interface) that allows you to run JavaScript files, execute commands, manage packages, and interact with the Node.js REPL.

Below are all the important command-line usages.


🟢 1. Run a JavaScript File

Example file: app.js

node app.js

Or with a path:

node folder/app.js

🟢 2. Check Node.js Version

node -v

or

node --version

🟢 3. Use Node.js REPL (Interactive Mode)

Start REPL:

node

Run JavaScript directly:

> 10 + 20
30
> const name = "Vipul";
> name
"Vipul"

Exit REPL:

.exit

or:

Ctrl + C (twice)

🟢 4. Run Node.js Script with Arguments

Use this:

node app.js arg1 arg2

Example:

node app.js hello world

Inside app.js:

console.log(process.argv);

Output:

['node', 'app.js', 'hello', 'world']

🟢 5. Run Node.js with Flags

✔ Enable ES Modules

node --experimental-modules app.js

✔ Increase memory limit

node --max-old-space-size=4096 app.js

✔ Run with tracing

node --trace-warnings app.js

🟢 6. Run Node.js Code Without File

node -e "console.log('Hello Node.js')"

-e means evaluate inline code.


🟢 7. Watch Mode (Auto Reload) — Node 18+

Automatically reload when file changes:

node --watch app.js

🟢 8. Execute JSON or WASM (Special Cases)

node --experimental-json-modules app.js

🟢 9. Start a Server Directly

node server.js

🟢 10. Node.js Help Command

node --help

Shows all available CLI options.


🟢 11. Run Node.js Package Scripts (via NPM)

From package.json:

"scripts": {
"start": "node server.js",
"dev": "node --watch server.js"
}

Run using:

npm run start

or:

npm run dev

⭐ Summary: Important Node CLI Commands

Task Command
Run JS file node file.js
Interactive REPL node
Evaluate code node -e "code"
Run with watch mode node --watch file.js
Check version node -v
Show help node --help
Pass arguments node file.js arg1 arg2

You may also like...