Running Scripts

`node file.js` — Your First Node Program

Running Scripts

Save JS to a file, run it with node. The most basic workflow there is.

3 min read Level 1/5 #nodejs#scripts#run
What you'll learn
  • Run a .js file
  • Run a .mjs file (ESM)
  • Use --watch for auto-reload

The most common Node workflow: put code in a file, run node file.js.

Hello World

// hello.js
console.log("Hello from Node!");
$ node hello.js
Hello from Node!

ESM vs CommonJS

By default .js is CommonJS (the old Node module system). For modern ES modules:

  • Rename to .mjs, OR
  • Set "type": "module" in package.json

We’ll do that in detail later. For now:

// example.mjs
import { readFileSync } from "node:fs";
const text = readFileSync("hello.js", "utf8");
console.log(text);
$ node example.mjs
console.log("Hello from Node!");

Auto-Reload with --watch

Tired of stopping/restarting? Node 22 has built-in watch mode:

node --watch hello.js

Edit and save the file — Node re-runs automatically. No nodemon needed.

Running TypeScript Directly

Node 22+ can strip TS types at runtime:

node --experimental-strip-types script.ts

For full TS features (decorators, JSX), use tsx:

npx tsx script.ts

Shebangs (Unix)

Make a script self-executing:

#!/usr/bin/env node
console.log("I'm a CLI!");

Then chmod +x my-cli and run ./my-cli.

Up Next

Reading arguments your script was called with.

CLI Arguments →