JavaScript with Node

Published Updated

This topic covers JavaScript run on your own computer rather than in a web page. Node is the program that makes that possible, and this section is where the language stops being about buttons and starts being about files, folders, and small tools you run yourself.

It is also the part of JavaScript most likely to stop somebody who is otherwise doing fine. The obstacle is rarely the code. It is the terminal window, the version numbers, and the sense that everyone else was handed a manual you never got.

So this page goes slower than its sibling topics. It starts with the window, not the language, and every command below appears in full with the answer it actually gave.

What Node Is and What It Is Not

Node is a program you install, the same way you installed your browser. You hand it a file of JavaScript and it runs that file, prints whatever the file prints, and then stops.

What it adds to JavaScript is access to your machine. A browser deliberately refuses to let a web page read your documents folder, and Node is not sandboxed that way, which is exactly why it can do the useful work in this topic.

It is not unrestricted, though. Your files are still governed by the operating system's own permissions, and Node has an opt-in Permission Model of its own, available since Node 20 and stable from Node 22.13.

What it removes is the page. There is no document, no window, no clicking and no styling, so the code from the DOM topic fails immediately if you paste it into a Node file. The language is the same in both places; the surroundings are not.

Opening a Terminal and Finding Your Way

A terminal is a window that takes typed commands instead of clicks. On macOS it is the Terminal app, on Windows it is Windows Terminal or PowerShell, and on Linux it is usually just called Terminal. Your code editor almost certainly has one built in, which is the easiest place to start.

When it opens you get a prompt: some text, then a blinking cursor waiting for you. You type one line, press Enter, and the program either prints something or prints nothing and gives the prompt back. Nothing happens until you press Enter.

Four commands carry you through this whole topic. pwd prints the folder you are currently in, cd followed by a folder name moves you into it, cd .. moves you back up one level, and ls lists what is there.

Those four are the macOS and Linux spellings, and Windows PowerShell accepts pwd, cd, and ls too.

Two habits save more time than any command. Pressing Tab completes a half-typed folder name, which also proves you spelled it right. Pressing Ctrl and C together usually interrupts whatever is running and hands the prompt back.

Checking Whether You Already Have Node

Before installing anything, ask. Type this and press Enter:

node --version
// terminal output on the machine this page was written on:
// v24.16.0

A version number means Node is installed and you can move straight to the first guide. Anything that reads as an unknown-command error means Node is not installed, or not on the list of places your shell searches.

The wording depends on the shell. macOS and Linux shells say command not found, while PowerShell says the term is not recognized. The pattern is the same either way: your typed word, then a complaint that it is unknown.

That failure is worth recognising rather than fearing. Nothing has broken, and nothing has been installed by accident.

To install it, download the current Long Term Support release from nodejs.org and run the installer for your operating system. The installer screens are not reproduced here, because they change and because this page can only honestly describe what it has run. Once it finishes, close the terminal, open a new one, and run node --version again.

JavaScript Node Topics

  • Running JavaScript with Node covers checking your install and understanding version numbers, running a file with node file.js, what "type": "module" changes about import, reading the arguments you typed after the filename with process.argv, the difference between the two output streams, and the exit code a script leaves behind.
  • package.json, npm, and Scripts covers the fields in that file and which ones Node itself reads, why dependencies and devDependencies are separate lists, naming long commands as npm scripts so you stop memorising them, what a lockfile is for and why it belongs in version control, and when npx is the right tool.
  • Files, Environment Variables, and Small Automations covers reading and writing files with fs/promises, building paths that survive being run from the wrong folder, keeping keys out of your code with environment variables and --env-file, and one complete bulk-edit script with a dry run, shown before and after it changed real files.

Read them in that order. The second assumes you can run a file, and the third assumes you can read the arguments the second one teaches you to pass.

The Rule Everything Here Depends On

A Node script runs from top to bottom, once, and the process then ends as soon as it has no work left that is still holding it open. Nothing is remembered between runs unless the script wrote it somewhere.

That explains most of what surprises people here. A variable you set is gone, while a change you made to a file is not, because the file is on disk.

Some things do hold the process open: a pending timer, a file watcher, an open socket. A server is simply the most familiar of them, not a special case.

It also explains the shape of every script in this topic. Each one takes input from its arguments or a file, does its work, prints what it did, and exits with a code saying whether it worked:

const args = process.argv.slice(2);
if (args.length === 0) {
  console.error("Usage: node check.js <name>");
  process.exit(1);
}
console.log("Checking", args[0]);
// node 24.16.0 output, run as: node check.js report.txt
// Checking report.txt

Common Pitfalls

  • Running the command in the wrong folder: node app.js looks for app.js in the folder you are currently in, not the folder you last had open in your editor. Run pwd and ls first, and the files guide covers the same trap for the paths inside your code.
  • Expecting browser objects to exist: document, window, and alert are provided by the browser, so a Node run stops on the first line that uses one. The DOM topic says the same thing from the other direction.
  • Assuming a file finished successfully because it printed something: output and success are separate signals. The running guide covers exit codes, which is the answer the terminal actually acts on.
  • Copying an installation command from a forum post: a command that installs software deserves more suspicion than a command that prints a version. Take install instructions from nodejs.org and package instructions from the package's own documentation.

Common Questions

Do you need to learn the terminal to use Node?

You need about five commands, not a course. Changing folder, listing what is in one, running a file, reading the answer, and stopping a program covers nearly everything on these three guides. Editors such as VS Code have a terminal panel built in, so you can stay in one window while you learn them.

Which version of Node should you install?

Take the current Long Term Support release from nodejs.org unless a project tells you otherwise. Through Node 26 only even-numbered majors became Long Term Support, and from Node 27 every major is planned to reach it. A version manager lets each project pin its own version rather than fighting over one global install.

Is Node a language?

No, and the distinction saves confusion later. The language is JavaScript, and Node is a program that runs it outside a browser, adding the abilities a browser deliberately withholds such as reading your files. Learning Node is learning what it adds, not learning a second language.

Can you run your browser code in Node?

Only the parts that are pure JavaScript. Functions, objects, promises, and array methods behave identically, because they belong to the language rather than to the browser. Anything that touches a page, such as document or window, does not exist in Node and fails on the line that uses it.

What do you do when a command seems stuck?

Press Ctrl and C together in the terminal window, which usually interrupts the running program. It sends an interrupt signal rather than forcing an exit, so a program that installs a handler can catch it and carry on. A prompt that returns means the program has ended and the terminal is ready again.

Continue Learning JavaScript

  • Return to the JavaScript guide for where the language fits, the full learning path, and what to build first.
  • Read JavaScript Language Basics for the values, functions, objects, and modules every script here is written from.
  • Read JavaScript Async for the promises behind every await in the file-handling examples.
  • Read JavaScript and the DOM for the browser half of the language, which is the half Node does not have.
  • Read TypeScript when a script grows big enough that you want the compiler checking it, and note the boundary: the runtime and the package manager are taught here, the compiler is taught there.

Sources

  1. [1]
  2. [2]
    Node.js Releases
    (nodejs.org)
  3. [3]
    Command-Line API
    (nodejs.org)