Expert · Performance and Node.js · topic 54 of 61 · 30 min
Node.js basics
Explanation
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
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'process: arguments, environment and exit codes
// 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 failurenpm 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.
{
"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" }
}| Command | Does |
|---|---|
| npm init -y | Create package.json |
| npm install express | Add a dependency (needed when the app runs) |
| npm install --save-dev vitest | Add a devDependency (only for development and tests) |
| npm install | Install everything listed (after cloning a project) |
| npm run dev | Run the "dev" script |
| npx some-tool | Run 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).
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
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.
// 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)
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.
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.)
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
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Files and a tiny server in NodePerformance and Node.js · same module
- Performance thinking and Big-OPerformance and Node.js · same module
- Setting up your toolsFull-Stack Development · What full-stack development is
- Node.js: JavaScript on the serverBackend Development · Node.js and Express
- Modules, npm and ViteFrontend Web Development · Async JavaScript, modules and tools
- Semantic versioning, tags and releasesDevOps: Git, Docker, Kubernetes and CI/CD · Git in depth
Questions people ask about this
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.
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.
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.
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.
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
- 2 more topics in “Performance and Node.js” before its exam: Files and a tiny server in Node, Real project tooling.
- Module 11 exam: Performance and Node.js · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, Interview preparation and career. 61 topics left in the course before the final exam.