n Documentation

Shell repository by @tj. Docker Compose & container deployment with 20k stars.

19,522 stars Shell Shell 1 file ยท ~4,469 tokens
RAW Doc

Docker & Self-Hosted Deployment Verified OCI

Deploy n on any server, homelab, or cloud container platform.

1-Click Terminal Command
bash (docker run)
docker run -d --name n --restart always tj/n:latest
Configured Services & Ports
โš™๏ธ ubuntu-curl
image: build locally
โš™๏ธ ubuntu-wget
image: build locally
โš™๏ธ fedora-curl
image: build locally
docker-compose.yml
yaml (docker-compose.yml)
services:
  ubuntu-curl:
    extends:
      file: ./docker-base.yml
      service: testbed
    build:
      context: dockerfiles
      dockerfile: Dockerfile-ubuntu-curl
  ubuntu-wget:
    extends:
      file: ./docker-base.yml
      service: testbed
    build:
      context: dockerfiles
      dockerfile: Dockerfile-ubuntu-wget
  fedora-curl:
    extends:
      file: ./docker-base.yml
      service: testbed
    build:
      context: dockerfiles
      dockerfile: Dockerfile-fedora-curl

File: README.md

`n` โ€“ Interactively Manage Your Node.js Versions




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

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:

text
npm install 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):

text
# 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:

text
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 n

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

text
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.

text
brew install n

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

text
port install n

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

text
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:

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

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.

text
n 10.16.0
n lts

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

text
$ 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 j and k to select next or previous version instead of using arrows, or ctrl+n and ctrl+p.)

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:

text
n rm 0.9.4 v0.10.0

Removing all cached versions except the installed version:

text
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.

text
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:

text
$ 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:

text
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.)

text
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:

text
$ npm install 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:

text
export N_PRESERVE_NPM=1
export N_PRESERVE_COREPACK=1

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

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

Miscellaneous

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

List matching remote versions available for download:

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

List downloaded versions in cache:

text
n ls

Download version into cache:

text
n download 22

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

text
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.

text
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:

text
n --download run 18.3 my-script.js

Display diagnostics to help resolve problems:

text
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:

text
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.

text
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. You can do that with N_ARCH or --arch:

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

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

text
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, or set N_ARCH environment variable.

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

text
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:

text
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:

text
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:

text
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.


File: docs/changing-node-location.md

Switching To `n` Managed Node.js

If you already have Node.js installed to a different root than n uses, you can easily end up with multiple copies of node (and npm, and npx, and globally installed packages!). Some common situations are you already had Node.js installed using your Linux package manager, or using another node version manager, or using say Homebrew. The two main ways you might resolve this are:

  • uninstall from the old directory and reinstall to the new directory
  • put the bin directory that n uses early in the PATH environment variable, so the n installed node is found first

The simplest setup to understand is the first one. Just have one version of node installed.

Let's walk-through the process of switching over from using Homebrew as an example. Let's start off with Node.js installed, npm updated, and an example global npm package. The key point is there are two install prefixes involved:

  • old: /opt/homebrew
  • new: /usr/local
console
% brew install node
% npm install npm@latest
% npm install @shadowspawn/forest-arborist
% brew list node
/opt/homebrew/Cellar/node/21.7.3/bin/node
/opt/homebrew/Cellar/node/21.7.3/bin/npm
/opt/homebrew/Cellar/node/21.7.3/bin/npx
/opt/homebrew/Cellar/node/21.7.3/etc/bash_completion.d/npm
/opt/homebrew/Cellar/node/21.7.3/include/node/ (107 files)
/opt/homebrew/Cellar/node/21.7.3/libexec/bin/ (2 files)
/opt/homebrew/Cellar/node/21.7.3/libexec/lib/ (2012 files)
/opt/homebrew/Cellar/node/21.7.3/share/doc/ (2 files)
/opt/homebrew/Cellar/node/21.7.3/share/man/man1/node.1
% command -v node
/opt/homebrew/bin/node
% command -v npm 
/opt/homebrew/bin/npm
% npm prefix --global
/opt/homebrew

Before we start transferring, list the global npm packages in the "old" location. We will refer back to this list.

console
% npm list --global
/opt/homebrew/lib
โ”œโ”€โ”€ @shadowspawn/[email protected]
โ””โ”€โ”€ [email protected]

We could clean out the old location first, but let's install n and another copy of node and see what that looks like. We end up with two versions of node, and the active one is still the Homebrew managed version.

console
% brew install n
% n lts
  installing : node-v20.12.2
       mkdir : /usr/local/n/versions/node/20.12.2
       fetch : https://nodejs.org/dist/v20.12.2/node-v20.12.2-darwin-arm64.tar.xz
     copying : node/20.12.2
   installed : v20.12.2 to /usr/local/bin/node
      active : v21.7.3 at /opt/homebrew/bin/node
% command -v node
/opt/homebrew/bin/node
% which -a node
/opt/homebrew/bin/node
/usr/local/bin/node
% command -v npm
/opt/homebrew/bin/npm
% command -v npx
/opt/homebrew/bin/npx
% n doctor
<...>

CHECKS

Checking n install destination is in PATH...
good

Checking n install destination priority in PATH...
โš ๏ธ There is a version of node installed which will be found in PATH before the n installed version.

Checking npm install destination...
โš ๏ธ There is an active version of npm shadowing the version installed by n. Check order of entries in PATH.
   installed : /usr/local/bin/npm
      active : /opt/homebrew/bin/npm

<...>

Now let's switch over. Delete everything from the old location. Delete all the global npm packages except npm itself, then delete npm, then delete node.

console
npm uninstall --global @shadowspawn/forest-arborist

npm uninstall --global npm

brew uninstall node

Check the active binaries are now the ones installed by n:

console
% command -v node
/usr/local/bin/node
% command -v npm 
/usr/local/bin/npm
% command -v npx
/usr/local/bin/npx

And lastly, reinstall the global npm packages you started with:

text
% npm prefix --global
/usr/local
% npm install npm@latest
% npm install @shadowspawn/forest-arborist
% npm list -g
/usr/local/lib
โ”œโ”€โ”€ @shadowspawn/[email protected]
โ””โ”€โ”€ [email protected]