JavaScript outside the browser: Node.js
Understand what a server runtime does and which APIs belong to it.
- Distinguish browser APIs from Node.js APIs
- Run a local .mjs file with node
- Explain why a server can access files and private credentials
JavaScript is a language; the runtime supplies its surroundings. In a browser, the surroundings include document, buttons and the page DOM. Node.js runs JavaScript outside the browser and supplies server APIs such as node:fs for files and node:http for HTTP. Both runtimes share language features such as arrays, functions and Promises.
Try it
Up to 15 XPWhich runtime owns it?
Sort each capability. Notice that JavaScript language features belong to both runtimes.
“document.querySelector()”
“import { readFile } from "node:fs/promises"”
“Array.prototype.map()”
“addEventListener on a button”
“JSON.parse()”
Run a first Node script
Install a supported Node.js release, save the following as hello.mjs, then run node hello.mjs in a terminal. The .mjs extension uses ECMAScript modules, so import works without a package configuration. node:fs/promises reads a file asynchronously. The browser exercise runner cannot access your disk, so run this example locally.
1import { writeFile, readFile } from "node:fs/promises";
2await writeFile("hello.txt", "Hello from Node.js");
3const message = await readFile("hello.txt", "utf8");
4console.log(message);Node.js is used for web APIs, command-line tools, build tooling and background jobs. It has an event loop and asynchronous I/O so one process can keep handling work while file or network operations complete. Start with the official Node.js introduction.
Key takeaways
JavaScript is the language; browser and Node.js provide different APIs.
Use .mjs for a beginner-friendly local ES module example.
Keep private credentials on the server and validate incoming data.
Lesson quiz
2 questions · pass with 2 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Practice: write JavaScript
Write the program, run it against the test cases, and use the feedback to build your JavaScript skills.
Choose the right runtime
Input is a comma-separated list of tasks: button, file, array, server, page. Print browser for button/page, node for file/server and both for array. This small exercise runs in the browser; the file example above runs locally in Node.
- Mixed tasks
- Shared
JavaScript runs in a sandboxed browser frame with a 1.5 second time limit. Input is available as the string variable input.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…