Node.js installation and npm prefix

Some things regarding Node.js installation and setup:

When installing Node.js with the installer, it will install the npm inside the Node.js location. So if you've set another prefix, you have to delete the npm packages from within the Node.js location, so they won't shadow the one in the prefix location.

The npm prefix variable is the location where globale packages will be installed. If npm prefix points to your home directory, the node modules are installed to ~/lib/node_modules. To get the prefix:

npm config get prefix

To set it:

npm config set prefix ~

Update: on MacOS you can also let your new npm shadow the npm installed by nodejs. For this to work you have to make sure the directory of the new npm comes before the one of the other one in your $PATH. We will then use the old npm only to install the new npm.

  1. Install node with the default installer. Node and npm are installed into /usr/local.
  2. Set the prefix: npm config set prefix ~/node_packages (this will make npm install all global packages to ~/node_packages/lib and ~/node_packages/bin).
  3. Now install a global npm: npm install -g npm (this will install npm into ~/node_packages).
  4. Because ~/node_packages/bin comes before /usr/local/bin, the system will now use the new npm. See npm --version to verify it.
  5. from now on you can upgrade npm easily with npm install -g npm.
  6. If you upgrade nodejs you can do so by running the installer. It will also upgrade npm in /usr/local, but this has no side effects.
Tag: | Category: