Skip to content

Expert · Performance and Node.js · topic 54 of 61 · 30 min

Node.js basics

Explanation

Node.js runs JavaScript on servers and computers, and adds built-in modules for files (fs), paths (path), the operating system (os), processes (process) and networking (http). npm installs packages listed in package.json, scripts automate commands, and semantic versions (^1.4.2) control upgrades. Use the LTS release for real projects.

What Node.js adds

In the browser, JavaScript can change web pages but cannot read your files. Node.js removes the page and adds access to the computer: files, folders, network ports, environment variables and other programs. It is used for web servers and APIs, command-line tools, build tools (Vite, ESLint and TypeScript's compiler all run on Node) and scripts.

Built-in modules

javascript
import { readFile } from 'node:fs/promises';
import path from 'node:path';
import os from 'node:os';

console.log(path.join('data', 'orders', '2026.csv'));  // data/orders/2026.csv (\ on Windows)
console.log(path.extname('invoice.pdf'));               // '.pdf'
console.log(os.cpus().length, 'CPU cores');
console.log(process.platform, process.version);        // e.g. 'linux' 'v24.x.x'
The node: prefix makes it clear these are built-in modules, not npm packages.

process: arguments, environment and exit codes

javascript
// node greet.js Asha
const name = process.argv[2] ?? 'friend';   // argv[0] is node, argv[1] the script path
console.log(`Namaste, ${name}!`);
console.log(process.env.NODE_ENV);           // an environment variable (or undefined)
if (!name) process.exit(1);                   // a non-zero exit code means failure

npm and package.json

npm (Node Package Manager) comes with Node. It installs packages (other people's code) from the npm registry and records them in package.json, the file that describes your project. package-lock.json records the exact versions installed, so every developer and server gets the same ones; commit it to Git.

json
{
  "name": "shop-api",
  "version": "1.0.0",
  "type": "module",
  "scripts": {
    "start": "node src/server.js",
    "dev": "node --watch src/server.js",
    "test": "vitest run"
  },
  "dependencies": { "express": "^5.1.0" },
  "devDependencies": { "vitest": "^3.2.0" }
}
Version numbers are examples; npm writes the real ones when you install.
CommandDoes
npm init -yCreate package.json
npm install expressAdd a dependency (needed when the app runs)
npm install --save-dev vitestAdd a devDependency (only for development and tests)
npm installInstall everything listed (after cloning a project)
npm run devRun the "dev" script
npx some-toolRun a package's command without installing it globally

Semantic versioning

Versions look like MAJOR.MINOR.PATCH (5.1.0). PATCH fixes bugs, MINOR adds features without breaking anything, MAJOR may break things. ^5.1.0 allows any 5.x.x from 5.1.0 up (not 6.0.0); ~5.1.0 allows only 5.1.x patches.

LTS releases

Node.js releases a new major version twice a year. Even-numbered versions become LTS (Long-Term Support) and get fixes for about two and a half years in total; odd-numbered ones are short-lived. For anything real, install the version marked LTS on nodejs.org. node --watch app.js restarts your program automatically when files change (handy during development).

Careful: Never commit node_modules to Git (add it to .gitignore; npm install recreates it), and think before adding a dependency for a tiny task. Every package is code you did not review that runs with your app's permissions; supply-chain attacks through npm packages do happen. Check downloads, maintenance and run npm audit.
Think of it like this: If JavaScript in the browser is a cook working in someone else's restaurant kitchen with only the tools provided, Node.js is your own kitchen: you can open the fridge (files), take deliveries (network) and hire helpers (npm packages).
Tip: Use npm ci (instead of npm install) on build servers and in CI: it installs exactly what package-lock.json says and fails if the two files disagree.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

Node.js basics · 1/6

Node.js basics

In this lesson you will use Node.js to run JavaScript on your own computer and on servers.

Prefer a video by a teacher? Find free videos on “node js tutorial for beginners npm package.json” on YouTube ↗

In real life: A command-line GST calculator for an accountant

An accountant's office uses a tiny Node script: node gst.js 25000 18 prints the tax and the total. It reads the numbers from process.argv, checks them, and exits with code 1 and a clear message if they are wrong, so it can also be used in other scripts.

javascript
// gst.js  →  node gst.js 25000 18
const [amountText, rateText = '18'] = process.argv.slice(2);
const amount = Number(amountText), rate = Number(rateText);
if (!Number.isFinite(amount) || !Number.isFinite(rate)) {
  console.error('Usage: node gst.js <amount> [rate]');
  process.exit(1);
}
const tax = Math.round(amount * rate / 100);
console.log(`Tax: ₹${tax}  Total: ₹${amount + tax}`);

Practice (0/3 done)

Task 1 · Code · Javascript

Write parseArgs(argv) for a script run as node calc.js 25000 18. argv is like process.argv. Return { amount, rate } as numbers, with rate defaulting to 18 when missing.

Task 2 · Code · Javascript

Write allowsVersion(range, version) for caret ranges only: allowsVersion("^5.1.0", "5.4.2") is true; it must have the same MAJOR and be greater than or equal to the range version. (Ignore pre-releases.)

Task 3 · Practice

Install the LTS version of Node.js. Create a project with npm init -y, add "type": "module", write gst.js that reads an amount and a rate from process.argv and prints the tax, and add a "gst" script so that npm run gst -- 1000 18 works.

Quick quiz

Question 1

1.node app.js hello: what is process.argv[2]?

Question 2

2.Which belongs in devDependencies?

Question 3

3.Does "^2.3.0" allow version 3.0.0?

Question 4

4.Which file should be committed to Git?

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

node app.js hello: what is process.argv[2]?

Answer: "hello"

argv[0] is node, argv[1] is the script, argv[2] is the first argument.

Which belongs in devDependencies?

Answer: vitest (your test runner)

Tools needed only for development and testing go in devDependencies.

Does "^2.3.0" allow version 3.0.0?

Answer: No

A caret never allows a new major version.

Which file should be committed to Git?

Answer: package-lock.json

The lock file guarantees the same versions everywhere; node_modules and secrets are not committed.

Which Node.js version should a beginner install?Related

Answer: The version nodejs.org marks as LTS

LTS versions get bug and security fixes for a long time. Even-numbered majors become LTS.

From Setting up your tools

Which Node.js version should a beginner install?Related

Answer: The LTS version

LTS means Long Term Support. It is the stable version that keeps getting fixes, so it is the safe choice.

From Node.js: JavaScript on the server

package.json has "dayjs": "^1.11.13". Which version can npm install?Related

Answer: 1.12.0

^ allows newer versions with the same major number: 1.12.0 yes, 2.0.0 no, and older ones like 1.10.0 no.

From Modules, npm and Vite

package.json has "express": "~4.18.2". Which version may npm install?Related

Answer: 4.18.5

The tilde allows newer patches of the same MAJOR.MINOR only: at least 4.18.2 and below 4.19.0.

From Semantic versioning, tags and releases

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. 2 more topics in “Performance and Node.js” before its exam: Files and a tiny server in Node, Real project tooling.
  2. Module 11 exam: Performance and Node.js · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Interview preparation and career. 61 topics left in the course before the final exam.

← Previous: Memory and garbage collection