Running JavaScript with Node
Node.js programs are usually run from the terminal.
Instead of loading a script with a <script> tag in a browser, you run a file with the node command.
Checking Node.js
To check whether Node.js is installed, run:
node --versionYou should see a version number.
Example:
v22.11.0The exact version may be different.
You can also check npm:
npm --versionnpm is the package manager commonly installed with Node.js.
Your First Node Script
Create a file named hello.js.
console.log("Hello from Node.js");Run it:
node hello.jsOutput:
Hello from Node.jsNode reads the file, runs the JavaScript, prints the output, and exits.
Running Inline Code
You can run a short piece of JavaScript with node -e.
node -e "console.log(2 + 3)"Output:
5This is useful for quick experiments, but real programs usually live in files.
The Node REPL
If you run node without a file, Node opens a REPL.
REPL stands for:
Read Eval Print LoopStart it:
nodeThen type JavaScript:
2 + 3The REPL prints:
5Exit the REPL with:
Ctrl + C twiceor:
.exitScript Execution Order
Node runs top-level code from top to bottom.
console.log("First");
console.log("Second");
console.log("Third");Output:
First
Second
ThirdAsynchronous work can complete later.
console.log("Start");
setTimeout(() => {
console.log("Timer");
}, 0);
console.log("End");Output:
Start
End
TimerThis is the event loop in action.
Current Working Directory
Node scripts run from a current working directory.
That directory is usually the directory where you ran the command.
console.log(process.cwd());Run:
node app.jsprocess.cwd() helps you understand where relative file paths are resolved from.
This matters when reading files.
import { readFile } from "node:fs/promises";
const text = await readFile("data.txt", "utf8");
console.log(text);data.txt is looked up relative to the current working directory, not necessarily relative to the script file.
File Paths in Node.js
On macOS and Linux, paths usually use /.
notes/today.txtOn Windows, paths often use \.
notes\today.txtTo build paths safely across operating systems, use the path module instead of manually joining strings.
import path from "node:path";
const filePath = path.join("notes", "today.txt");
console.log(filePath);Top-Level await
Modern Node.js supports top-level await in ES modules.
const response = await fetch("https://example.com");
console.log(response.status);Whether a file is treated as an ES module depends on the file extension or package configuration.
You will learn more about this in the modules lesson.
Exit Codes
When a Node process finishes, it exits with a status code.
By convention:
0means success- non-zero means failure
You can set an exit code:
if (!process.env.API_KEY) {
console.error("Missing API_KEY");
process.exitCode = 1;
}Prefer setting process.exitCode when possible because it lets pending logs and cleanup work finish.
process.exit(1) stops the process immediately.
Watching for File Changes
Recent Node versions include watch mode:
node --watch app.jsNode restarts the script when files change.
This is useful during development.
In real projects, you may also see tools like nodemon, test runners, or framework-specific dev servers.
Common Mistakes
Do not include the node command inside the JavaScript file.
// Wrong inside app.js
node app.jsThe command belongs in the terminal.
Do not assume relative paths are always relative to the file.
// This depends on where you ran node from
await readFile("config.json", "utf8");Do not ignore error output.
console.error("Something went wrong");
process.exitCode = 1;Errors should usually be visible in terminal scripts.
Summary
Use node file.js to run JavaScript with Node.js.
The terminal, current working directory, process exit code, and module format all matter when running scripts.
Node.js feels like JavaScript, but it runs in a command-line/server environment instead of a browser page.