Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

n

tj859.8kMIT10.1.0

Interactively Manage All Your Node Versions

nvm, node, version, manager, switcher, node, binary, env

readme

n – Interactively Manage Your Node.js Versions

npm npm npm npm

Node.js version management: no subshells, no profile setup, no convoluted API, just simple.

usage animation

Supported Platforms

n is supported on macOS, Linux, including with Windows Subsystem for Linux, and various other unix-like systems. It is written as a BASH script but does not require you to use BASH as your command shell.

n does not work in native shells on Microsoft Windows (like PowerShell), or Git for Windows BASH, or with the Cygwin DLL.

Installation

If you already have Node.js installed, an easy way to install n is using npm:

npm install -g n

The default root location used when running n is /usr/local where a normal user does not have write permission. You may strike the same sort of permission error when using npm to install global modules, like the above command. You have three main options:

1) change the ownership of the relevant directories to yourself (see below) 2) tell n to use a custom location where you do have write permissions (see N_PREFIX) 3) put sudo in front of the command to run it as super user

n caches Node.js versions in subdirectory n/versions. The active Node.js version is installed in subdirectories bin, include, lib, and share.

To take ownership of the system directories (option 1):

# make cache folder (if missing) and take ownership
sudo mkdir -p /usr/local/n
sudo chown -R $(whoami) /usr/local/n
# make sure the required folders exist (safe to execute even if they already exist)
sudo mkdir -p /usr/local/bin /usr/local/lib /usr/local/include /usr/local/share
# take ownership of Node.js install destination folders
sudo chown -R $(whoami) /usr/local/bin /usr/local/lib /usr/local/include /usr/local/share

If npm is not yet available, one way to bootstrap an install is to download and run n directly. To install the lts version of Node.js:

curl -fsSL https://raw.githubusercontent.com/tj/n/master/bin/n | bash -s install lts
# If you want n installed, you can use npm now.
npm install -g n

If you don't need support for updates to n itself you can just save the download:

curl -fsSL -o /usr/local/bin/n https://raw.githubusercontent.com/tj/n/master/bin/n 
chmod 0755 /usr/local/bin/n
n install lts

Third Party Installers

On macOS with Homebrew you can install the n formula.

brew install n

Or on macOS with MacPorts you can install the n port:

port install n

On Linux and macOS, n-install allows installation directly from GitHub; for instance:

curl -L https://bit.ly/n-install | bash

n-install sets both PREFIX and N_PREFIX to $HOME/n, installs n to $HOME/n/bin, modifies the initialization files of supported shells to export N_PREFIX and add $HOME/n/bin to the PATH, and installs the latest LTS Node.js version.

As a result, both n itself and all Node.js versions it manages are hosted inside a single, optionally configurable directory, which you can later remove with the included n-uninstall script. n-update updates n itself to the latest version. See the n-install repo for more details.

Replacing a previous node install

Changing from a previous Node.js installed to a different location may involve a few extra steps. See docs for changing node location for a walk-through example of switching from using Homebrew to using n to manage Node.js.

You have a problem with multiple versions if after installing node you see the "installed" and "active" locations are different:

% n lts
     copying : node/20.12.2
   installed : v20.12.2 to /usr/local/bin/node
      active : v21.7.3 at /opt/homebrew/bin/node

Installing Node.js Versions

Simply execute n <version> to download and install a version of Node.js. If <version> has already been downloaded, n will install from its cache.

n 10.16.0
n lts

Execute n on its own to view your downloaded versions, and install the selected version.

$ n

  node/4.9.1
ο node/8.11.3
  node/10.15.0

Use up/down arrow keys to select a version, return key to install, d to delete, q to quit

(You can also use <kbd>j</kbd> and <kbd>k</kbd> to select next or previous version instead of using arrows, or <kbd>ctrl+n</kbd> and <kbd>ctrl+p</kbd>.)

If the active node version does not change after install, try opening a new shell in case seeing a stale version.

Specifying Node.js Versions

