In this lesson you will
- Run a JavaScript file with node and read its output
- Read command line arguments from process.argv
- Report success or failure with exit codes and console.error
- Predict the order of synchronous code, promises and timers
Your first script
Install the LTS version from nodejs.org; it includes node and the package manager npm. Check it worked by running node -v in a terminal, which prints the version, such as v22.23.4 on our machine. Then make a folder, create hello.js in your editor and run node hello.js from a terminal opened in that folder. Node.js reads the file from top to bottom, runs it, and exits when there is nothing left to do.
Everything you know from JavaScript works: variables, arrays, reduce, template strings. console.log writes to the terminal instead of the browser console, and it separates several arguments with spaces, as in "Total: 49.5 AED". Node.js also gives you a global process object that describes the running program: process.version is the Node.js version and process.platform is the operating system, linux, darwin for macOS or win32 for Windows.
For a quick calculation you do not even need a file. node -p prints the value of an expression and node -e runs code: node -p "Math.round(1234.5 * 1.05)" printed 1296. Typing just node opens the REPL, an interactive prompt where each line runs as you press Enter; leave it with Ctrl+D or .exit.
const name = "Sara";
const items = [12.5, 7, 30];
const total = items.reduce((sum, price) => sum + price, 0);
console.log(`Hello, ${name}`);
console.log("Total:", total, "AED");
console.log("Node version:", process.version);
console.log("Platform:", process.platform);Hello, Sara
Total: 49.5 AED
Node version: v22.23.4
Platform: linux1296Sign in free to read the full lesson
You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.
Unlocks after sign-in:
- 3 more sections with live code examples
- Common mistakes to avoid
- Hands-on coding exercise with automatic checks
- Practice quiz and the graded lesson test
Free forever. No card, no paywall.
Questions people ask
How do I check that Node.js is installed?
Open a terminal and run node -v and npm -v. Each prints a version number. If you see "command not found", reinstall from nodejs.org and open a new terminal so it picks up the new path.
What is the difference between process.exit and process.exitCode?
process.exitCode = 1 records the code and lets Node.js finish pending work, such as writing output, before it exits. process.exit(1) stops immediately and can cut that work off, so prefer exitCode.
Can I run a Node.js script again automatically when I save it?
Yes. node --watch hello.js restarts the script whenever the file or a module it imports changes. It is built in, so you no longer need a separate tool such as nodemon for simple scripts.
Is Node.js single threaded?
Your JavaScript runs on one main thread, while Node.js does file and network work in the background and calls you back when it is done. For heavy calculations you can use worker threads, but most web servers never need them.
Does fetch work in Node.js without installing anything?
Yes. fetch is built into current Node.js versions, with the same API as in the browser, as the rates example showed. Older guides install node-fetch or axios, which you no longer need for simple requests.
Sources
Facts in this lesson were checked against these official pages on 27 November 2026.
- Node.js: Introduction to Node.js
- Node.js docs: process
- Node.js: The Node.js event loop
- Node.js releases
Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.