Node.js Get Started

Get Node.js installed, run your first script from the command line, and understand how the runtime executes your files.

Installing Node.js

Download Node.js from nodejs.org, choosing the LTS (Long-Term Support) version for stability. Installers are available for Windows, macOS, and Linux, and most package managers (Homebrew, apt, winget, nvm) can install it too. Node.js ships with npm, its package manager, so you get both tools in one install.

  • Download the LTS installer for Windows, macOS, or Linux from nodejs.org.
  • Or use a version manager like nvm to install and switch between Node versions easily.
  • Verify the install with node --version and npm --version in a terminal.
  • npm (Node Package Manager) installs automatically alongside Node.js.

Your First Script

Node.js runs plain .js files directly — there's no compilation step and no HTML wrapper required. Create a file named app.js, add JavaScript, and execute it with the node command from your terminal.

app.js

// app.js
console.log("Hello from Node.js!");
console.log("Node version:", process.version);
console.log("Current directory:", process.cwd());

// Run with: node app.js
Note: process is a global object Node.js injects into every script. It exposes runtime details like process.version, command-line arguments (process.argv), and environment variables (process.env) — none of which exist in a browser.

Command-Line Arguments

greet.js

// greet.js
const name = process.argv[2] || "stranger";
console.log(`Hello, ${name}!`);

// Run with: node greet.js Alex
// Output: Hello, Alex!

Node.js vs. the Browser

ConceptIn the BrowserIn Node.js
Global objectwindowglobal (or globalThis)
Entry pointindex.html loads scriptsnode app.js runs a file directly
File/OS accessNot available, for securityFull access via fs, path, and os modules

info.js

// info.js
const os = require("os");

console.log("Platform:", os.platform());
console.log("CPU cores:", os.cpus().length);
console.log("Free memory (MB):", Math.round(os.freemem() / 1024 / 1024));

// Run with: node info.js

Exercise: Node.js Get Started

How do you run a file named app.js with Node.js from a terminal?