Skip to content

Run JavaScript with Node.js: Your First Script, process.argv, Exit Codes and the Event Loop

NeedMyTools Academy

Sign in free to read the full lesson

Sign in or create a free account

Free forever. No card, no paywall.

In the browser, JavaScript lives inside a page and talks to buttons and forms. Node.js takes the same language out of the browser and lets it run as a normal program on your computer or a server. You write a .js file, type node and its name in a terminal, and the output appears right there. This first lesson covers what every later lesson builds on: running a script, printing results, reading values typed after the command, telling the terminal whether the script succeeded, what is missing compared with the browser, and the order in which Node.js runs your code. Every output below was recorded with Node.js 22.23.4 on Linux.

Quick answer

To run JavaScript with Node.js, install Node.js from nodejs.org, save your code in a file such as hello.js, open a terminal in that folder and run node hello.js. console.log prints to the terminal. Values typed after the file name arrive in process.argv, starting at index 2. Set process.exitCode = 1 to report failure. Browser objects such as document and window do not exist in Node.js.

30 min read and practice · Content checked on 27 November 2026

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.

Example: hello.js
hello.js
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);
Real output: terminal, node hello.js
Hello, Sara
Total: 49.5 AED
Node version: v22.23.4
Platform: linux
Real output: terminal, node -p "Math.round(1234.5 * 1.05)"
1296

Sign 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
Sign in or create a free account

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.

Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.