bugl
bugl
HomeLearnPatternsSearch
HomeLearnPatternsSearch

Loading lesson path

Learn/Node.js/Node.js Tutorial
Node.js•Node.js Tutorial

Node.js Command Line Usage

Concept visual

Node.js Command Line Usage

push / pop from the top({[← top

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.

Note:

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

# 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

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:

Using the REPL

> 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

Command Line Arguments

// 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

Environment Variables

Access and set environment variables:

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:

Formula

// NODE_ENV = production MY_VARIABLE = test node env.js

Set Environment Variables

# Set environment variables when running

Formula

NODE_ENV = production MY_VARIABLE = test node env.js

Debugging Node.js Applications Node.js includes a powerful debugging system that integrates with Chrome DevTools:

Basic Debugging Commands

# 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.js

Using Chrome DevTools for Debugging

Start your application with node --inspect app.js

Open Chrome and navigate to chrome://inspect

Click on "Open dedicated DevTools for Node"

Set breakpoints and debug your application

Common CLI Tools

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

Common Command Line Flags

Formula

Node.js provides several command - line flags to control its behavior. Here are some of the most useful ones:

Basic Flags

Formula

# 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

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.js

Performance and Optimization

Formula

# Enable ES module loader node -- experimental - modules app.mjs
# Enable experimental features node -- experimental - repl - await
# Enable experimental worker threads node -- experimental - worker

Previous

Node.js vs Browser

Next

Node.js V8 Engine