2016-06-16 02:17:38 +02:00
# `n` – Interactively Manage Your Node.js Versions
2011-01-05 16:01:29 +01:00
2015-12-27 05:29:15 +01:00
[![npm ](https://img.shields.io/npm/dt/n.svg?style=flat-square )](https://www.npmjs.com/package/n)
[![npm ](https://img.shields.io/npm/dm/n.svg?style=flat-square )](https://www.npmjs.com/package/n)
[![npm ](https://img.shields.io/npm/v/n.svg?style=flat-square )](https://www.npmjs.com/package/n)
[![npm ](https://img.shields.io/npm/l/n.svg?style=flat-square )](https://www.npmjs.com/package/n)
2015-05-08 15:55:59 +02:00
2016-06-16 02:17:38 +02:00
Node.js version management: no subshells, no profile setup, no convoluted API, just **simple** .
2011-01-05 16:01:29 +01:00
2020-07-15 10:39:09 +02:00
![usage animation ](https://nimit.io/images/n/n.gif )
2013-08-28 05:08:43 +02:00
2020-07-04 05:24:14 +02:00
- [`n` – Interactively Manage Your Node.js Versions ](#n--interactively-manage-your-nodejs-versions )
2021-06-06 09:49:50 +02:00
- [Supported Platforms ](#supported-platforms )
2019-08-03 09:54:26 +02:00
- [Installation ](#installation )
- [Third Party Installers ](#third-party-installers )
2024-05-11 06:18:53 +02:00
- [Replacing a previous node install ](#replacing-a-previous-node-install )
2021-03-12 07:13:26 +01:00
- [Installing Node.js Versions ](#installing-nodejs-versions )
- [Specifying Node.js Versions ](#specifying-nodejs-versions )
2019-08-03 09:54:26 +02:00
- [Removing Versions ](#removing-versions )
2021-03-12 07:13:26 +01:00
- [Using Downloaded Node.js Versions Without Reinstalling ](#using-downloaded-nodejs-versions-without-reinstalling )
2020-02-24 09:50:23 +01:00
- [Preserving npm ](#preserving-npm )
2019-08-03 09:54:26 +02:00
- [Miscellaneous ](#miscellaneous )
2024-11-08 23:57:34 +01:00
- [Custom Mirror ](#custom-mirror )
2019-08-03 09:54:26 +02:00
- [Custom Architecture ](#custom-architecture )
- [Optional Environment Variables ](#optional-environment-variables )
2019-11-23 05:54:41 +01:00
- [How It Works ](#how-it-works )
2017-10-23 07:26:37 +02:00
2021-06-06 09:49:50 +02:00
## 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.
2011-01-05 16:01:29 +01:00
## Installation
2021-09-05 07:03:29 +02:00
If you already have Node.js installed, an easy way to install `n` is using `npm` :
2015-07-20 15:43:48 +02:00
2019-04-07 07:23:31 +02:00
npm install -g n
2011-01-05 16:01:29 +01:00
2024-01-19 21:56:52 +01:00
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
2021-09-25 11:35:03 +02:00
`n` caches Node.js versions in subdirectory `n/versions` . The _active_ Node.js version is installed in subdirectories `bin` , `include` , `lib` , and `share` .
2014-02-11 17:30:08 +01:00
2024-01-19 21:56:52 +01:00
To take ownership of the system directories (option 1):
2019-04-07 06:54:02 +02:00
2019-08-03 09:54:26 +02:00
# make cache folder (if missing) and take ownership
sudo mkdir -p /usr/local/n
sudo chown -R $(whoami) /usr/local/n
2021-07-24 23:32:12 +02:00
# 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
2021-03-12 07:13:26 +01:00
# take ownership of Node.js install destination folders
2019-08-03 09:54:26 +02:00
sudo chown -R $(whoami) /usr/local/bin /usr/local/lib /usr/local/include /usr/local/share
2019-04-07 06:54:02 +02:00
2019-11-15 22:09:01 +01:00
-----
2022-12-29 23:19:13 +01:00
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:
2019-11-15 22:09:01 +01:00
2024-10-20 00:05:12 +02:00
curl -fsSL https://raw.githubusercontent.com/tj/n/master/bin/n | bash -s install lts
2022-12-29 23:19:13 +01:00
# If you want n installed, you can use npm now.
2021-09-05 07:03:29 +02:00
npm install -g n
2019-11-15 22:09:01 +01:00
2024-11-08 23:43:28 +01:00
If you don't need support for updates to `n` itself you can just save the download:
2019-11-15 22:09:01 +01:00
2024-11-08 23:43:28 +01:00
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
2019-11-15 22:09:01 +01:00
2019-03-31 11:00:09 +02:00
### Third Party Installers
2023-08-16 08:14:44 +02:00
On macOS with [Homebrew ](https://brew.sh/ ) you can install the [n formula ](https://github.com/Homebrew/homebrew-core/blob/master/Formula/n/n.rb ).
2018-10-28 09:48:56 +01:00
brew install n
2021-09-25 11:35:03 +02:00
2021-01-27 10:19:52 +01:00
Or on macOS with [MacPorts ](https://www.macports.org/ ) you can install the [n port ](https://ports.macports.org/port/n/summary ):
port install n
2018-10-28 09:48:56 +01:00
2019-03-31 11:00:09 +02:00
On Linux and macOS, [n-install ](https://github.com/mklement0/n-install ) allows installation directly from GitHub; for instance:
2015-07-20 15:43:48 +02:00
2022-04-27 04:51:31 +02:00
curl -L https://bit.ly/n-install | bash
2015-07-20 15:43:48 +02:00
2021-03-12 07:13:26 +01:00
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.
2016-06-16 02:17:38 +02:00
2021-03-12 07:13:26 +01:00
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 ](https://github.com/mklement0/n-install ) for more details.
2011-01-05 16:48:51 +01:00
2024-05-11 06:18:53 +02:00
### Replacing a previous node install
2024-05-29 22:50:34 +02:00
Changing from a previous Node.js installed to a different location may involve a few extra steps. See docs for [changing node location ](./docs/changing-node-location.md ) for a walk-through example of switching from using Homebrew to using `n` to manage Node.js.
2024-05-11 06:18:53 +02:00
You have a problem with multiple versions if after installing node you see the "installed" and "active" locations are different:
2024-10-27 22:05:44 +01:00
2024-05-11 06:18:53 +02:00
```console
% 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
```
2021-03-12 07:13:26 +01:00
## Installing Node.js Versions
2016-06-16 02:17:38 +02:00
2021-03-12 07:13:26 +01:00
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.
2011-01-05 16:48:51 +01:00
2019-08-03 09:54:26 +02:00
n 10.16.0
n lts
2011-01-05 16:48:51 +01:00
2019-08-03 09:54:26 +02:00
Execute `n` on its own to view your downloaded versions, and install the selected version.
2011-01-05 16:48:51 +01:00
$ n
2011-10-26 22:59:40 +02:00
2019-01-13 03:35:56 +01:00
node/4.9.1
ο node/8.11.3
node/10.15.0
2011-01-05 16:48:51 +01:00
2019-10-23 12:04:32 +02:00
Use up/down arrow keys to select a version, return key to install, d to delete, q to quit
2011-01-22 00:25:07 +01:00
2021-05-03 10:35:04 +02:00
(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 > .)
2012-01-25 12:42:27 +01:00
2019-08-03 09:54:26 +02:00
If the active node version does not change after install, try opening a new shell in case seeing a stale version.
2015-11-05 04:08:42 +01:00
2021-03-12 07:13:26 +01:00
## Specifying Node.js Versions
2019-08-03 09:54:26 +02:00
2021-03-12 07:13:26 +01:00
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.
2019-08-03 09:54:26 +02:00
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:
2015-11-05 04:08:42 +01:00
2019-08-03 09:54:26 +02:00
- `lts` : newest Long Term Support official release
- `latest` , `current` : newest official release
2020-04-11 04:10:29 +02:00
2020-07-25 11:59:32 +02:00
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:
2020-04-11 04:10:29 +02:00
2020-07-25 11:59:32 +02:00
- `.n-node-version` : version on single line. Custom to `n` .
- `.node-version` : version on single line. Used by multiple tools: [node-version-usage ](https://github.com/shadowspawn/node-version-usage )
- `.nvmrc` : version on single line. Used by `nvm` .
2020-12-20 04:17:18 +01:00
- if no version file found, look for `engine` as below.
2024-08-16 06:37:02 +02:00
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.
2019-08-03 09:54:26 +02:00
2020-06-24 11:01:53 +02:00
There is support for the named release streams:
2019-08-03 09:54:26 +02:00
- `argon` , `boron` , `carbon` : codenames for LTS release streams
2021-03-12 07:13:26 +01:00
These Node.js support aliases may be used, although simply resolve to the latest matching version:
2020-06-24 11:01:53 +02:00
- `active` , `lts_active` , `lts_latest` , `lts` , `current` , `supported`
2020-07-04 05:24:14 +02:00
The last version form is for specifying [other releases ](https://nodejs.org/download ) available using the name of the remote download folder optionally followed by the complete or incomplete version.
2019-08-03 09:54:26 +02:00
- `nightly`
- `test/v11.0.0-test20180528`
- `rc/10`
2019-06-03 11:27:18 +02:00
2019-03-31 11:00:09 +02:00
## Removing Versions
2011-01-05 16:55:56 +01:00
2019-05-09 12:22:17 +02:00
Remove some cached versions:
2011-01-05 17:17:16 +01:00
2019-04-07 07:23:31 +02:00
n rm 0.9.4 v0.10.0
2011-01-05 17:23:02 +01:00
2021-06-05 05:36:49 +02:00
Removing all cached versions except the installed version:
2017-04-12 15:10:25 +02:00
2019-04-07 07:23:31 +02:00
n prune
2017-04-12 15:10:25 +02:00
2021-06-05 05:36:49 +02:00
Remove the installed Node.js (does not affect the cached versions). This can be useful
2019-05-09 12:22:17 +02:00
to revert to the system version of node (if in a different location), or if you no longer
2019-05-10 10:20:34 +02:00
wish to use node and npm, or are switching to a different way of managing them.
2019-05-09 12:22:17 +02:00
n uninstall
2021-03-12 07:13:26 +01:00
## Using Downloaded Node.js Versions Without Reinstalling
2011-01-05 17:43:16 +01:00
2021-03-12 07:13:26 +01:00
There are three commands for working directly with your downloaded versions of Node.js, without reinstalling.
2011-01-05 17:43:16 +01:00
2021-03-12 07:13:26 +01:00
You can show the path to the downloaded `node` version:
2011-01-05 16:01:29 +01:00
2019-08-03 09:54:26 +02:00
$ n which 6.14.3
/usr/local/n/versions/6.14.3/bin/node
2011-01-05 16:01:29 +01:00
2019-08-03 09:54:26 +02:00
Or run a downloaded `node` version with the `n run` command:
2011-01-05 17:17:16 +01:00
2019-08-03 09:54:26 +02:00
n run 8.11.3 --debug some.js
2011-01-05 16:01:29 +01:00
2021-03-12 07:13:26 +01:00
Or execute a command with `PATH` modified so `node` and `npm` will be from the downloaded Node.js version.
2021-03-12 07:41:44 +01:00
(NB: `npm` run this way will be using global node_modules from the target node version folder.)
2011-01-05 16:01:29 +01:00
2019-08-03 09:54:26 +02:00
n exec 10 my-script --fast test
2021-03-12 07:41:44 +01:00
n exec lts zsh
2015-05-07 16:40:23 +02:00
2020-02-24 09:50:23 +01:00
## Preserving npm
2022-07-16 05:00:35 +02:00
A Node.js install normally also includes `npm` , `npx` , and `corepack` , but you may wish to preserve your current (especially newer) versions using `--preserve` :
2020-02-24 09:50:23 +01:00
$ npm install -g npm@latest
...
$ npm --version
6.13.7
2022-07-16 05:00:35 +02:00
# Node.js 8.17.0 includes (older) npm 6.13.4
2020-02-24 09:50:23 +01:00
$ n -p 8
installed : v8.17.0
$ npm --version
6.13.7
2022-07-16 05:00:35 +02:00
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` :
2020-02-24 09:50:23 +01:00
export N_PRESERVE_NPM=1
2022-07-16 05:00:35 +02:00
export N_PRESERVE_COREPACK=1
2020-02-24 09:50:23 +01:00
2022-07-16 05:00:35 +02:00
You can be explicit to get the desired behaviour whatever the environment variables:
2020-02-24 09:50:23 +01:00
n --preserve nightly
n --no-preserve latest
2019-08-03 09:54:26 +02:00
## Miscellaneous
2011-01-05 16:01:29 +01:00
2019-08-03 09:54:26 +02:00
Command line help can be obtained from `n --help` .
2013-01-18 16:41:51 +01:00
2019-08-03 09:54:26 +02:00
List matching remote versions available for download:
2013-01-18 16:41:51 +01:00
2019-08-03 09:54:26 +02:00
n ls-remote lts
n ls-remote latest
n lsr 10
n --all lsr
2013-01-18 16:41:51 +01:00
2019-08-03 09:54:26 +02:00
List downloaded versions in cache:
2013-01-18 16:41:51 +01:00
2019-08-11 22:23:28 +02:00
n ls
2013-01-18 16:41:51 +01:00
2024-11-08 23:42:51 +01:00
Download version into cache:
n download 22
2023-11-27 08:52:40 +01:00
Use `n` to access cached versions (already downloaded) without internet available.
n --offline 12
2024-10-20 00:05:12 +02:00
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
2024-10-28 02:54:27 +01:00
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:
2024-10-20 00:05:12 +02:00
n --download run 18.3 my-script.js
2019-08-03 09:54:26 +02:00
Display diagnostics to help resolve problems:
2013-01-18 16:41:51 +01:00
2019-08-11 22:23:28 +02:00
n doctor
2011-01-22 00:25:07 +01:00
2024-11-08 23:57:34 +01:00
## Custom Mirror
2015-08-28 23:18:25 +02:00
2021-03-12 07:13:26 +01:00
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` .
2024-11-08 23:57:34 +01:00
One example is for users in China who can define:
2015-08-28 23:18:25 +02:00
2022-01-14 21:44:20 +01:00
export N_NODE_MIRROR=https://npmmirror.com/mirrors/node
2019-08-03 09:54:26 +02:00
2024-11-08 23:57:34 +01:00
Another example is the Node.js [unofficial-builds project ](https://github.com/nodejs/unofficial-builds/ ) 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
2021-04-25 01:26:46 +02:00
If the custom mirror requires authentication you can add the [url-encoded ](https://urlencode.org ) username and password into the URL. e.g.
2021-04-25 00:09:17 +02:00
2021-04-25 01:26:46 +02:00
export N_NODE_MIRROR=https://encoded-username:encoded-password@host:port/path
2021-04-25 00:09:17 +02:00
There is also `N_NODE_DOWNLOAD_MIRROR` for a different mirror with same layout as the default < https: // nodejs . org / download > .
2015-08-28 23:18:25 +02:00
2019-06-02 10:15:50 +02:00
## Custom Architecture
2015-08-30 12:27:35 +02:00
2021-09-25 11:35:03 +02:00
By default `n` picks the binaries matching your system architecture. For example, on a 64 bit system `n` will download 64 bit binaries.
2015-08-30 12:27:35 +02:00
2021-04-19 10:21:53 +02:00
On a Mac with Apple silicon:
2021-09-25 11:35:03 +02:00
2021-04-19 10:21:53 +02:00
- 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
2015-08-30 12:27:35 +02:00
2021-04-19 10:21:53 +02:00
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
2015-08-30 12:27:35 +02:00
2019-06-02 10:15:50 +02:00
## Optional Environment Variables
2016-06-16 02:17:38 +02:00
2019-06-02 10:15:50 +02:00
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:
2016-06-16 02:17:38 +02:00
2019-06-02 10:15:50 +02:00
export N_PREFIX=$HOME/.n
export PATH=$N_PREFIX/bin:$PATH
2016-06-16 02:17:38 +02:00
2024-10-27 22:05:44 +01:00
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.
2022-03-18 05:59:09 +01:00
2021-03-12 07:13:26 +01:00
`n` defaults to using xz compressed Node.js tarballs for the download if it is likely tar on the system supports xz decompression.
2020-01-29 09:42:31 +01:00
You can override the automatic choice by setting an environment variable to zero or non-zero:
2019-07-20 03:17:16 +02:00
2020-01-29 09:42:31 +01:00
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
2019-07-20 03:17:16 +02:00
2019-06-02 10:15:50 +02:00
In brief:
2019-07-20 03:17:16 +02:00
2019-08-03 09:54:26 +02:00
- `N_NODE_MIRROR` : See [Custom source ](#custom-source )
- `N_NODE_DOWNLOAD_MIRROR` : See [Custom source ](#custom-source )
2020-07-15 10:39:09 +02:00
- support for [NO_COLOR ](https://no-color.org ) and [CLICOLOR=0 ](https://bixense.com/clicolors ) for controlling use of ANSI color codes
2019-08-03 09:54:26 +02:00
- `N_MAX_REMOTE_MATCHES` to change the default `ls-remote` maximum of 20 matching versions
2020-02-24 09:50:23 +01:00
- `N_PRESERVE_NPM` : See [Preserving npm ](#preserving-npm )
2022-07-16 05:00:35 +02:00
- `N_PRESERVE_COREPACK` : See [Preserving npm ](#preserving-npm )
2019-11-23 05:54:41 +01:00
## How It Works
2021-03-12 07:13:26 +01:00
`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` ).
2019-11-23 05:54:41 +01:00
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
2021-03-12 07:13:26 +01:00
exception of `npm` itself which is part of the Node.js install.