Skip to content
Codeloom
Node.js

Install Node.js and Run Your First Script

A practical guide to installing Node.js with nvm on macOS, Linux, and Windows — checking versions, running .js files, and using the REPL for quick experiments.

·7 min read · By Codeloom
Beginner 8 min read

What you'll learn

  • Why nvm is the right way to install Node on a dev machine
  • How to install Node on macOS, Linux, and Windows
  • How to check your version and switch between releases
  • How to run a .js file with the node command
  • How to use the Node REPL for quick experiments

Prerequisites

You can install Node.js by downloading an installer from the website, but that is not what most working developers do. They use a version manager instead. This post walks through the setup that scales — install once, switch versions per project, never fight your system again.

If you have never seen Node before, start with What Is Node.js? for the background.

Why a version manager

Different projects need different Node versions. A legacy service might be locked to Node 18; a new project might target Node 22. The installer from nodejs.org puts a single version on your system, and upgrading or downgrading is a chore.

A version manager solves this. You install many Node versions side by side, and switch between them with a single command. The tool of choice on macOS and Linux is nvm (Node Version Manager). On Windows, nvm-windows does the same job.

Install on macOS or Linux

Open a terminal and run the official install script:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

The script adds a few lines to your shell config (~/.zshrc on modern macOS, ~/.bashrc on most Linux distros). Close and reopen your terminal, or source the file:

source ~/.zshrc
# or
source ~/.bashrc

Confirm nvm is on your path:

command -v nvm
# output: nvm

If you see nvm, you are ready. If you see nothing, your shell config did not load the install snippet — open the file and check that the NVM_DIR lines exist near the bottom.

Install on Windows

nvm itself does not support Windows. Use nvm-windows instead — a separate project with similar commands.

  1. Visit github.com/coreybutler/nvm-windows/releases
  2. Download nvm-setup.exe from the latest release
  3. Run the installer and accept the defaults
  4. Open a new PowerShell or Command Prompt window

Verify the install:

nvm version
# output: 1.1.12

From here, the commands are nearly identical to nvm on macOS/Linux.

Install Node itself

With nvm installed, pick the latest LTS release. LTS (Long Term Support) versions get security patches for thirty months — exactly what you want for real work.

nvm install --lts

nvm downloads the release, installs it, and sets it as the active version. Test it:

node --version
# output: v22.11.0

npm --version
# output: 10.9.0

npm ships with Node, so you get it for free.

To install a specific version:

nvm install 20
nvm install 18.19.0

And to switch between installed versions:

nvm use 20
node --version
# output: v20.18.0

nvm use --lts
node --version
# output: v22.11.0

Pin a version per project

Drop a .nvmrc file at the root of any project:

echo "22" > .nvmrc

Then nvm use inside that directory will read the file and switch automatically:

nvm use
# output: Now using node v22.11.0

Commit .nvmrc to your repo. Anyone who clones it gets the right version with one command.

Try it yourself. Install the latest LTS with nvm install --lts. Then install Node 20 alongside it with nvm install 20. Switch between them using nvm use 22 and nvm use 20, and confirm with node --version after each switch. You now have two Node versions ready to go.

Run your first script

Create a file called hello.js in any folder:

// hello.js
const name = 'world';
console.log(`Hello, ${name}!`);

Open a terminal in that folder and run it:

node hello.js
# output: Hello, world!

That is the whole workflow. Edit, save, run. No build step, no configuration, no boilerplate.

Try a slightly bigger script — one that actually does something useful:

// sum.js
const numbers = process.argv.slice(2).map(Number);
const total = numbers.reduce((acc, n) => acc + n, 0);
console.log(`Sum of ${numbers.join(', ')} is ${total}`);

Run it with arguments:

node sum.js 3 4 5
# output: Sum of 3, 4, 5 is 12

process.argv is the array of command-line arguments. The first two entries are the path to Node and the path to your script — that is why we slice(2).

The REPL

A REPL — Read-Eval-Print Loop — is an interactive prompt for trying things out. Type node with no arguments:

node

You will see a > prompt. Type any JavaScript expression and press Enter:

> 2 + 2
4
> const greet = (name) => `hi, ${name}`
undefined
> greet('Yash')
'hi, Yash'
> [1, 2, 3].map(n => n * n)
[ 1, 4, 9 ]

The REPL is perfect for exploring an API quickly. Want to see what Object.keys does on a date? Try it. Want to test a regex? Type it. No file to create, no script to run.

Press Ctrl+D (or .exit) to leave the REPL.

A few REPL conveniences:

  • Tab completion — type Array. and hit Tab to see every available method.
  • History — Up/Down arrows step through previous lines, even across sessions.
  • _ variable — holds the result of the last expression. Useful for chaining.
> 10 * 10
100
> _ + 1
101

Running TypeScript or modern JS

If you want to run TypeScript directly, modern Node has built-in stripping in v22.6+:

node --experimental-strip-types script.ts

For older versions or full type checking, tsx is the popular tool:

npm install -g tsx
tsx script.ts

You do not need any of this to get started. Plain .js files run as-is.

Try it yourself. Open the REPL and explore. Try Math.random(), new Date(), JSON.stringify({a: 1}), and 'hello'.toUpperCase(). Then exit, write a short script that prints the current time every second using setInterval, and run it with node. Press Ctrl+C to stop.

Common problems

command not found: node — your shell did not pick up nvm. Close the terminal completely and reopen it. If that still fails, check that your shell config (~/.zshrc or ~/.bashrc) has the NVM_DIR block at the bottom.

nvm: command not found after install — the install script edits your shell rc file. If you use a non-standard shell (fish, nushell), you may need to add the snippet manually. The nvm README has instructions.

Permission errors when installing global packages — never use sudo npm install -g. If you see EACCES errors, you probably installed Node from the system package manager rather than nvm. Remove the system version and reinstall with nvm.

Old Node version sticking aroundwhich node shows you which binary is running. If it points to /usr/local/bin/node rather than ~/.nvm/..., you have a stale install ahead of nvm in your PATH.

What gets installed

When you install Node via nvm, you get:

  • node — the runtime itself
  • npm — the package manager (covered in npm and package.json)
  • npx — a tool for running packages without installing them globally
  • corepack — manages alternative package managers like pnpm and yarn

All four live under ~/.nvm/versions/node/<version>/bin/ and are automatically on your PATH when nvm is active.

Recap

You now know:

  • nvm (or nvm-windows) is the cleanest way to install Node
  • nvm install --lts gets you the current Long Term Support release
  • node --version confirms what is active; nvm use <version> switches
  • A .nvmrc file in a project pins the version for anyone who clones it
  • node script.js runs a file; node alone opens the REPL
  • process.argv exposes command-line arguments inside your script

Next steps

Now that Node runs, you need a way to organise code across multiple files and pull in libraries from the wider ecosystem. The next post covers exactly that.

Next: Node.js Modules — CommonJS vs ES Modules

Questions or feedback? Email codeloomdevv@gmail.com.