Node.js CLI Recipes

Node.js CLI recipes for watch mode, built-in tests, REPL usage, shebang scripts, and practical automation patterns.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Watch Mode, Test Runner, and REPL

Use watch mode, built-in tests, the REPL, and script recipes.

Run in watch mode

Restart the process when files change.

bashANYnodewatchdev
bash
node --watch app.js
Notes

Built-in watch mode is stable in current Node releases and useful for local development.

Watch specific paths

Only restart when selected paths change.

bashANYnodewatchpath
bash
node --watch-path=src --watch-path=config app.js
Notes

Good for reducing restart noise in larger projects.

Run built-in test runner

Execute tests using Node's built-in runner.

bashANYnodetest
bash
node --test
Notes

Useful for projects using the native node:test module.

Watch tests

Re-run tests when files change.

bashANYnodetestwatch
bash
node --test --watch
Notes

Convenient during development of native test suites.

Start the REPL

Open the interactive Node shell.

bashANYnoderepl
bash
node
Notes

Run Node without arguments to start the REPL.

Persist REPL history

Use a custom history file for REPL commands.

bashANYnodereplhistory
bash
NODE_REPL_HISTORY=$HOME/.node_repl_history node
Notes

Helpful when you use the REPL heavily across sessions.

Use Node in a shebang script

Create a portable Node-based CLI script.

bashANYnodeclishebang
bash
#!/usr/bin/env node
console.log('hello from a Node CLI')
Notes

Place this at the top of executable JS files to create custom CLIs.

Pass script args after double dash

Separate Node flags from script arguments.

bashANYnodeargumentscli
bash
node --trace-warnings app.js -- --port=3000 --verbose
Notes

`--` tells Node to stop parsing its own flags and pass the rest to your script.

Recommended next

No recommendations yet.