Global installations with npm
will fail on some distributions, if tried without sudo
.
npm WARN checkPermissions Missing write access to /usr/lib/node_modules
npm ERR! Error: EACCES: permission denied, access '/usr/lib/node_modules'
npm ERR! Please try running this command again as root/Administrator.
There's another option to using sudo
: Configure the prefix path.
Steps
- Create a directory to use as the
npm
global installation root:
$ mkdir ~/npm
- Set the
npm
prefix to the newly created directory:
$ npm config set prefix /home/me/npm
or, alternatively, edit ~/.npmrc
and add the line prefix=/home/me/npm
.
- Verify it with:
$ npm config get prefix
It should output the prefix path, i.e. /home/me/npm
. You can now do "user-global" installations without sudo
and everything will go under that directory:
/home/me/npm
bin
bower -> ../lib/node_modules/bower/bin/bower
harp -> ../lib/node_modules/harp/bin/harp
lib
node_modules
bower
harp
Finally, add the bin
directory to the $PATH to be able to access the binaries from everywhere. For example, edit ~/.profile
and add:
export PATH="/home/me/npm/bin/:$PATH"
or just create symbolic links to the necessary apps, in a directory already in the $PATH (i.e. /home/me/bin
).
Yarn
The same thing can be achieved with yarn like this:
$ yarn config set prefix /home/me/yarn