What Node.js adds
JavaScript is the language. Node.js is a runtime that executes JavaScript outside a browser. It supplies facilities for command-line programs, file access and servers, but it does not give a script a browser page or a document object.
Developers use Node.js to run local scripts, web servers, test tools and build tools. npm is the package manager normally installed alongside an official Node.js installation. You can learn browser JavaScript without Node; you need a runtime such as Node when you want to run a .js file in a terminal.
Choose a supported release
Check the official Node.js download and release pages when you install. Choose the release currently labelled LTS (Active or Maintenance) for beginner and production work, then use the newest patch offered for that supported line. A Current release is for trying newer features; an EOL release no longer receives normal upstream support.
Do not select a release only because its major version is even or copy a version number from an old tutorial: Node.js release policy changes over time. A project may specify an engine range in package.json; check that range before choosing a runtime. Installing Node does not upgrade an incompatible project automatically.
Install safely on macOS
On the official Node.js download page, select the current LTS release and a macOS installer appropriate for your Mac. Use the official installer or follow the page’s current instructions for a version manager if you need several Node versions. Do not mix methods without checking which node is on your PATH.
After installation, quit and reopen Terminal. The installer does not turn an editor into a JavaScript runtime; it provides the node command and normally npm.
- Select LTS and the correct macOS download at nodejs.org.
- Follow the installer prompts or the current official version-manager guidance.
- Open a new Terminal window and verify both commands.
Terminal: verify the runtime and location
node --version
npm --version
command -v nodeRun hello.js in Terminal
Create javascript-start in your home folder. Save the example as hello.js inside it in a plain-text editor. Then enter that folder and run Node. The path in the command assumes that location; adjust it if you saved the file elsewhere.
Terminal: enter your folder and run the script
cd ~/javascript-start
node hello.jsCommon macOS problems
If Terminal says command not found, reopen it and check command -v node. If a version manager is in use, follow its setup instructions for your shell. A package installed through a different manager may put another Node first on PATH.
If you get a permissions error during npm installs, avoid using sudo as a default fix. npm recommends using a version manager or a writable location for global tools. A normal project-local npm install should be run from your project folder.
If Node reports that it cannot find hello.js, check pwd and ls, confirm the file is saved with a .js extension, and run it from the folder containing it.
Save and run your first file
Create a folder you control, then save the example below in that folder as hello.js using a plain-text editor. The file extension matters: hello.js.txt is a text file, not the JavaScript file you intend to run. In a terminal, change into the folder and run the commands for your operating system below.
node hello.js asks Node to execute the saved file. The output appears in the terminal, not on a web page. A script that calls document.querySelector needs a browser page; running it directly with Node will fail because Node has no document object.
What npm and package.json do
npm installs and manages packages for a project. In a new project folder, npm init -y creates a starter package.json. That file records project metadata, package dependencies and optional named scripts. npm install installs dependencies declared by an existing package.json; npm run NAME runs a script only if NAME is defined there.
A project may also contain a package-lock.json that records resolved dependencies for repeatable installs. npm packages can execute code during installation, so review dependencies before adding them. Do not install a newer npm globally merely because a prompt advertises one: a new npm major can require a newer Node release.
Create a project manifest in an empty folder
npm init -yInstall dependencies already listed by a project
npm installSyntax
node hello.jsExamples
A first JavaScript file
Save this as hello.js, then run node hello.js in a terminal. Expected output: Hello, Codectionary!
console.log("Hello, Codectionary!");- The quotes create a string containing the message. console.log writes that string to the terminal when Node.js runs this file.
- Save the file as hello.js, open a terminal in its folder and run node hello.js. The output is the message without quote marks.
- Try changing the message and running the file again. If the old message appears, check that you saved the file and are running the file you edited.
Best practices
- Choose a currently supported LTS release from the official Node.js site.
- Reopen your terminal after installation so it reads the updated PATH.
- Save scripts as .js files and run them from the folder that contains them.
- Check the Node version required by a package before upgrading npm or installing dependencies.
Runtime & versions
Browsers and JavaScript runtimes. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
ECMAScript specification