n Node Version Manager Deployment and Usage Guide
This guide provides comprehensive instructions for deploying and using n, an interactive Node.js version manager.
Prerequisites
- Operating System: macOS, Linux (including Windows Subsystem for Linux), or other Unix-like systems.
- Shell: BASH (though
ndoes not require you to use BASH as your command shell). - Node.js (Optional): An existing Node.js installation (e.g., via
npm) can simplifyn's initial installation. curl: For downloadingndirectly.npm(Optional): For installingnas a global package.sudo(Optional): May be required for managing system-wide Node.js installations or ownership of directories.- Homebrew (macOS, Optional): For installing
nvia Homebrew. - MacPorts (macOS, Optional): For installing
nvia MacPorts.
Installation
n can be installed in several ways, depending on your current setup and preferences.
Via npm (Recommended if Node.js is already installed)
If you have an existing Node.js installation with npm, this is the simplest method:
npm install -g n
Addressing Permissions Issues:
By default, n uses /usr/local as its root, which often requires root permissions. You have three options:
-
Change Directory Ownership (Recommended for system-wide control): This grants your user ownership of the necessary directories, allowing
nto manage Node.js versions withoutsudofor subsequent operations.# Create n cache folder and take ownership sudo mkdir -p /usr/local/n sudo chown -R $(whoami) /usr/local/n # Ensure Node.js installation destination folders exist sudo mkdir -p /usr/local/bin /usr/local/lib /usr/local/include /usr/local/share # Take ownership of Node.js installation destination folders sudo chown -R $(whoami) /usr/local/bin /usr/local/lib /usr/local/include /usr/local/share -
Use a Custom Location (
N_PREFIX): Set theN_PREFIXenvironment variable to a directory where your user has write permissions (e.g., your home directory). See the Configuration section for details. -
Use
sudoforncommands: Prefixncommands withsudo(e.g.,sudo n install lts). This is generally less convenient and not recommended for regular use.
Direct Download
If npm is not available, you can download and install n directly.
To install n and an LTS Node.js version:
curl -fsSL https://raw.githubusercontent.com/tj/n/master/bin/n | bash -s install lts
# You can now install n globally via npm if desired:
npm install -g n
To download n as a standalone executable:
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 # Install an LTS Node.js version
Third-Party Installers
-
macOS with Homebrew:
brew install n -
macOS with MacPorts:
port install n -
n-install (Linux and macOS):
n-installplacesnand all managed Node.js versions within$HOME/nand configures your shell.curl -L https://bit.ly/n-install | bashAfter installation,
n-installprovidesn-uninstallandn-updatescripts.
Replacing a Previous Node.js Installation
If you're switching from another Node.js manager (e.g., Homebrew's Node.js), you might encounter conflicts. n will show different "installed" and "active" locations.
% 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
To resolve this, ensure your PATH environment variable prioritizes the n managed Node.js path (typically /usr/local/bin if using default N_PREFIX) over other Node.js installations. You might need to uninstall previous Node.js versions managed by other tools. Refer to n's documentation on changing node location for detailed steps.
Configuration
n uses environment variables for custom configurations.
-
N_PREFIX: Specifies the root directory whereninstalls Node.js versions and stores its cache. Default:/usr/localExample: To install Node.js versions in your home directory:export N_PREFIX="$HOME/n" # Add N_PREFIX/bin to your PATH export PATH="$N_PREFIX/bin:$PATH"It's recommended to add these
exportcommands to your shell's configuration file (e.g.,.bashrc,.zshrc,.profile). -
N_NODE_MIRROR: Overrides the default Node.js download mirror. Useful for custom mirrors or when experiencing slow downloads from the default. Default:https://nodejs.org/dist/Example:export N_NODE_MIRROR="https://npmmirror.com/mirrors/node/" -
N_NODE_ARCH: Specifies the architecture for Node.js binaries to download. Useful for cross-compilation or specific system requirements (e.g., ARM on an x86 machine). Default: Automatically detected. Example:export N_NODE_ARCH="arm64" # For ARM 64-bit architecture -
N_npm_GLOBAL: Controlsnpmpreservation behavior. See Preserving npm section. Default:true(preservesnpm) Example:export N_npm_GLOBAL="false" # Do not preserve npm
Build & Run
n is a shell script and does not require a "build" step. You "run" it by executing commands.
Installing Node.js Versions
-
Install a specific version:
n 10.16.0 n 18.17.1 -
Install the latest LTS (Long Term Support) version:
n lts -
Install the latest stable version:
n latest -
Install the latest version from a major release:
n 16 # Installs the latest 16.x.x version -
Install a specific version from a remote URL:
n https://nodejs.org/dist/v14.17.0/node-v14.17.0-linux-x64.tar.xz
Interactive Version Selection
Run n without arguments to enter an interactive selection menu:
n
Use the up/down arrow keys (or j/k, ctrl+n/ctrl+p) to navigate.
- Press
Enterto install the selected version. - Press
dto delete the selected version from the cache. - Press
qto quit.
Using Downloaded Node.js Versions Without Reinstalling
n caches downloaded versions in N_PREFIX/n/versions. If a version is already downloaded, n will install it from the cache.
Removing Versions
-
Remove a specific version:
n rm 10.16.0 -
Remove all cached versions (except the currently active one):
n prune
Preserving npm
By default, n attempts to preserve your global npm packages when switching Node.js versions. If N_npm_GLOBAL is set to false, npm will not be preserved.
To explicitly disable npm preservation:
export N_npm_GLOBAL="false"
n lts # npm will not be preserved during this operation
Miscellaneous Commands
-
List installed versions:
n ls -
List remote versions available for download:
n ls-remote -
Display the path to the currently active Node.js binary:
n bin -
Run a command with a specific Node.js version:
n use 14.17.0 my-script.js n exec 16.x npm test -
Clean the cache (removes all downloaded Node.js tarballs):
n cache clean
Deployment
n is a local development tool for managing Node.js versions. It is not designed for "deployment" in the traditional sense of deploying an application to a server. Its purpose is to manage the Node.js runtime environment on a development machine or CI/CD runner.
However, you can incorporate n into your CI/CD pipelines or server setup scripts to ensure a specific Node.js version is used.
Example CI/CD Integration (e.g., GitHub Actions, GitLab CI):
# .github/workflows/ci.yml
name: CI
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Node.js with n
run: |
curl -fsSL https://raw.githubusercontent.com/tj/n/master/bin/n | bash -s install 18.17.1
# Ensure n's installed node is in PATH
echo "/usr/local/bin" >> $GITHUB_PATH
node -v # Verify the version
npm -v
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm test
Example Server Setup Script:
#!/bin/bash
# Install n if not present
if ! command -v n &> /dev/null
then
echo "n not found, installing..."
curl -fsSL -o /usr/local/bin/n https://raw.githubusercontent.com/tj/n/master/bin/n
chmod 0755 /usr/local/bin/n
fi
# Ensure n's default install location is owned by the current user
sudo mkdir -p /usr/local/n
sudo chown -R $(whoami) /usr/local/n
sudo mkdir -p /usr/local/bin /usr/local/lib /usr/local/include /usr/local/share
sudo chown -R $(whoami) /usr/local/bin /usr/local/lib /usr/local/include /usr/local/share
# Install the desired Node.js version
n install lts # Or n install 20.12.2
# Verify installation
node -v
npm -v
echo "Node.js environment configured."
Troubleshooting
-
"Permission denied" errors during installation or version switching:
- Cause:
nis trying to write to system directories (e.g.,/usr/local) without sufficient permissions. - Solution:
- Change directory ownership: Follow the
sudo chowncommands in the Installation section to grant your user ownership of/usr/local/n,/usr/local/bin, etc. - Use
N_PREFIX: Set theN_PREFIXenvironment variable to a directory where your user has write permissions (e.g.,$HOME/.n). Remember to add$N_PREFIX/binto yourPATH. - Use
sudo: As a last resort, prefixncommands withsudo(e.g.,sudo n install lts).
- Change directory ownership: Follow the
- Cause:
-
Node.js version doesn't change after installation:
- Cause: Your shell's
PATHenvironment variable might be pointing to a different Node.js installation (e.g., one installed by Homebrew or a system package manager) that takes precedence over then-managed one. Or, you might need to restart your shell. - Solution:
- Restart your shell: Close and reopen your terminal.
- Check
PATH: Runecho $PATH. Ensure that the directory whereninstalls Node.js (typically/usr/local/binor$N_PREFIX/bin) appears before any other Node.js installation paths. - Identify conflicting installations: Run
which nodeandwhich npmto see where your shell is finding these executables. You may need to uninstall conflicting Node.js versions or adjust yourPATHpermanently in your shell configuration file (.bashrc,.zshrc,.profile). - Verify
n's active version: Runn lsand check theonext to the active version. Then runn binto see the pathnexpects.
- Cause: Your shell's
-
ncommand not found:- Cause: The directory where
nwas installed (e.g.,/usr/local/binor$HOME/n/bin) is not in yourPATH. - Solution:
- Verify
n's location: Check ifnexists in/usr/local/binor your customN_PREFIX/bin. - Add to
PATH: Add the directory containing thenexecutable to yourPATHenvironment variable in your shell's configuration file. Example:export PATH="/usr/local/bin:$PATH"orexport PATH="$HOME/n/bin:$PATH".
- Verify
- Cause: The directory where
-
ndoes not work on Windows (PowerShell, Git Bash, Cygwin):- Cause:
nis a BASH script and is not designed to run in native Windows shells or environments that emulate Unix with a compatibility layer like Cygwin. - Solution: Use
nwithin Windows Subsystem for Linux (WSL) on Windows, where it is fully supported.
- Cause:
-
Slow download speeds for Node.js versions:
- Cause: Geographic distance to the default Node.js mirror.
- Solution: Set the
N_NODE_MIRRORenvironment variable to a closer or faster mirror. See the Configuration section.
-
npmglobal packages are lost after switching Node.js versions:- Cause:
n'snpmpreservation mechanism might be disabled or encountering an issue. - Solution:
- Ensure
N_npm_GLOBALis not explicitly set tofalse. - Manually reinstall global packages after switching if preservation fails.
- Ensure
- Cause: