Codectionary / Developer documentation / JavaScript

Choose a supported Node.js release on Linux, check npm, and run a .js file from the shell.

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.

Choose an installation method

Linux distributions differ. Begin with the official Node.js download page, select the current LTS release and follow the method shown for your distribution and architecture. A distribution package may contain an older Node release; check node --version against the Node release-status page rather than assuming its package name means it is supported.

If you use a version manager such as nvm, follow its own installation instructions linked from the Node.js download page, reopen your shell, then install its current LTS selection. nvm is a separate community-maintained tool, not part of the JavaScript language or the Node.js binary. Avoid copying an unreviewed curl-pipe-shell command from a random tutorial.

If you use Windows Subsystem for Linux, install Node inside the Linux environment for Linux-shell commands. A Windows installation does not replace a deliberate Linux setup.

  1. Choose the current LTS release on nodejs.org.
  2. Follow a method appropriate for your distribution and CPU architecture.
  3. Open a new shell and verify the installed commands.

If you installed nvm by following its own instructions

nvm install --lts
nvm use --lts

Shell: verify the runtime and location

node --version
npm --version
command -v node

Run hello.js in the shell

Create javascript-start in your home directory and save the example as hello.js there. Then enter the folder and run the file. If your editor saves to a different directory, use that path instead.

Shell: enter your folder and run the script

cd ~/javascript-start
node hello.js

Common Linux problems

If node is missing, reopen the shell and inspect command -v node. Some older distribution packages use a different command name; prefer a supported Node installation that provides node, rather than making an unexplained alias.

If the version is EOL or too old for a package, select a supported LTS release with a method appropriate to your distribution. Do not fix an npm permission error by routinely running npm install with sudo; use project-local installs and check npm’s official permissions guidance.

If the file cannot be found, check pwd and ls and confirm that it is named hello.js. Browser-only APIs such as document are unavailable in an ordinary Node process.

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 -y

Install dependencies already listed by a project

npm install

Syntax

node hello.js

Examples

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!");
  1. The quotes create a string containing the message. console.log writes that string to the terminal when Node.js runs this file.
  2. 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.
  3. 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

Specifications & further reading