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 Windows
Open the official Node.js download page, select the LTS release and choose the Windows installer appropriate for your computer. Run the installer from nodejs.org and follow its prompts. Keep the standard npm option selected. On a managed work or school computer, use your organisation’s approved software process.
Finish the installer and open a NEW PowerShell window. An already-open terminal may still have the old PATH. Check the installed versions before trying to run a project.
- Download the LTS Windows installer from the official Node.js site.
- Run the installer and keep the standard Node.js and npm components.
- Open a new PowerShell window and run the verification commands.
PowerShell: verify Node.js and npm
node --version
npm --version
where.exe nodeRun hello.js in PowerShell
Create a folder named javascript-start in Documents and save the example as hello.js inside it. Replace the path below with the folder where you actually saved the file. If the file is missing, check the current folder with Get-Location and list its files with Get-ChildItem.
PowerShell: enter your folder and run the script
cd "$HOME\Documents\javascript-start"
node hello.jsCommon Windows problems
If node is not recognised, close and reopen PowerShell, then check where.exe node. If several Node paths appear, an older installation or version manager may be taking precedence. Confirm the version used by the project before removing or changing an installation.
If PowerShell blocks npm.ps1 because of an execution policy, npm.cmd --version can call the Windows command shim without changing the machine-wide policy. Ask your administrator before changing policy on a managed computer. If npm reports EBADENGINE, compare node --version with the required engine range; installing the same incompatible npm version again will not fix it.
If Node cannot find hello.js, check the filename, including hidden .txt extensions, and run the command from the file’s folder. If document is not defined, that code needs a browser page rather than a bare Node process.
PowerShell: check the npm command shim if npm.ps1 is blocked
npm.cmd --versionSave 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