You've been using Node.js since Kindergarten to run JavaScript files. But Node can do much more than print to the console. Since it isn't trapped inside a web page, it can do things that browsers aren't allowed to do (for security reasons):
| The browser can... | Node.js can... |
|---|---|
| Change web pages (the DOM) | Read and write files on your computer |
| React to clicks and typing | Run web servers that websites talk to |
Store small things in localStorage | Talk to databases |
| Fetch data from APIs | Run command-line tools and scripts |
Both have JavaScript at their core: variables, functions, arrays, objects, classes, promises... everything you've learned works in both. What's different is the set of extra tools (APIs) each environment gives you. The browser gives you document and window. Node gives you modules for files, networks, and the operating system.
Modules in Node
Node supports the same import/export syntax. To turn it on, you either give your files the extension .mjs, or (more commonly) add "type": "module" to a file called package.json in your project folder. (You'll meet package.json properly in a moment.)
Node actually had its own module system long before import and export existed. It's called CommonJS, and it looks like this:
// exporting
module.exports = { add, circleArea };
// importing
const { add, circleArea } = require("./math-utils.js");
You'll still see require in lots of tutorials, older projects and Stack Overflow answers. It does the same job as import. For new projects, use import/export (often called ES modules, or ESM), which works in both browsers and Node.