Running JavaScript with Node

Topic 3 of 4 in Getting started6 min readIntermediateFree course

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:

JavaScript
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:

Terminal
node hello.js
# Hello from Node v26.4.0!
# Running on darwin

That’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:

Terminal
$ node
> 2 + 2
4
> const words = ['fast', 'async', 'javascript']
undefined
> words.map((w) => w.toUpperCase())
[ 'FAST', 'ASYNC', 'JAVASCRIPT' ]
> .exit

Useful REPL tricks:

  • _ holds the result of the last expression.
  • .help lists the special commands; .editor lets you paste multi-line code.
  • Built-in modules are available without importing: try os.cpus().length or path.join('a', 'b').
  • await works directly at the prompt.

For a one-off line, use -e (evaluate) or -p (evaluate and print):

Terminal
node -p "os.totalmem() / 1024 ** 3"   # total RAM in GB

Command-line arguments

Anything you type after the file name is passed to your program in process.argv, an array of strings:

JavaScript
// args.js
console.log(process.argv);
Terminal
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:

JavaScript
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:

Terminal
node --watch app.js

It 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:

JSON
{
  "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:

JavaScript
// 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:

Terminal
node app.ts

This 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 --watch and another for commands like npm install. VS Code’s terminal can split side by side.

Exercise

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.

JavaScript
// greet.js
// process.argv holds the command-line arguments

Summary

  • node file.js runs a file; node on its own opens the REPL for quick experiments.
  • process.argv holds the command-line arguments; your own start at index 2.
  • node --watch restarts your program whenever you save a file.
  • globalThis replaces window; there is no DOM in Node.
  • Top-level await works in ES module files (.mjs, or "type": "module").