All academies

Running Your First Program

Write and run your first script, use the REPL, and read CLI args.

12 minLevel 3 of 20

Write a script, run it with node

A Node.js program is just a text file containing JavaScript. Create a file called hello.js, write some code, and run it by passing the filename to the node command. Node reads the file top to bottom and executes it.

That is the entire development loop: edit file, run `node file.js`, read the output. No compiler, no build step, no browser.

hello.js
// Save this as hello.js and run:  node hello.js
const name = "world";
console.log("Hello, " + name + "!");

// You can compute anything JavaScript can:
const sum = [1, 2, 3, 4, 5].reduce((a, b) => a + b, 0);
console.log("1+2+3+4+5 =", sum);

console: more than just log

console.log is your primary output tool, but the console object has friends worth knowing:

  • console.error — writes to the stderr stream instead of stdout, so errors can be redirected separately
  • console.warn — same as error, conventionally for warnings
  • console.table — renders arrays of objects as a table
  • console.time / console.timeEnd — quick-and-dirty performance timing

console.log also accepts multiple arguments and formats objects readably — no need to concatenate strings manually.

The console toolbox
console.log("plain log");
console.error("this goes to stderr");

const user = { name: "Ada", role: "engineer" };
console.log("Multiple", "args", "and objects:", user);

console.time("loop");
let total = 0;
for (let i = 0; i < 1e6; i++) total += i;
console.timeEnd("loop"); // prints elapsed time

The REPL: an interactive playground

Run node with no arguments and you enter the REPL — Read, Evaluate, Print, Loop. You type an expression, Node evaluates it and prints the result immediately. It is perfect for trying out an API or checking how a method behaves.

Useful REPL tricks:

  • The special variable _ holds the last result
  • Press Tab to autocomplete object properties
  • .help lists dot-commands; .exit (or Ctrl+D) quits
  • Multi-line input works — the REPL detects unfinished blocks
A REPL session
$ node
Welcome to Node.js v20.11.0.
> 2 + 2
4
> "abc".toUpperCase()
'ABC'
> _ + "!"
'ABC!'
> .exit

Reading command-line arguments

Scripts become tools when they accept input. process.argv is an array of the command-line arguments:

  • process.argv[0] — the path to the node executable
  • process.argv[1] — the path to your script
  • process.argv[2] and beyond — the actual arguments the user typed

Because the first two entries are always paths, real arguments start at index 2. The common idiom is process.argv.slice(2). Note that every argument arrives as a string — convert numbers yourself with Number().

greet.js — a script that takes arguments
// Run as:  node greet.js Ada Grace
const args = process.argv.slice(2);

if (args.length === 0) {
  console.log("Usage: node greet.js <name> [more names...]");
} else {
  for (const name of args) {
    console.log("Hello, " + name + "!");
  }
}
console.log("(received " + args.length + " argument(s))");

Exit codes: how scripts report success

Every process ends with an exit code. 0 means success; any non-zero value signals failure. Shells, CI pipelines, and other programs check this code to decide what to do next.

Node exits with 0 automatically when your script finishes normally. To signal failure, set process.exitCode = 1 and let the program finish naturally — this is safer than calling process.exit(1), which stops immediately and can cut off pending writes or callbacks.

Signalling failure politely
const args = process.argv.slice(2);

if (args.length === 0) {
  console.error("Error: expected at least one argument");
  process.exitCode = 1; // shell will see a failure...
} else {
  console.log("Processing:", args.join(", "));
}

console.log("Cleanup still runs before exit.");

Check your understanding

5 questions

Question 1 of 5

Score: 0

Multiple choice

How do you run a file named app.js with Node?