Node CMD Line
Node.js Command Line Usage
Node.js provides a powerful command line interface (CLI) that allows you to run JavaScript files, manage packages, debug applications, and more.
All commands should be run in a terminal or command prompt.
- Windows: Command Prompt, PowerShell, or Windows Terminal
- macOS/Linux: Terminal
Basic Node.js Commands
Run JavaScript files with Node.js:
# Run a JavaScript file
node app.js
# Run with additional arguments
node app.js arg1 arg2
# Run in watch mode (restarts on file changes)
node --watch app.js
Using the REPL
The Node.js REPL (Read-Eval-Print Loop) is an interactive shell for executing JavaScript:
> const name = 'Node.js';
> console.log(`Hello, ${name}!`);
> .help // Show available commands
> .exit // Exit REPL
Command Line Arguments
Access command line arguments using process.argv:
// args.js
console.log('All arguments:', process.argv);
console.log('First argument:', process.argv[2]);
console.log('Second argument:', process.argv[3]);
// Example usage:
// node args.js hello world
// Output:
// All arguments: ['/path/to/node', '/path/to/args.js', 'hello', 'world']
// First argument: hello
// Second argument: world
Environment Variables
Access and set environment variables:
// env.js
console.log('Environment:', process.env.NODE_ENV || 'development');
console.log('Custom variable:', process.env.MY_VARIABLE);
console.log('Database URL:', process.env.DATABASE_URL || 'Not set');
// Example usage with environment variables:
// NODE_ENV=production MY_VARIABLE=test node env.js
Set environment variables when running:
NODE_ENV=production MY_VARIABLE=test node env.js
Debugging Node.js Applications
Node.js includes a powerful debugging system:
# Start with inspector (default port 9229)
node --inspect app.js
# Break on first line
node --inspect-brk app.js
# Custom port
node --inspect=9222 app.js
# Enable remote debugging
node --inspect=0.0.0.0:9229 app.js
Using Chrome DevTools:
- Start your application with
node --inspect app.js - Open Chrome and navigate to
chrome://inspect - Click "Open dedicated DevTools for Node"
- Set breakpoints and debug your application
Common CLI Tools
Node Version Manager (nvm)
# Install and use different Node.js versions
nvm install 18.16.0 # Install specific version
nvm use 18.16.0 # Switch to version
nvm ls # List installed versions
npm (Node Package Manager)
# Common npm commands
npm init # Initialize a new project
npm install # Install dependencies
npm update # Update packages
npm audit # Check for vulnerabilities
Common Command Line Flags
# Show Node.js version
node --version # or -v
# Show V8 version
node --v8-options
# Show command-line help
node --help
Runtime Behavior:
# Check syntax without executing
node --check app.js
# Show stack traces for warnings
node --trace-warnings app.js
# Set max memory (in MB)
node --max-old-space-size=4096 app.js
# Preload a module before execution
node --require dotenv/config app.js
Performance and Optimization:
# Enable ES module loader
node --experimental-modules app.mjs
# Enable experimental features
node --experimental-repl-await
# Enable experimental worker threads
node --experimental-worker