There are a variety of ways of specifying the target Node.js version for n commands. Most commands use the latest matching version, and n ls-remote lists multiple matching versions.

Numeric version numbers can be complete or incomplete, with an optional leading v.

  • 4.9.1
  • 8: 8.x.y versions
  • v6.1: 6.1.x versions

There are labels for two especially useful versions:

  • lts: newest Long Term Support official release
  • latest, current: newest official release

There is an auto label to read the target version from a file in the current directory, or any parent directory. n looks for in order:

  • .n-node-version: version on single line. Custom to n.
  • .node-version: version on single line. Used by multiple tools: node-version-usage
  • .nvmrc: version on single line. Used by nvm.
  • if no version file found, look for engine as below.

The engine label looks for a package.json file and reads the engines field to determine compatible Node.js. Requires an installed version of jq or node, and uses npx semver to resolve complex ranges.

There is support for the named release streams:

  • argon, boron, carbon: codenames for LTS release streams

These Node.js support aliases may be used, although simply resolve to the latest matching version:

  • active, lts_active, lts_latest, lts, current, supported

The last version form is for specifying other releases available using the name of the remote download folder optionally followed by the complete or incomplete version.

  • nightly
  • test/v11.0.0-test20180528
  • rc/10

Removing Versions

Remove some cached versions:

n rm 0.9.4 v0.10.0

Removing all cached versions except the installed version:

n prune

Remove the installed Node.js (does not affect the cached versions). This can be useful to revert to the system version of node (if in a different location), or if you no longer wish to use node and npm, or are switching to a different way of managing them.

n uninstall

Using Downloaded Node.js Versions Without Reinstalling

There are three commands for working directly with your downloaded versions of Node.js, without reinstalling.

You can show the path to the downloaded node version:

$ n which 6.14.3
/usr/local/n/versions/6.14.3/bin/node

Or run a downloaded node version with the n run command:

n run 8.11.3 --debug some.js

Or execute a command with PATH modified so node and npm will be from the downloaded Node.js version. (NB: npm run this way will be using global node_modules from the target node version folder.)

n exec 10 my-script --fast test
n exec lts zsh

Preserving npm

A Node.js install normally also includes npm, npx, and corepack, but you may wish to preserve your current (especially newer) versions using --preserve:

$ npm install -g npm@latest
...
$ npm --version
6.13.7
# Node.js 8.17.0 includes (older) npm 6.13.4
$ n -p 8
   installed : v8.17.0
$ npm --version
6.13.7

You can make this the default by setting the environment variable to a non-empty string. There are separate environment variables for npm and corepack:

export N_PRESERVE_NPM=1
export N_PRESERVE_COREPACK=1

You can be explicit to get the desired behaviour whatever the environment variables:

n --preserve nightly
n --no-preserve latest

Miscellaneous

Command line help can be obtained from n --help.

List matching remote versions available for download:

n ls-remote lts
n ls-remote latest
n lsr 10
n --all lsr

List downloaded versions in cache:

n ls

Download version into cache:

n download 22

Use n to access cached versions (already downloaded) without internet available.

n --offline 12

Remove the cache version after installing using --cleanup. This is particularly useful for a one-shot install, like in a docker container.

curl -fsSL https://raw.githubusercontent.com/tj/n/master/bin/n | bash -s install --cleanup lts

Normally n run, n exec, and n which will fail if the target version is not already in the cache. You can add --download to use the cache if available or download if required:

n --download run 18.3 my-script.js

Display diagnostics to help resolve problems:

n doctor

Custom Mirror

If you would like to use a different Node.js mirror which has the same layout as the default https://nodejs.org/dist/, you can define N_NODE_MIRROR.

One example is for users in China who can define:

export N_NODE_MIRROR=https://npmmirror.com/mirrors/node

Another example is the Node.js unofficial-builds project which has downloads for some platforms not made available officially, such as armv6l (Raspberry Pi) and 32-bit x86.

export N_NODE_MIRROR=https://unofficial-builds.nodejs.org/download/release

You may need to specify the architecture explicitly if not autodetected by n, such as using musl libc on Alpine:

