Install Node.js to install n to install Node.js?

Solution 1:

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
    

Solution 2:

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

Solution 3:

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.

Solution 4:

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