• Stars
    star
    18,395
  • Rank 1,342 (Top 0.03 %)
  • Language
    Shell
  • License
    MIT License
  • Created over 13 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Node version management

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 lts
# If you want n installed, you can use npm now.
npm install -g n

Alternatively, you can clone this repo and

make install

which defaults to /usr/local/bin/n. To install n in a custom location such as $CUSTOM_LOCATION/bin/n, run PREFIX=$CUSTOM_LOCATION make install.

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.

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

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

n --offline 12

Display diagnostics to help resolve problems:

n doctor

Custom Source

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. The most common example is from users in China who can define:

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

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 the currently active node version.

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.

More Repositories

1

commander.js

node.js command-line interfaces made easy
JavaScript
26,053
star
2

git-extras

GIT utilities -- repo summary, repl, changelog population, author commit percentages and more
Shell
16,697
star
3

co

The ultimate generator based flow-control goodness for nodejs (supports thunks, promises, etc)
JavaScript
11,853
star
4

ejs

Embedded JavaScript templates for node
JavaScript
4,450
star
5

node-prune

Remove unnecessary files from node_modules (.md, .ts, ...)
Go
4,297
star
6

consolidate.js

Template engine consolidation library for node.js
JavaScript
3,476
star
7

frontend-boilerplate

webpack-react-redux-babel-autoprefixer-hmr-postcss-css-modules-rucksack-boilerplate (unmaintained, I don't use it anymore)
JavaScript
2,939
star
8

connect-redis

Redis session store for Connect
TypeScript
2,775
star
9

should.js

BDD style assertions for node.js -- test framework agnostic
JavaScript
2,757
star
10

luna

luna programming language - a small, elegant VM implemented in C
C
2,446
star
11

dox

JavaScript documentation generator for node using markdown and jsdoc
JavaScript
2,155
star
12

mmake

Modern Make
Go
1,701
star
13

node-migrate

Abstract migration framework for node
JavaScript
1,522
star
14

terminal-table

Ruby ASCII Table Generator, simple and feature rich.
Ruby
1,504
star
15

axon

message-oriented socket library for node.js heavily inspired by zeromq
JavaScript
1,494
star
16

react-enroute

React router with a small footprint for modern browsers
JavaScript
1,491
star
17

commander

The complete solution for Ruby command-line executables
Ruby
1,090
star
18

mon

mon(1) - Simple single-process process monitoring program written in C
C
1,065
star
19

reds

light-weight, insanely simple full text search module for node.js - backed by Redis
JavaScript
891
star
20

node-thunkify

Turn a regular node function into one which returns a thunk
JavaScript
858
star
21

robo

Simple Go / YAML-based task runner for the team.
Go
781
star
22

react-click-outside

ClickOutside component for React.
JavaScript
775
star
23

gobinaries

Golang binaries compiled on-demand for your system
Go
770
star
24

node-blocked

Check if the event loop is blocked
JavaScript
722
star
25

node-ratelimiter

Abstract rate limiter for nodejs
JavaScript
715
star
26

staticgen

Static website generator that lets you use HTTP servers and frameworks you already know
Go
712
star
27

histo

beautiful charts in the terminal for static or streaming data
C
697
star
28

serve

Simple command-line file / directory server built with connect - supports stylus, jade, etc
JavaScript
563
star
29

styl

Flexible and fast modular CSS preprocessor built on top of Rework
JavaScript
525
star
30

pomo

Ruby Pomodoro app for the command-line (time / task management)
Ruby
524
star
31

go-spin

Terminal spinner package for Golang
Go
521
star
32

mdconf

Markdown driven configuration!
JavaScript
507
star
33

node-growl

growl unobtrusive notification system for nodejs
JavaScript
484
star
34

watch

watch(1) periodically executes the given command - useful for auto-testing, auto-building, auto-anything
C
458
star
35

node-querystring

querystring parser for node and the browser - supporting nesting (used by Express, Connect, etc)
JavaScript
452
star
36

node-delegates

Nodejs method and accessor delegation utility
JavaScript
418
star
37

haml.js

Faster Haml JavaScript implementation for nodejs
JavaScript
409
star
38

triage

Interactive command-line GitHub issue & notification triaging tool.
Go
399
star
39

log.js

super light-weight nodejs logging + streaming log reader
HTML
368
star
40

go-tea

Tea provides an Elm inspired functional framework for interactive command-line programs.
Go
364
star
41

punt

Elegant UDP messaging for nodejs
JavaScript
341
star
42

react-fatigue-dev

Module of modules for making modules
Makefile
312
star
43

php-selector

PHP DOM parser / queries with CSS selectors
PHP
299
star
44

node-gify

Convert videos to gifs using ffmpeg and gifsicle
JavaScript
296
star
45

palette

Node.js image color palette extraction with node-canvas
JavaScript
292
star
46

better-assert

c-style assert() for nodejs, reporting the expression string as the error message
JavaScript
285
star
47

js-yaml

CommonJS YAML Parser -- fast, elegant and tiny yaml parser for javascript
JavaScript
275
star
48

go-naturaldate

Natural date/time parsing for Go.
Go
272
star
49

lingo

Linguistics module for Node - inflection, transformation, i18n and more
JavaScript
271
star
50

react-batch

Batch component for performant frequent updates (flush on count or interval)
JavaScript
251
star
51

sponsors-api

GitHub Sponsor avatar listings in your Readme.md
Go
244
star
52

mad

mad(1) is a markdown manual page viewer
Shell
244
star
53

d3-heatmap

D3 heatmap
JavaScript
243
star
54

go-update

Go package for auto-updating system-specific binaries via GitHub releases.
Go
241
star
55

callsite

node.js access to v8's "raw" CallSites -- useful for custom traces, c-style assertions, getting the line number in execution etc
JavaScript
239
star
56

bm

CLI bookmarks -- dropbox persisted bookmarks in your terminal - view screenshots in your browser
Shell
227
star
57

term-canvas

javascript canvas api for your terminal!
JavaScript
226
star
58

go-termd

Package termd provides terminal markdown rendering, with code block syntax highlighting support.
Go
224
star
59

parse-curl.js

Parse curl commands, returning an object representing the request.
JavaScript
217
star
60

go-progress

Another Go progress bar
Go
216
star
61

es

Go DSL for Elasticsearch queries
Go
206
star
62

co-prompt

sane terminal user-input for node.js using thunks / generators
JavaScript
193
star
63

node-cookie-signature

cookie signing
JavaScript
175
star
64

co-views

Higher-level template rendering for node.js using generators
JavaScript
175
star
65

d3-bar

D3 bar chart
JavaScript
173
star
66

node-only

return whitelisted properties of an object
JavaScript
170
star
67

ngen

nodejs project generator
JavaScript
168
star
68

react-hooks

Fire off actions in stateless components.
JavaScript
167
star
69

letterbox

Go program to batch-process letter-boxing of photographs.
Go
164
star
70

go-search

Search Godoc.org via the command-line.
Go
159
star
71

co-monk

MongoDB generator goodness for node.js
JavaScript
155
star
72

eson

Extended (pluggable) JSON for node - great for configuration files and JSON transformations
JavaScript
150
star
73

growl

Ruby growlnotify 'bindings' (unobtrusive notification system)
Ruby
146
star
74

go

Go packages
Go
140
star
75

d3-series

D3 line series chart used for error reporting on Apex Ping
JavaScript
139
star
76

channel.js

Go-style channel implementation for JavaScript
JavaScript
135
star
77

node-amp

Abstract message protocol for nodejs
JavaScript
135
star
78

burl

better curl(1) through augmentation
Shell
134
star
79

jog

JSON document logging & filtering inspired by loggly for node.js
JavaScript
132
star
80

node-pwd

Hash and compare passwords with pbkdf2
JavaScript
131
star
81

nedis

Redis server implementation written with nodejs
JavaScript
130
star
82

d3-circle

D3 circle chart
JavaScript
130
star
83

d3-dot

D3 dot chart
JavaScript
129
star
84

node-term-list

Interactive terminal list for nodejs
JavaScript
126
star
85

node-comment-macros

JavaScript comment macros useful for injecting logging, tracing, debugging, or stats related code.
JavaScript
126
star
86

d3-line

D3 line chart
JavaScript
125
star
87

asset

little asset manager for lazy people (think bundler/homebrew/npm for assets). written with node
JavaScript
122
star
88

go-dropbox

Dropbox v2 client for Go.
Go
120
star
89

node-term-css

style terminal output using CSS
JavaScript
116
star
90

go-terminput

Package terminput provides terminal keyboard input for interactive command-line tools.
Go
114
star
91

vscode-snippets

Personal VSCode snippets for Go, JS, Elm, etc.
114
star
92

nshell

scriptable command-line shell written with node.js
JavaScript
109
star
93

node-actorify

Turn any node.js duplex stream into an actor
JavaScript
108
star
94

co-parallel

Execute thunks in parallel with concurrency support
JavaScript
108
star
95

node-monquery

mongodb query language for humans
JavaScript
106
star
96

co-fs

nodejs core fs module thunk wrappers for "co"
JavaScript
105
star
97

axon-rpc

Axon RPC client / server
JavaScript
103
star
98

s3.js

S3 uploads from the browser.
JavaScript
100
star
99

spa

Tiny Single Page Application server for Go with `spa` command-line tool.
Go
94
star
100

d3-tipy

D3 tooltip
JavaScript
94
star