Node.js is the program that runs JavaScript code on a machine, outside of a web browser.
This page covers Node.js, the JavaScript runtime. For a language-agnostic introduction to programs that run code, see Interpreter.
Why Node.js Exists#
JavaScript was originally built to run inside web browsers. A browser has its own engine that reads and runs the JavaScript on a web page. On its own, that engine cannot run JavaScript anywhere else, such as a plain file on the machine.
Node.js takes the engine that browsers use to run JavaScript and packages it as a standalone program. With Node.js installed, a .js file can be run directly from the terminal, the same way other languages run their code, without a browser involved.
Running a File#
Node.js runs a JavaScript file with the node command followed by the file name:
node index.jsNode reads the file and carries out its statements, printing any output to the terminal.
npm#
Node.js comes with npm, its package manager. npm installs packages, extra code written by others, into a project so the code can use them. Installing Node.js installs npm at the same time, so both are available together.
⚠️ Mistakes
Thinking Node.js is a different language
Node.js is not a language. The language is still JavaScript. Node.js is the program that runs JavaScript outside a browser. The same JavaScript that runs on a web page can run under Node.js, with some differences in what is available.
Expecting browser features to work in Node.js
Browser-only things like document or window do not exist in Node.js, because there is no web page. Node.js provides its own set of built-in features for working with files, the network, and the system instead.
Forgetting that npm comes with Node.js
There is no separate npm install. Installing Node.js installs npm as well. If the npm command is not found, the usual cause is that Node.js itself is not installed or not on the PATH.
📚 Resources
- About Node.js (external link), official site
- Node.js (external link), Wikipedia