Your First Node.js Program

Run a JavaScript file with the node command, try lines one at a time in the REPL, rerun a file on every save with node --watch, read an error and its stack trace, and see what Node has that a browser doesn't.

Novice · Node.js · JavaScript · Updated

By

After this lesson you can

  • Run a JavaScript file with the node command, rerun it on every save with node --watch, and try single lines in the REPL
  • Read a Node error message and its stack trace to find the error's name, its file, line and column, and the line that called it
  • Tell what Node has that a browser doesn't, such as process, and what it lacks, such as document and window

Key takeaways

  • node and a file's name, such as node hello.js, runs that file from top to bottom and prints what console.log writes.
  • node on its own opens the REPL, where each line runs as you press Enter. .exit leaves it.
  • node --watch runs a file again each time you save it. Ctrl+C stops it.
  • An error names the file and line where it stopped, the error's name and its message. The stack trace under it lists the route there, your own code first.
  • Node has process, files and the network, but no web page, so there's no document or window.

Every Node program you'll write, from a small tool to an API, starts the same way: you write a few lines, run them, and read what comes out, or what went wrong. This chapter's example is Maple Street Bakery, a neighborhood bakery whose owner wants the day's price board printed by a program, so it can go on the screen by the counter.

Your first program: hello.js

In your nodejs-course folder, make a folder named maple-street-bakery, and open it in VS Code with File > Open Folder. In the Explorer, click New File, name it hello.js, type this line and save with Ctrl+S (Cmd+S on a Mac):

console.log("Good morning from Maple Street Bakery!");

Open the terminal (Terminal > New Terminal) and run the file with the node command and the file's name:

node hello.js
Good morning from Maple Street Bakery!

Node reads the file, runs it from top to bottom, and console.log prints a line in the terminal. Give console.log several values, separated by commas, and it prints them on one line with a space between each. Make price-board.js:

console.log("MAPLE STREET BAKERY");
console.log("Sourdough loaf:", "$6.50");
console.log("A dozen bagels:", "$12.00");
console.log("Loaves left:", 14);
MAPLE STREET BAKERY
Sourdough loaf: $6.50
A dozen bagels: $12.00
Loaves left: 14

Try one line at a time: the REPL

Type node on its own and press Enter. Node prints a welcome line with its version, then a > prompt and waits. This is the REPL (read "rep-ul"): it reads a line, evaluates (runs) it, prints the result and loops back for the next. It's a scratch pad for checking one idea without making a file:

> 6.50 * 2
13
> "Maple Street Bakery".toUpperCase()
'MAPLE STREET BAKERY'
> console.log("Sold out!")
Sold out!
undefined
> .exit

The REPL shows each line's value. console.log prints its text and has no value of its own, so the REPL shows undefined after it. .exit (or Ctrl+C twice) takes you back to the terminal.

Run it again on every save: node --watch

Running the same file after every change gets old. --watch (read "dash dash watch") keeps Node watching the file:

node --watch hello.js

Node runs it, then waits. Add a second line to hello.js, console.log("Fresh sourdough at 7 AM.");, and save. Node runs it again by itself, and the terminal shows:

Restarting 'hello.js'
Good morning from Maple Street Bakery!
Fresh sourdough at 7 AM.
Completed running 'hello.js'. Waiting for file changes before restarting...

Press Ctrl+C to stop watching.

Reading an error and its stack trace

A smoke alarm panel says which room the smoke is in. Node's error messages do the same for your code. Make board.js, with one letter missing from console inside a function:

function printPrice(item, price) {
  consol.log(item + ": " + price);
}

printPrice("Sourdough loaf", "$6.50");
C:\Users\Sam\nodejs-course\maple-street-bakery\board.js:2
  consol.log(item + ": " + price);
  ^

ReferenceError: consol is not defined
    at printPrice (C:\Users\Sam\nodejs-course\maple-street-bakery\board.js:2:3)
    at Object.<anonymous> (C:\Users\Sam\nodejs-course\maple-street-bakery\board.js:5:1)
    at Module._compile (node:internal/modules/cjs/loader:1872:14)
    at Object..js (node:internal/modules/cjs/loader:2003:10)
    at Module.load (node:internal/modules/cjs/loader:1594:32)
    at Module._load (node:internal/modules/cjs/loader:1396:12)
    at wrapModuleLoad (node:internal/modules/cjs/loader:255:19)
    at Module.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:154:5)
    at node:internal/main/run_main_module:33:47

Node.js v24.19.0

Read it from the top:

  1. Where: board.js:2, the file and line 2. Node shows the line, with a ^ (read "caret") under the spot.
  2. The name: ReferenceError, Node met a name it doesn't know.
  3. The message: consol is not defined.

The at lines are the stack trace: the route the program took to get there, newest first. at printPrice (...board.js:2:3) is line 2, column 3, inside printPrice; board.js:5:1 is line 5, which called it. The lines with node:internal are Node's own code: skip them, as their numbers change between versions. Your path, and the version on the last line, will be your own.

What Node has that a browser doesn't

Node has no web page, so it has no document or window. It has things a browser keeps from pages instead, such as process, an object about the running program. typeof (read "type of") names what kind of value something is:

console.log("Running on", process.platform);
console.log(typeof process);
console.log(typeof document);
console.log(typeof window);
Running on win32
object
undefined
undefined

On a Mac, process.platform is darwin. process.version holds Node's version, the one node --version prints. Node can read and write files too, which Reading and Writing Files shows. Write document.title in a Node file, and it stops with ReferenceError: document is not defined.

Check yourself

2 questions to try on the lesson page.

So what

node, the REPL and --watch are how you'll run every program in this course, and an error read from the top, then down its stack trace, tells you the line to fix and the line that called it.

Key terms