Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

npm sets weird permisions for packages installed globally

I was about to start some node.js development at home, but stumbled on some weird behavior when installing npm packages globally.

I use WebStorm as my IDE, and want to use ECMAScript6 features, which means I have to transpile the sources, which WebStorm does by using Babel.

So I tried to install Babel globally:

$ sudo npm install -g babel-cli

What happens is that the files are all accessible fine from the root user, and the babel-cli directory and all files and directories in it look fine as long as I look as root. The problem comes when I try to look at the package, or use it, as another user:

$ ls -l /usr/lib/node_modules/babel-cli/
ls: cannot access '/usr/lib/node_modules/babel-cli/lib': Permission denied
ls: cannot access '/usr/lib/node_modules/babel-cli/index.js': Permission denied
ls: cannot access '/usr/lib/node_modules/babel-cli/scripts': Permission denied
ls: cannot access '/usr/lib/node_modules/babel-cli/node_modules': Permission denied
ls: cannot access '/usr/lib/node_modules/babel-cli/package.json': Permission denied
ls: cannot access '/usr/lib/node_modules/babel-cli/README.md': Permission denied
ls: cannot access '/usr/lib/node_modules/babel-cli/bin': Permission denied
total 0
d????????? ? ? ? ?            ? bin/
d????????? ? ? ? ?            ? lib/
d????????? ? ? ? ?            ? node_modules/
d????????? ? ? ? ?            ? scripts/
-????????? ? ? ? ?            ? index.js
-????????? ? ? ? ?            ? package.json
-????????? ? ? ? ?            ? README.md

It doesn't matter which package I install, they all become like this.

I am using node.js v6.3.0 and npm 3.10.3, on Ubuntu 16.04.

I installed node.js freshly as instructed here. If I try to update npm then the same happens to the global npm directory and npm becomes unavailable except to root.

When I list the global node_modules as a user I see

$ ll /usr/lib/node_modules/
total 8,0K
drwxr--r--  6 nobody root 4,0K jul 15 23:50 babel-cli/
drwxr-xr-x 10 root   root 4,0K jul 15 23:21 npm/

Besides the directory being owned by nobody I see nothing special.

There are of course no errors when I install the package.

Is it something wrong with npm and/or node? Is it something wrong with Ubuntu? Or how I installed node? What could be the reason for something like that to happen?

Installing locally works fine, and for the babel-cli package it is an acceptable workaround to install it as a local development package. I would still like to know what happens when installing packages globally, and why.


Problem answered here. Problem was unrelated to npm and node.

like image 585
Some programmer dude Avatar asked Jul 15 '16 21:07

Some programmer dude


1 Answers

If you want to install a package globally, just use a command without sudo like:

$ npm install -g <package>

If you're getting EACCES or permissions errors, the use of sudo should be avoided but you should instead fix your permissions so npm can run without sudo.

These errors are raised if you don't have the permissions to write to the folder that npm uses to store global packages. To fix that, you can start by finding what's the path to npm's default directory:

$ npm config get prefix

On most systems, it'll be /usr/local and you'll be able to fix permissions on this folder. If the folder is /usr or /usr/lib, you shouldn't change permissions on this directory since it'll cause some problems and in that case where you do not want to change permissions of the default directory, you can configure npm to use a different directory.

If you can and want to change permissions on the default folder, you can use:

$ sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}

It'll change the owner of npm's directories to the name of the current user.

If you want to change npm's default directory, you can start by creating this new folder:

$ mkdir ~/.npm-packages

Then, you configure npm to use this new folder:

$ npm config set prefix '~/.npm-packages'

You'll also need to add this new folder to your PATH (for example using ~/.profile):

export PATH=~/.npm-packages/bin:$PATH

At this point, you can update your system variables with source ~/.profile and test to install a package globally without using sudo.

You should be able to install the package without getting any permissions errors since npm will use the ~/.npm-packages folder.

You can find more informations regarding this matter on the npm documentation.

like image 154
HiDeo Avatar answered Oct 15 '22 22:10

HiDeo