Chapter 4: Node.js Command Line
Node.js Command Line Usage — written as if I’m sitting next to you, showing you the terminal, explaining every command, why it exists, what people actually use in real life, and giving practical examples you can copy-paste right now.
Let’s go step by step.
1. Most important thing first: Node.js is a command-line program
When you type node in the terminal and press enter, you get an interactive REPL (Read-Eval-Print Loop).
Try it:
|
0 1 2 3 4 5 6 |
node |
You should see something like:
|
0 1 2 3 4 5 6 7 8 |
Welcome to Node.js v20.17.0. Type ".help" for more information. > |
Now you are inside Node.js — you can write JavaScript directly.
Quick examples to play with:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
> 2 + 2 4 > const name = "Aman" undefined > name 'Aman' > console.log(`Hello ${name}`) Hello Aman undefined > process.version 'v20.17.0' > .exit ← to quit |
This REPL is great for:
- Quick calculations
- Testing small pieces of code
- Exploring Node.js built-in objects (process, fs, path, os…)
But most real work happens by running files.
2. The most common command: node filename.js
Create a file called hello.js:
|
0 1 2 3 4 5 6 7 8 |
console.log("Hello from command line!"); console.log("Current directory:", process.cwd()); console.log("Arguments:", process.argv); |
Run it:
|
0 1 2 3 4 5 6 |
node hello.js |
Output:
|
0 1 2 3 4 5 6 7 8 9 10 11 |
Hello from command line! Current directory: /home/aman/projects/my-node-stuff Arguments: [ '/usr/local/bin/node', '/home/aman/projects/my-node-stuff/hello.js' ] |
3. Passing arguments to your script (very important!)
process.argv contains all command-line arguments.
Modify hello.js:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// hello.js const args = process.argv.slice(2); // remove node + filename console.log("Arguments received:", args); if (args.length === 0) { console.log("No name given. Example: node hello.js Aman"); } else { console.log(`Hello ${args[0]}! Welcome to Node.js CLI`); } |
Run different ways:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
node hello.js # → No name given... node hello.js Aman # → Hello Aman! Welcome to Node.js CLI node hello.js Aman 25 Hyderabad --dark-mode # → Arguments received: [ 'Aman', '25', 'Hyderabad', '--dark-mode' ] # → Hello Aman! Welcome to Node.js CLI |
Real-world usage: many CLI tools accept arguments this way (version, –help, –port, file paths…).
4. Useful flags you should know
| Command | What it does | When to use it |
|---|---|---|
| node –version | Shows Node.js version | Check which version is active |
| node -v | Same as above (short version) | Quick check |
| node –help | Shows all available flags | Discover more options |
| node -e “console.log(2+2)” | Execute JavaScript string directly | Quick one-liners |
| node –inspect | Start debugging (Chrome DevTools) | Debug hard problems |
| node –inspect-brk | Start debugging and pause on first line | Step through startup code |
| node –trace-warnings | Show detailed warnings | Find hidden problems |
| node –no-warnings | Hide all warnings | Clean output in scripts |
| node –env-file=.env | Load .env file automatically (Node 20.6+ / 21+) | Modern way instead of dotenv package |
Example one-liner:
|
0 1 2 3 4 5 6 7 |
node -e "console.log(process.platform, process.arch)" # → linux x64 (or win32 x64, darwin arm64...) |
5. Running modern ESM files (2025–2026 style)
Two common ways:
Way 1: Use .mjs extension
Create greet.mjs
|
0 1 2 3 4 5 6 7 8 9 10 |
import { argv } from 'node:process'; const name = argv[2] ?? "friend"; console.log(`Hi ${name}, welcome!`); |
Run:
|
0 1 2 3 4 5 6 |
node greet.mjs Aman |
Way 2: Add “type”: “module” in package.json (recommended)
|
0 1 2 3 4 5 6 7 8 9 |
{ "name": "my-cli", "type": "module" } |
Now you can use .js files with import:
|
0 1 2 3 4 5 6 7 8 9 |
// greet.js import { argv } from 'node:process'; console.log(`Hello ${argv[2] ?? "stranger"}!`); |
|
0 1 2 3 4 5 6 |
node greet.js Webliance |
6. Real-world example: Mini CLI tool
Let’s build a tiny useful command-line tool.
Create color.js
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
#!/usr/bin/env node // ↑ very important shebang line for Unix-like systems import { argv } from 'node:process'; import chalk from 'chalk'; // we'll install it const command = argv[2]; if (!command || command === '--help') { console.log(` Usage: node color.js <command> Commands: red → show red message green → show green message rainbow → fun effect `); process.exit(0); } if (command === 'red') { console.log(chalk.bgRed.white(" This is a RED message ")); } else if (command === 'green') { console.log(chalk.bgGreen.black(" This is a GREEN message ")); } else if (command === 'rainbow') { console.log(chalk.rgb(255,100,100)('R') + chalk.rgb(255,165,0)('A') + chalk.rgb(255,255,0)('I') + chalk.rgb(0,255,0)('N') + chalk.rgb(0,0,255)('B') + chalk.rgb(75,0,130)('O') + chalk.rgb(148,0,211)('W')); } else { console.log(chalk.yellow(`Unknown command: {command}`)); } |
Install chalk:
|
0 1 2 3 4 5 6 |
npm install chalk |
Run:
|
0 1 2 3 4 5 6 7 8 |
node color.js red node color.js rainbow node color.js --help |
Now make it executable (Linux/macOS):
|
0 1 2 3 4 5 6 |
chmod +x color.js |
Then you can run it like this:
|
0 1 2 3 4 5 6 |
./color.js green |
7. Quick reference – most useful CLI patterns
| Pattern | Example command | Typical use case |
|---|---|---|
| Simple script | node script.js | Automation, one-time tasks |
| With arguments | node script.js input.txt output.txt | File processing |
| With flags | node server.js –port 8080 –dev | Configuration |
| Shebang + chmod | ./cli-tool.js –help | Real CLI tools |
| npx for one-time run | npx cowsay Hello | Try packages without installing |
| Global install | npm install -g create-vite | Tools you use everywhere |
Summary – Command line cheat sheet
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# Quick checks node -v node --version node --help # Run file node index.js node --env-file=.env index.js # Run string node -e "console.log(new Date().toISOString())" # Debug node --inspect server.js # Modern ESM # (use "type": "module" in package.json or .mjs extension) node api.mjs # Make executable (Unix) chmod +x myscript.js |
Want to go deeper into any of these?
- Building a real CLI tool with commander.js or yargs
- How to publish your own CLI to npm
- Using process.exit(), process.stdin, process.stdout
- Creating interactive prompts in terminal
- Handling large input from pipes (cat file.txt | node script.js)
- Debugging CLI scripts with –inspect
Just tell me what you want to explore next — I’ll give you full examples and step-by-step guidance! 😄
