- Notifications
You must be signed in to change notification settings - Fork754
tj/n
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Node.js version management: no subshells, no profile setup, no convoluted API, justsimple.
n
– Interactively Manage Your Node.js Versions
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.
If you already have Node.js installed, an easy way to installn
is usingnpm
:
npm install -g n
The default root location used when runningn
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:
- change the ownership of the relevant directories to yourself (see below)
- tell
n
to use a custom location where you do have write permissions (seeN_PREFIX
) - put
sudo
in front of the command to run it as super user
n
caches Node.js versions in subdirectoryn/versions
. Theactive Node.js version is installed in subdirectoriesbin
,include
,lib
, andshare
.
To take ownership of the system directories (option 1):
# make cache folder (if missing) and take ownershipsudo mkdir -p /usr/local/nsudo 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 folderssudo chown -R $(whoami) /usr/local/bin /usr/local/lib /usr/local/include /usr/local/share
Ifnpm
is not yet available, one way to bootstrap an install is to download and runn
directly. To install thelts
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 ton
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/nn install lts
On macOS withHomebrew you can install then formula.
brew install n
Or on macOS withMacPorts you can install then 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 bothPREFIX
andN_PREFIX
to$HOME/n
, installsn
to$HOME/n/bin
, modifies the initialization files of supported shells to exportN_PREFIX
and add$HOME/n/bin
to thePATH
, and installs the latest LTS Node.js version.
As a result, bothn
itself and all Node.js versions it manages are hosted inside a single, optionally configurable directory, which you can later remove with the includedn-uninstall
script.n-update
updatesn
itself to the latest version. See then-install repo for more details.
Changing from a previous Node.js installed to a different location may involve a few extra steps. See docs forchanging node location for a walk-through example of switching from using Homebrew to usingn
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
Simply executen <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.0n lts
Executen
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.0Use up/down arrow keys to select a version, return key to install, d to delete, q to quit
(You can also usej andk to select next or previous version instead of using arrows, orctrl+n andctrl+p.)
If the active node version does not change after install, try opening a new shell in case seeing a stale version.
There are a variety of ways of specifying the target Node.js version forn
commands. Most commands use the latest matching version, andn ls-remote
lists multiple matching versions.
Numeric version numbers can be complete or incomplete, with an optional leadingv
.
4.9.1
8
: 8.x.y versionsv6.1
: 6.1.x versions
There are labels for two especially useful versions:
lts
: newest Long Term Support official releaselatest
,current
: newest official release
There is anauto
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 ton
..node-version
: version on single line. Used by multiple tools:node-version-usage.nvmrc
: version on single line. Used bynvm
.- if no version file found, look for
engine
as below.
Theengine
label looks for apackage.json
file and reads theengines
field to determine compatible Node.js. Requires an installed version ofjq
ornode
, and usesnpx 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 specifyingother 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
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 usefulto revert to the system version of node (if in a different location), or if you no longerwish to use node and npm, or are switching to a different way of managing them.
n uninstall
There are three commands for working directly with your downloaded versions of Node.js, without reinstalling.
You can show the path to the downloadednode
version:
$ n which 6.14.3/usr/local/n/versions/6.14.3/bin/node
Or run a downloadednode
version with then run
command:
n run 8.11.3 --debug some.js
Or execute a command withPATH
modified sonode
andnpm
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 testn exec lts zsh
A Node.js install normally also includesnpm
,npx
, andcorepack
, but you may wish to preserve your current (especially newer) versions using--preserve
:
$ npm install -g npm@latest...$ npm --version6.13.7# Node.js 8.17.0 includes (older) npm 6.13.4$ n -p 8 installed : v8.17.0$ npm --version6.13.7
You can make this the default by setting the environment variable to a non-empty string. There are separate environment variables fornpm
andcorepack
:
export N_PRESERVE_NPM=1export N_PRESERVE_COREPACK=1
You can be explicit to get the desired behaviour whatever the environment variables:
n --preserve nightlyn --no-preserve latest
Command line help can be obtained fromn --help
.
List matching remote versions available for download:
n ls-remote ltsn ls-remote latestn lsr 10n --all lsr
List downloaded versions in cache:
n ls
Download version into cache:
n download 22
Usen
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
Normallyn run
,n exec
, andn 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
If you would like to use a different Node.js mirror which has the same layout as the defaulthttps://nodejs.org/dist/, you can defineN_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.jsunofficial-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 byn
, such as usingmusl
libc
on Alpine:
export N_NODE_MIRROR=https://unofficial-builds.nodejs.org/download/releaseapk add bash curl libstdc++n --arch x64-musl install lts
If the custom mirror requires authentication you can add theurl-encoded username and password into the URL. e.g.
export N_NODE_MIRROR=https://encoded-username:encoded-password@host:port/path
There is alsoN_NODE_DOWNLOAD_MIRROR
for a different mirror with same layout as the defaulthttps://nodejs.org/download.
By defaultn
picks the binaries matching your system architecture. For example, on a 64 bit systemn
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 currentn --arch x64 current
Then
command downloads and installs to/usr/local
by default, but you may override this location by definingN_PREFIX
.To change the location to say$HOME/.n
, add lines like the following to your shell initialization file:
export N_PREFIX=$HOME/.nexport PATH=$N_PREFIX/bin:$PATH
If you want to store the downloads under a different location, useN_CACHE_PREFIX
. This doesnot affect where the activenode 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 disableexport N_USE_XZ=1 # to enable
You can be explicit to get the desired behaviour whatever the environment variable:
n install --use-xz nightlyn install --no-use-xz latest
In brief:
N_NODE_MIRROR
: SeeCustom sourceN_NODE_DOWNLOAD_MIRROR
: SeeCustom source- support forNO_COLOR andCLICOLOR=0 for controlling use of ANSI color codes
N_MAX_REMOTE_MATCHES
to change the defaultls-remote
maximum of 20 matching versionsN_PRESERVE_NPM
: SeePreserving npmN_PRESERVE_COREPACK
: SeePreserving npm
n
downloads a prebuilt Node.js package and installs to a single prefix (e.g./usr/local
). This overwrites the previous version. Thebin
folder in this location should be in yourPATH
(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 usingn which
andn run
andn exec
.
The globalnpm
packages are not changed by the install, with theexception ofnpm
itself which is part of the Node.js install.
About
Node version management