export N_NODE_MIRROR=https://unofficial-builds.nodejs.org/download/release
apk add bash curl libstdc++
n --arch x64-musl install lts

If the custom mirror requires authentication you can add the url-encoded username and password into the URL. e.g.

export N_NODE_MIRROR=https://encoded-username:encoded-password@host:port/path

There is also N_NODE_DOWNLOAD_MIRROR for a different mirror with same layout as the default https://nodejs.org/download.

Custom Architecture

By default n picks the binaries matching your system architecture. For example, on a 64 bit system n will download 64 bit binaries.

On a Mac with Apple silicon:

  • for Node.js 16 and higher, n defaults to arm64 binaries which run natively
  • for older versions of Node.js, n defaults to x64 binaries which run in Rosetta 2

You can override the default architecture by using the -a or --arch option.

e.g. reinstall latest version of Node.js with x64 binaries:

n rm current
n --arch x64 current

Optional Environment Variables

The n command downloads and installs to /usr/local by default, but you may override this location by defining N_PREFIX. To change the location to say $HOME/.n, add lines like the following to your shell initialization file:

export N_PREFIX=$HOME/.n
export PATH=$N_PREFIX/bin:$PATH

If you want to store the downloads under a different location, use N_CACHE_PREFIX. This does not affect where the active node version is installed.

n defaults to using xz compressed Node.js tarballs for the download if it is likely tar on the system supports xz decompression. You can override the automatic choice by setting an environment variable to zero or non-zero:

export N_USE_XZ=0 # to disable
export N_USE_XZ=1 # to enable

You can be explicit to get the desired behaviour whatever the environment variable:

n install --use-xz nightly
n install --no-use-xz latest

In brief:

How It Works

n downloads a prebuilt Node.js package and installs to a single prefix (e.g. /usr/local). This overwrites the previous version. The bin folder in this location should be in your PATH (e.g. /usr/local/bin).

The downloads are kept in a cache folder to be used for reinstalls. The downloads are also available for limited use using n which and n run and n exec.

The global npm packages are not changed by the install, with the exception of npm itself which is part of the Node.js install.

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

Unreleased (date goes here)

10.1.0 (2024-11-09)

Added

  • --cleanup option to delete cached version after install for a one-shot install (#818)
  • download command to download Node.js version into cache (#821)
  • document using --download with run/exec/which in README
  • support trailing comments in .nvmrc file (#820)
  • mask password in download URL when displayed (#815)

Changed

  • removed stale code (#817)

10.0.0 (2024-09-06)

The major version bump is due to using jq in preference to node for reading package.json.

Changed

  • if jq is available, use jq for reading engines from package.json instead of using node (#810)
  • avoid a network lookup if auto or engine find a fully specified numeric version. (#813)

9.2.3 (2024-04-21)

Fixed

  • avoid problems with curl 8.7.1 and --compressed by removing option until fixed

9.2.2 (2024-04-21)

(No changes.)

9.2.1 (2024-02-25)

Fixed

  • n doctor works with custom N_CACHE_PREFIX

Added

  • expand tests in n doctor for folder existence and permissions

9.2.0 (2023-10-15)

Added

  • --offline for resolving target version against cached downloads instead of internet lookup (#785)

9.1.0 (2023-04-15)

Added

  • check for possible problem with multiple npm locations when running n doctor (#764)

9.0.1 (2022-11-04)

Fixed

  • Makefile compatible with more flavours of make (#745)
  • quote paths in Makefile in case PREFIX contains spaces (#746)

9.0.0 (2022-07-16)

Changed

  • --preserve preserves corepack too (#736)

Added

  • optional N_PRESERVE_COREPACK to change default behaviour for preserving corepack (#736)

8.2.0 (2022-04-18)

Added

  • log before copying files during install (#720)

8.1.0 (2022-03-18)

Added

  • optional N_CACHE_PREFIX for separate location for downloaded files than install location (#717)

8.0.2 (2022-01-09)

Fixed

  • improved warning message when utility location hash may be holding old location to cover a wider range of shells including dash (#707)

8.0.1 (2021-12-04)

Fixed

  • improve error handling for tar extraction errors (#701)
  • add tar flag for compatibility with tar builds which do not default to stdin (#697)

8.0.0 (2021-10-23)

Changed

  • display error if version missing in version file for n auto and n engine (rather than fallback to current) (#693)

7.5.0 (2021-09-26)

Added

  • support for Corepack (which was added to Node.js in v16.9.0)

7.4.1 (2021-09-11)

Fixed

  • run commands from correct directory after --download causes a download

7.4.0 (2021-09-10)

Added

  • support for --download option to run and exec to download the target version when needed (#685)

7.3.1 (2021-07-25)

Changed

  • Improved README for new users missing expected folders in /usr/local (#679)

7.3.0 (2021-06-06)

Added

  • ls-remote supports engine and auto labels (#675)
  • reduce engine and auto logging with --quiet (#675)
  • add WSL support to README (#676)
  • support for Emacs up and down keys (ctrl-p and ctrl-n) (#669)

Changed

  • diagnostic logging during processing of engine and auto written to stderr rather than stdout (#675)

7.2.2 (2021-04-25)

Fixed

  • arrow key navigation of version menu when terminal in application mode (e.g. PowerShell on Mac) (#668)

7.2.1 (2021-04-19)

Added

  • install native arm64 Node.js on Macs with Apple silicon for Node.js 16 and higher (#664)

7.2.0 (2021-04-19) [YANKED]

Released off wrong branch, essentially same as 7.1.0.

7.1.0 (2021-03-12)

Added

  • support installs where /usr/local/share/man is a symlink (such as archlinux)
  • remove requirement for rsync for --preserve
  • avoid install pollution if user installs global packages when using n exec

7.0.2 (2021-02-27)

Fixed

  • consistently log to STDOUT (#654)

7.0.1 (2021-01-30)

Changed

  • update bats, and use bats-assert for better unit test failure messages

Fixed

  • fail to display error in some cases for missing both curl and wget (#649)

7.0.0 (2020-12-20)

Changed

  • auto label now scans for package.json only if it can not find a version control file (#644)

Added

  • engine label to look for engines.node in package.json (as used byauto) (#644)

Fixed

  • avoid colorized grep output via GREP_OPTIONS breaking version lookup (#643)

6.8.0 (2020-12-12)

Fixed

  • suppress unwanted warning during auto when using npx with npm 7
  • temporary fix for installing on Mac with Apple M1 chip, look for x64 versions of node as arm64 not available yet

6.7.1 (2020-11-25)

Fixed

  • detect and handle a failed download of full archive (#635)

6.7.0 (2020-07-25)

Added

  • auto support for:
    • .node-version
    • .nvmrc
    • engines field of package.json

6.6.0 (2020-07-04)

Added

  • labels for node support aliases, such as lts_latest

Fixed

  • Enable xz support by default for macOS 11+ (#624)

6.5.1 (2020-04-11)

Added

  • specify auto to read the target version from a .n-node-version file (i.e. change filename)

6.5.0 (2020-04-11) [YANKED]

Added

  • specify auto to read the target version from a .node-version file (#616)

6.4.0 (2020-03-10)

Added

  • treat armv8l as arm64 (#614)

6.3.1 (2020-02-25)

Fixed

  • remove old version of node before copy to avoid firewall issues on macOS (#394)

6.3.0 (2020-02-24)

Added

  • --preserve to preserve npm and npx during install of node (#587)

6.2.0 (2020-01-29)

Added

  • Downloads now default to using tarballs compressed by xz over gzip, if xz support detected. (#606 #607)

6.1.3 (2019-11-23)

Added

  • added How It Works to README

Changed

  • simplified layout for n doctor output

6.1.2 (2019-11-16)

Added

  • advice to reset command hash when node location changes (#170 #381 #451 #588)
  • in README describe raw download of n to bootstrap install of node and npm

6.1.1 (2019-11-10)

Fixed

  • Specify --no-same-owner for tarball extraction so cache files not owned by unexpected user (when run with sudo) (#593)

6.1.0 (2019-10-25)

Added

  • deletion of cached versions from menu using 'd' (#590)

6.0.1 (2019-08-20)

Fixed

  • allow options to come after commands, especially n lsr --all

6.0.0 (2019-08-16)

Added

  • version specified using release stream codenames, like argon (#423)
  • version specified using nightly et al (#376)
  • n exec for running arbitrary command with node and npm in PATH (#185)
  • n run with legacy aliases of as and use
  • n lsr for listing matching remote versions, limited to 20 by default (#383)
  • n doctor for displaying diagnostic information
  • n install for people used to other products with this command (#524)
  • --insecure to disable curl/wget certificate checks
  • added npm version to installed message (#210 #484 #574)

Changed

  • Breaking wget now checks certificates (secure by default, same as curl setup). (#475 #509)
  • failure messages go to stderr instead of stdout
  • prefixed N_NODE_MIRROR to eventually replace NODE_MIRROR
  • Breaking n ls now lists local download versions (rather than remote versions)
  • lookup available versions using index.tab rather than screen-scraping (#560)

Fixed

  • download errors display informative message, instead of just Invalid version (#482 #492 et al)
  • improve reliability of downloads from custom node mirrors, including removing broken is_oss_ok (#560)
  • restrict downloads to versions with architecture available (#463)

Removed

  • Breaking support for PROJECT_NAME and PROJECT_URL for custom downloads (#342)

5.0.2 (2019-08-02)

Added

  • instructions to bottom of menu version selection

5.0.1 (2019-07-20)

Changed

  • removed reference to prerelease version of v5.0.0 from README

5.0.0 (2019-07-20)

Added

  • log message after install from cache (previously silent)
  • extra logging after install if the active and installed node locations are different
  • support for NO_COLOR and CLICOLOR=0
  • suppress progress and colour if not interactive tty
  • define N_USE_XZ to download .xz compressed archives instead of .gz archives

Changed

  • reinstalling active node version always does reinstall (previously silently did nothing)
  • log message for installing using menu now same format as npm install message
  • updates to GitHub templates and guidelines for contributing et al

4.1.0 (2019-05-10)

Added

  • 'n uninstall` to remove node and npm
  • describe NODE_MIRROR in README

Removed

  • PROJECT_NAME and PROJECT_URL from README. First step to deprecating n project. Open an issue if you still need this!

4.0.0 (2019-05-05)

Only minor functional changes, but technically could break scripts relying on specific behaviour.

Fixed

  • remove trailing space from bin output #456

Added

  • development tests #545

Changed

  • internal: improve shell script based on ShellCheck suggestions, quoting variables use etc #187 #465
  • put single quote marks around parameters to clarify error messages #485
  • update terminology to be more careful with current/latest #522

3.0.2 (2019-04-07)

Added

  • instructions to avoid need for sudo when installing to /usr/local #416 #562

Fixed

  • permission denied errors when running read-only commands without sudo #416

3.0.1 (2019-04-05)

Added

  • install instruction using Homebrew (macOS) #534
  • Table of Contents to README #466

Fixed

  • lts lookup on node mirrors which don't purge old versions (e.g. taobao) #512
  • hide cursor while selecting version from menu #528

Removed

  • gitter badge from README, as gitter chatroom inactive
  • inactive Core Team from README
  • instructions for scripted install of npm from README, which should no longer be needed and not working on Mac #536

3.0.0 (2019-03-29)

Added

Changed

  • allow n rm of active version of node #541 #169 #327 #441
  • show more version examples in README, including partial version number #548
  • updated description of interactive version selection #518
  • make (old) stable an alias for lts #467 #335
  • replace use of which with more standard command -v #532

Fixed

  • error messages when selecting from version menu if active node version not listed #541 #292 #367 #391 #400
  • removed inappropriate shift from prune function #531 #529

Removed

  • Remove old io project support #516 #331