Skip to content
Sign inGet all access
Chapter 1·12 minFree

Getting Started

Running a file, printing, template literals, and the two module systems.

Run a file

Save a file and hand it to node:

app.mjs
console.log("Hello from Node");
Output$ node app.mjsHello from Node

There is no compile step and no main function. The file runs top to bottom.

node with no arguments opens a REPL, which is useful for checking one expression:

terminal
node
Output$ nodeWelcome to Node.js v26.12.1.> 1 + 12> const a = 5undefined> a * 210> .exit

console.log

It takes any number of values and separates them with spaces:

app.mjs
console.log("a", "b", 1, true);
Outputa b 1 true

Objects and arrays print structurally, without you formatting them:

app.mjs
console.log({ name: "ada", age: 36 });
console.log([1, 2, 3]);
Output{ name: 'ada', age: 36 }[ 1, 2, 3 ]

console.error writes to stderr instead of stdout, so it survives a > redirect:

app.mjs
console.log("plain");
console.error("to stderr");
Outputplainto stderr

Template literals

Backticks interpolate with ${}, and the expression inside can be any JavaScript:

app.mjs
const name = "ada";
const age = 36;

console.log(`${name} is ${age}`);
console.log(`next year: ${age + 1}`);
Outputada is 36next year: 37

A backtick string can also span lines, keeping its own indentation:

app.mjs
const msg = `Dear ada,
  your build passed.
Thanks.`;

console.log(msg);
OutputDear ada, your build passed.Thanks.

Semicolons are optional

The parser inserts them for you, so this runs:

app.mjs
const a = 1
const b = 2
console.log(a + b)
Output3

The rule is not “end of line” — a line starting with [ or ( continues the previous statement:

app.mjs
const x = 5
const value = x
[1, 2].forEach((n) => console.log(n))
OutputTypeError: Cannot read properties of undefined (reading 'forEach')

Comments

app.mjs
// A line comment.

/* A block comment
   over two lines. */

const data = await Promise.resolve("ready")
console.log(data)
Outputready

.mjs vs .cjs

JavaScript has two module systems, and the extension decides which one a file gets. .mjs is ESM — import and export:

greet.mjs
export function greet(name) {
  return `hello ${name}`
}
app.mjs
import { greet } from "./greet.mjs"

console.log(greet("ada"))
Output$ node app.mjshello ada

.cjs is CommonJS — require and module.exports, the older system Node shipped with:

greet.cjs
function greet(name) {
  return `hello ${name}`
}

module.exports = { greet }
app.cjs
const { greet } = require("./greet.cjs")

console.log(greet("linus"))
Output$ node app.cjshello linus

Mixing the keywords fails. require in an ESM file:

bad.mjs
const fs = require("node:fs")
OutputReferenceError: require is not defined in ES module scope, you can use import instead

And import in a CommonJS file:

bad.cjs
import { greet } from "./greet.cjs"
OutputSyntaxError: Cannot use import statement outside a module

A plain .js file is CommonJS by default. "type": "module" in package.json makes it ESM — the export below would be a SyntaxError without it:

package.json
{
  "type": "module"
}
app.js
export function add(a, b) {
  return a + b
}

console.log(add(2, 3))
Output$ node app.js5

Strict mode

A module is always in strict mode — you never write "use strict". Assigning to an undeclared name is an error:

app.mjs
count = 10
OutputReferenceError: count is not defined

The same two lines in a CommonJS file silently create a global instead:

app.cjs
count = 10
console.log("assigned without declaring:", count)
Outputassigned without declaring: 10

Its other changes are mostly about this, and land in chapter 5.