Node.js Guide: JavaScript Scripts, Packages, and Files
Node.js runs JavaScript outside a browser. You can use it to build web servers, APIs, command-line tools, and scripts that work with files on your computer.
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.
Start by opening a terminal window and checking which version of Node is installed before running a JavaScript file.
What Is Node.js?
Node.js is a JavaScript runtime, a program that executes JavaScript outside a browser. Give it a script and it runs the code; a short file-processing job usually exits when finished, while a server keeps running to handle requests.
Node provides built-in tools for files, networking, and operating-system tasks. A normal web page cannot read arbitrary files on your computer without a browser-supported permission flow. Node scripts can access files allowed by your operating-system permissions.
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.
How Do You Check Whether Node.js Is Installed?
Run node --version in a terminal to check whether Node.js is installed. A version number confirms that your terminal can find it:
node --version // terminal output (your version number may differ):
// 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. You see your typed word, then a complaint that it is unknown.
That failure is worth recognizing 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. Once it finishes, close the terminal, open a new one, and run node --version again.
Which Node.js Guide Should You Read First?
Start with running a JavaScript file, then learn package management and file automation. You only need basic JavaScript values and functions to begin.
- Running JavaScript with Node covers checking your install and understanding version numbers, running a file with
node file.js, what"type": "module"changes aboutimport, reading the arguments you typed after the filename withprocess.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
npmscripts so you stop memorizing them, what a lockfile is for and why it belongs in version control, and whennpxis 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 changes the 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.
When Does a Node.js Script Stop?
A Node.js process normally stops when no active work keeps its event loop running. Synchronous statements run in order, while callbacks and other asynchronous work may finish later. Variables disappear when the process ends unless the script saves their values 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.
An active timer, file watcher, or listening server can keep Node running. The asynchronous JavaScript guide explains how work can continue after the initial statements finish.
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 output, run as: node check.js report.txt
// Checking report.txt Common Pitfalls
- Running the command in the wrong folder:
node app.jslooks forapp.jsin the folder you are currently in, not the folder you last had open in your editor. Runpwdandlsfirst, and the files guide covers the same trap for the paths inside your code. - Expecting browser objects to exist:
document,window, andalertare 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 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.
Frequently Asked Questions
Do you need to learn the terminal to use Node?
You need a few terminal basics to follow these Node.js guides: change folders, list files, run a script, and stop a running program. Editors such as VS Code include a terminal, so you can write and run your code in one window.
Which version of Node should you install?
Install a supported Node.js LTS release unless your project requires another supported version. For an existing project, check its runtime requirements first. A version manager lets different projects use different Node versions.
Is Node a language?
Node.js is not a programming language. It runs JavaScript outside a browser and adds tools for tasks such as reading files and handling network requests. You learn those tools without learning a second language.
Can you run your browser code in Node?
Browser JavaScript can run in Node.js when it uses language features supported by that Node version and does not depend on browser-only APIs. Code that uses document or window needs a browser or a suitable replacement for those APIs.
How do you stop a Node.js command that seems stuck?
Press Ctrl+C in the terminal to interrupt a Node.js command. Most programs stop, but a program can handle that signal and continue running. When the terminal prompt returns, you can enter another command.
Sources
-
[1]
Introduction to Node.js(nodejs.org)
-
[2]
Node.js Releases(nodejs.org)
-
[3]
Command-Line API(nodejs.org)
Read Next
Checking your install and reading version numbers, running a file, what type module changes, reading arguments from process.argv, the two output streams, and exit codes.
The fields Node and npm each read, why dependencies and devDependencies are separate lists, naming commands as npm scripts, what a lockfile protects, and when npx is the right tool.
Is JavaScript still worth learning with AI agents? What it is used for beyond the browser, JavaScript or Python first, frontend or backend, Node, Deno or Bun, and a roadmap to every guide.