Loading lesson path
Concept visual
Node.js provides a powerful command line interface (CLI) that allows you to run JavaScript files, manage packages, debug applications, and more. This guide covers the essential commands and techniques every Node.js developer should know.
All commands should be run in a terminal or command prompt. On Windows, you can use Command Prompt, PowerShell, or Windows Terminal.
Formula
On macOS/Linux, use Terminal.Basic Node.js Commands These are the most common commands you'll use when working with Node.js applications:
# 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
Formula
The Node.js REPL (Read - Eval - Print Loop) is an interactive shell for executing JavaScript code.The REPL is started by running node in the terminal:
> const name = 'Node.js';
> console.log(`Hello, ${name}!`);> .help // Show available commands > .exit // Exit REPL
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:
Formula
// All arguments: ['/path/to/node', '/path/to/args.js', 'hello', 'world']// First argument: hello // Second argument: world
// 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:
Formula
// NODE_ENV = production MY_VARIABLE = test node env.js# Set environment variables when running
Formula
NODE_ENV = production MY_VARIABLE = test node env.jsDebugging Node.js Applications Node.js includes a powerful debugging system that integrates with Chrome DevTools:
# Start with inspector (listens on default port 9229) node --inspect app.js
Formula
# Break on first line of application node -- inspect - brk app.js
# Specify a custom port node -- inspect = 9222 app.js# Enable remote debugging (be careful with this in production)
Formula
node -- inspect = 0.0.0.0:9229 app.jsStart your application with node --inspect app.js
Click on "Open dedicated DevTools for Node"
Formula
Node.js comes with several useful command - line 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
Formula
Node.js provides several command - line flags to control its behavior. Here are some of the most useful ones:Formula
# Show Node.js version node -- version # or - v
# Show V8 version node -- v8 - options
# Show command - line help node -- help# Check syntax without executing node --check app.js
Formula
# Show stack traces for warnings node -- trace - warnings app.js# Set max memory (in MB)
Formula
node -- max - old - space - size = 4096 app.js
# Preload a module before execution node -- require dotenv/config app.jsFormula
# Enable ES module loader node -- experimental - modules app.mjs
# Enable experimental features node -- experimental - repl - await
# Enable experimental worker threads node -- experimental - worker