Difficulty: Beginner
Before writing Node.js applications, you need to install the runtime on your machine. The recommended approach is to use a version manager like nvm (Node Version Manager) on macOS and Linux, or nvm-windows on Windows. A version manager lets you install multiple versions of Node.js side by side and switch between them easily. This is essential because different projects may require different Node versions, and you do not want to be stuck with a single system-wide installation.
Node.js follows a predictable release schedule with two main tracks: LTS (Long Term Support) and Current. Even-numbered major versions (18, 20, 22) become LTS releases and receive bug fixes and security patches for 30 months. Odd-numbered versions (19, 21, 23) are Current releases with the latest features but shorter support windows. For production applications, always use an LTS version. For experimenting with cutting-edge features, you can use the Current release.
Once installed, Node.js provides two primary ways to run JavaScript. The first is the REPL (Read-Eval-Print Loop), an interactive shell you launch by typing 'node' in your terminal with no arguments. The REPL reads your input, evaluates it, prints the result, and loops back for more input. It is excellent for quick experiments, testing snippets, and exploring APIs. The REPL supports tab completion, multi-line input, and special commands like .help, .clear, .exit, and .save.
The second way to run JavaScript is by executing script files. You create a .js file and run it with 'node filename.js'. This is how you run actual applications. You can also use the --watch flag (node --watch app.js) to automatically restart the script when the file changes, which is very useful during development. For TypeScript, tools like tsx and ts-node let you run .ts files directly without a separate compilation step.
The Node.js installation also comes with npm (Node Package Manager) and, starting from Node 16.9, corepack which provides access to yarn and pnpm. You can verify your installation by running 'node --version' and 'npm --version' in your terminal. The npx command, bundled with npm, lets you execute packages without installing them globally, which is useful for one-off commands like creating new projects with 'npx create-react-app my-app'.
# Install nvm (macOS/Linux)
# curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
# List available versions
# nvm ls-remote
# Install latest LTS
# nvm install --lts
# Install a specific version
# nvm install 20.11.0
# Switch between versions
# nvm use 20
# nvm use 18
# Set a default version
# nvm alias default 20
# Check current version
console.log("Node version:", process.version);
console.log("npm would be checked with: npm --version");
nvm allows you to install and switch between multiple Node.js versions. The commented lines show terminal commands (not JavaScript). Always use an LTS version for production projects.
// In the REPL (type 'node' in terminal):
// > 2 + 2
// 4
// > const name = "Node"
// undefined
// > `Hello ${name}`
// 'Hello Node'
// > .help
// Shows all REPL commands
// > .exit
// Exits the REPL
// The _ variable holds the last evaluated result:
// > 5 * 3
// 15
// > _ + 1
// 16
// You can also evaluate expressions in one shot:
// node -e "console.log(2 10)"
// 1024
console.log("REPL special variable _ holds last result");
console.log("Use .help in REPL to see all commands");
The REPL is an interactive playground for testing code quickly. The underscore (_) variable always contains the result of the last expression. The -e flag lets you evaluate a JavaScript expression directly from the command line.
// Save as app.js and run with different flags:
// Basic execution:
// node app.js
// Watch mode (auto-restart on changes):
// node --watch app.js
// Check syntax without running:
// node --check app.js
// Print the V8 version:
// node -p "process.versions.v8"
console.log("Script is running!");
console.log("V8 version:", process.versions.v8);
console.log("All versions:", JSON.stringify(process.versions, null, 2));
The --watch flag is built into Node.js 18+ and eliminates the need for nodemon during development. The process.versions object contains version strings for all the native dependencies bundled with Node.js including V8, libuv, and OpenSSL.
nvm, Node Versions, REPL, Running Scripts, LTS vs Current