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
Run in watch mode
node --watch app.js

# Restart the process when files change.

Watch specific paths
node --watch-path=src --watch-path=config app.js

# Only restart when selected paths change.

Run built-in test runner
node --test

# Execute tests using Node's built-in runner.

Watch tests
node --test --watch

# Re-run tests when files change.

Start the REPL
node

# Open the interactive Node shell.

Persist REPL history
NODE_REPL_HISTORY=$HOME/.node_repl_history node

# Use a custom history file for REPL commands.

Use Node in a shebang script
#!/usr/bin/env node
console.log('hello from a Node CLI')

# Create a portable Node-based CLI script.

Pass script args after double dash
node --trace-warnings app.js -- --port=3000 --verbose

# Separate Node flags from script arguments.

Recommended next

No recommendations yet.