Node.js Command Line Usage

Node.js Tutorial

Node.js Command Line Usage

Node.js Command Line Usage 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:


Run using:

npm run start

or:

npm run dev

 Summary: Important Node CLI Commands

TaskCommand
Run JS filenode file.js
Interactive REPLnode
Evaluate codenode -e "code"
Run with watch modenode --watch file.js
Check versionnode -v
Show helpnode --help
Pass argumentsnode file.js arg1 arg2

You may also like...