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:

Bash

You should see something like:

text

Now you are inside Node.js — you can write JavaScript directly.

Quick examples to play with:

JavaScript

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:

JavaScript

Run it:

Bash

Output:

text

3. Passing arguments to your script (very important!)

process.argv contains all command-line arguments.

Modify hello.js:

JavaScript

Run different ways:

Bash

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:

Bash

5. Running modern ESM files (2025–2026 style)

Two common ways:

Way 1: Use .mjs extension

Create greet.mjs

JavaScript

Run:

Bash

Way 2: Add “type”: “module” in package.json (recommended)

JSON

Now you can use .js files with import:

JavaScript
Bash

6. Real-world example: Mini CLI tool

Let’s build a tiny useful command-line tool.

Create color.js

JavaScript

Install chalk:

Bash

Run:

Bash

Now make it executable (Linux/macOS):

Bash

Then you can run it like this:

Bash

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

Bash

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! 😄

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *