62

I have a problem understanding the use of n. Basically, it is clear that it is a version manager for Node.js such as nvm.

But in contrast to nvm, which is basically a shell script, according to the documentation you are encouraged to use npm to install n:

$ npm install -g n

What I don't get is: For having npm at hand you need to install Node.js. Why would I install Node.js manually to use npm to then be able to install Node.js using n?

To put my question in other words: Why does n suggest installing using npm, if its main purpose is to install Node.js, which includes npm?

mklement0
  • 382,024
  • 64
  • 607
  • 775
Golo Roden
  • 140,679
  • 96
  • 298
  • 425

8 Answers8

47

tl; dr

# Installs n and the latest LTS Node.js version to ~/n.
# For bash, ksh, zsh, modifies the respective user-specific shell-initialization file to
# define env. variable N_PREFIX and append $N_PREFIX/bin to the $PATH.
curl -L https://git.io/n-install | bash  

I feel your pain. Installing Node.js to then install n to then manage Node.js installations is indeed a strange setup.

It would indeed be great to be able to install n by itself first.

I've created a project to support installation of n directly from GitHub; the only prerequisite beyond what n itself needs is git.

Note that you must first remove any pre-existing n / Node.js versions.
The target directory, ~/n by default, must either not yet exist or be empty.
For bash, ksh, and zsh, the relevant shell initialization file (e.g., ~/.bashrc) is automatically modified to define environment variable N_PREFIX and append $N_PREFIX/bin to the $PATH; for other shells, this must be done manually.

Aside from installing n directly from GitHub, it also installs helper scripts for updating n (n-update) and uninstalling it (n-uninstall).

Here are working examples; see the n-install GitHub repo for details:

  • Installation with confirmation prompt to confirm installing to default location $HOME/n and installing the latest LTS Node.js version:

    curl -L https://git.io/n-install | bash
    
  • Automated installation to the default location, with subsequent installation of the latest LTS (long-term support) and latest-overall Node.js versions, as well as the latest 4.1.x Node.js version:

    curl -L https://git.io/n-install | bash -s -- -y lts latest 4.1
    
  • Automated installation to the default location, without subsequent installation of a Node.js version:

    curl -L https://git.io/n-install | bash -s -- -y -
    
  • Automated installation to custom location ~/util/n, with subsequent installation of the latest LTS Node.js version:

    curl -L https://git.io/n-install | N_PREFIX=~/util/n bash -s -- -y
    
mklement0
  • 382,024
  • 64
  • 607
  • 775
25

If you prefer, you can install n from source:

cd /tmp
git clone --depth=1 https://github.com/tj/n
cd n
sudo make install

Then you can install the latest stable version of node as follows:

n stable
user456584
  • 86,427
  • 15
  • 75
  • 107
  • I just want to add that if you get an error during "make install", you may have had installed it before. Then you should call "make uninstall" first. – Nikolay Tsenkov Feb 16 '15 at 16:13
  • 1
    `make install` fails with `cp: cannot create regular file '/usr/local/bin/n': Permission denied`. Maybe update the example to run with sudo? – btx9000 Jun 23 '16 at 09:09
16

The n module was created for convenience.

For example, if you wanted to update your version of Node.js from v0.8.0 to v0.10.20, would you rather download a package, extract and compile? Or would you rather type n 0.10.20 and have it instantly installed, while still retaining previous versions of Node for easy switching?

n suggests using npm to install it because n is a module. That is, npm is the easiest way to install it. Node modules have the functionality of being able to run in a shell when installed globally, so that function was utilized to make switching Node versions much easier.

hexacyanide
  • 88,222
  • 31
  • 159
  • 162
  • 2
    Of course you are right that it's much more convenient to just type `n 0.10.20` and you're done, but why not use n for the initial install? This way, the very first install feels "special". E.g.: Is it guaranteed that n uses the same folders as the Node.js installer does? – Golo Roden Oct 18 '13 at 14:22
  • Because *n* is a module, and using *npm* is simply the easiest way to install it. As for your second question, Node.js binaries are stored in *n*'s own directory, something like `/usr/local/n/versions`, otherwise you wouldn't be able to have multiple versions installed at the same time. – hexacyanide Oct 18 '13 at 14:24
  • But the initial install isn't installed there - isn't that a problem? Please forgive me if this questions may be a little naive, but I just wonder why it is and I'm curious ;-) – Golo Roden Oct 18 '13 at 14:26
  • Why would it be a problem if the initial install isn't there? – hexacyanide Oct 18 '13 at 14:29
  • Maybe I have not yet understood how *n* works, but supposed I switch to another than the initially installed version: I assume that *n* then puts the new version to `/usr/local/bin`, hence it overwrites the previous one. And without installing the initial version *again*, I can not switch back. Am I missing something? – Golo Roden Oct 18 '13 at 14:31
  • 2
    I'm not sure about the initial install, but for most cases `n prev` would just restore the previous version. **Edit:** It appears it also saves the initial install to `/usr/local/n/versions/.prev` when you first add a new version, as it does with any installation. Therefore `n prev` would also work in restoring the initial install. – hexacyanide Oct 18 '13 at 14:38
  • Terminology quibble, because it confused me at first: _n_ (anything installed globally) is a _package_, not a _module_: "Most npm packages are modules, because they are libraries that you load with require. However, there's no requirement that an npm package be a module! Some only contain an executable command-line interface, and don't provide a main field for use in Node programs." - https://www.npmjs.org/doc/misc/npm-faq.html – mklement0 Jun 10 '15 at 13:33
2

You can also install npm separately from Node.JS; e.g.: on a system without Node.JS:

git clone https://github.com/npm/npm
cd npm
./configure
make

Reference: NPM GitHub project

A T
  • 13,008
  • 21
  • 97
  • 158
1

The README for n now has a longer section covering different installation approaches.

Like nvm, n is a bash script. npm is suggested as an easy way to install n if you already have npm, and then you can use n to change the Node.js version. But there are plenty of other approaches for a first install of Node.js. In brief and in no particular order...

You can install n using curl:

curl -L https://raw.githubusercontent.com/tj/n/master/bin/n -o n

Or clone the n repo and install from there:

make install

Or use n-install:

curl -L https://git.io/n-install | bash

Or Homebrew:

brew install n

Or MacPorts:

port install n

(Disclaimer: I am the current maintainer of n.)

shadowspawn
  • 3,039
  • 22
  • 26
0

I had the same question, but have seen the light. 'n' is a handy tool and makes it simple to test different versions of node. Works great on Linux, but no matter how I try to install it on OS X (git clone, then npm install or using user456584's recommended method), when I run it, I always get the same results of "Error: no installed version", even though it installs into

/usr/local/lib/node_modules/n

and

/usr/local/bin/n

Frustrating because I've found this tool to be so handy on Linux.

Crooner
  • 11
  • 2
    :) this could be a comment maybe. – Orkun Jan 08 '15 at 13:52
  • Crooner, @fraxture: If you installed only by cloning `n`'s GitHub repo, then not finding any installed versions is expected: you must explicitly install them; e.g., with `n stable` to install the latest stable Node.js version. With neither `n` nor Node.js installed, you can also try `curl -L http://git.io/n-install | bash`, which includes installing the latest stable Node.js version - see [my answer](http://stackoverflow.com/a/30967671/45375). – mklement0 Jun 21 '15 at 18:09
  • 2
    If it wasn't clear already, the error message is telling you that n has not yet installed any versions of Node, not that n itself hasn't been installed. Obviously, running `n` without having n installed (i.e. on your PATH) would give you a bash/ksh/zsh error message like `-bash: n: command not found` instead of the error message you see. – superEb Jun 26 '15 at 21:38
0

If you have included your default node bin in the $PATH variable like this

export PATH=/usr/local/Cellar/node/11.5.0/bin:$PATH

then n will not be able to active other node versions. Remove this export from the path and then you can manage the currently active node version by n.

r.bhardwaj
  • 1,603
  • 6
  • 28
  • 54
-1

If you are using n then you should use below command

bash$ sudo n latest

Mrinal
  • 125
  • 5