Running JavaScript with Node
Overview
Now that Node is installed, let’s run some code. There are three ways to run JavaScript with Node: a file, the interactive REPL, and a one-line -e command. You’ll use files for almost everything, but the other two are handy for quick experiments.
Running a file
Create a folder for this course, and inside it a file called hello.js:
const name = 'Node';
console.log(`Hello from ${name} ${process.version}!`);
console.log('Running on', process.platform);Run it from the terminal, in the same folder:
node hello.js
# Hello from Node v26.4.0!
# Running on darwinThat’s it — no HTML page, no browser. Node reads the file, runs it from top to bottom, and exits when there’s nothing left to do. If the program is waiting for something (a server listening for requests, a timer), Node keeps running until that’s finished or you press Ctrl + C.
The REPL
Type node on its own to start the REPL (Read–Eval–Print Loop) — an interactive prompt, like the browser console:
$ node
> 2 + 2
4
> const words = ['fast', 'async', 'javascript']
undefined
> words.map((w) => w.toUpperCase())
[ 'FAST', 'ASYNC', 'JAVASCRIPT' ]
> .exitUseful REPL tricks:
_holds the result of the last expression..helplists the special commands;.editorlets you paste multi-line code.- Built-in modules are available without importing: try
os.cpus().lengthorpath.join('a', 'b'). awaitworks directly at the prompt.
For a one-off line, use -e (evaluate) or -p (evaluate and print):
node -p "os.totalmem() / 1024 ** 3" # total RAM in GBCommand-line arguments
Anything you type after the file name is passed to your program in process.argv, an array of strings:
// args.js
console.log(process.argv);node args.js report.csv --verbose
# [
# '/Users/asha/.nvm/versions/node/v26.4.0/bin/node',
# '/Users/asha/course/args.js',
# 'report.csv',
# '--verbose'
# ]The first two items are the Node executable and your script; your own arguments start at index 2. A common pattern:
const [, , ...args] = process.argv;
const file = args[0];
const verbose = args.includes('--verbose');For anything more complex, use the built-in parseArgs helper from node:util — you’ll see it in The process and the command line.
Watch mode
Re-running node app.js after every change gets tedious. Watch mode restarts your program automatically whenever you save:
node --watch app.jsIt watches the file you run and everything it imports. This replaces older tools such as nodemon for most projects. You’ll typically put it in an npm script so the whole team uses it:
{
"scripts": {
"dev": "node --watch src/server.js",
"start": "node src/server.js"
}
}What’s different from the browser
Your JavaScript knowledge transfers directly, but a few things change.
No DOM. There is no document, window or alert. Code that touches the page won’t run in Node.
globalThis is the global object. In the browser it’s window; in Node it’s simply globalThis (older code also uses global).
Browser-compatible APIs are there. Many familiar globals exist in Node: console, setTimeout, setInterval, fetch, URL, URLSearchParams, TextEncoder, AbortController, structuredClone, crypto.randomUUID(), WebSocket and more. Code using these often runs unchanged in both places.
Each file is a module. Variables declared at the top of a file are private to that file — they don’t become globals. You share code between files with import and export (next chapter).
Top-level await
In ES module files you can use await outside any function:
// main.mjs
import { readFile } from 'node:fs/promises';
const text = await readFile('notes.txt', 'utf8');
console.log(text.length);The .mjs extension tells Node the file is an ES module. In the next chapter you’ll set "type": "module" in package.json so plain .js files work this way too.
Running TypeScript
Recent versions of Node can run .ts files directly by stripping type annotations:
node app.tsThis only removes types — it doesn’t type-check, and some TypeScript-only features (like enum) aren’t supported. Use the TypeScript compiler (tsc --noEmit) to check types. This course uses plain JavaScript so that every example runs everywhere, but everything you learn applies equally to TypeScript projects.
Tip: Keep one terminal running
node --watchand another for commands likenpm install. VS Code’s terminal can split side by side.
A greeting script
Write greet.js so that node greet.js Asha prints Hello, Asha! and node greet.js (no name) prints Hello, world!. Run it in watch mode and change the message to see it restart.
// greet.js
// process.argv holds the command-line arguments// greet.js
const name = process.argv[2] ?? 'world';
console.log(`Hello, ${name}!`);
// Run: node greet.js Asha
// Watch: node --watch greet.js AshaSummary
node file.jsruns a file;nodeon its own opens the REPL for quick experiments.process.argvholds the command-line arguments; your own start at index 2.node --watchrestarts your program whenever you save a file.globalThisreplaceswindow; there is no DOM in Node.- Top-level
awaitworks in ES module files (.mjs, or"type": "module").