Reputation: 409166
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
.
Upvotes: 5
Views: 1111
Reputation: 2362
If you change the user than you must do one of the following because when you installed npm at that time you are installed as Root user but after tat you change the user and that gives you permission error because this indicates that you do not have permission to write to the directories that npm uses to store global packages and commands.you can fix this via:-
Use a package manager that takes care of this for you.
If you're doing a fresh install of node on Mac OS you can avoid this problem altogether by using the Homebrew package manager. Homebrew sets things up out of the box with the correct permissions.
brew install node
OR
Change npm's default directory to another directory
There are times when you do not want to change ownership of the default directory that npm uses (i.e. /usr) as this could cause some problems, for example if you are sharing the system with other users.
Instead, you can configure npm to use a different directory altogether. In our case, this will be a hidden directory in our home folder.
Make a directory for global installations:
mkdir ~/.npm-global
Configure npm to use the new directory path:
npm config set prefix '~/.npm-global'
Open or create a ~/.profile file and add this line:
export PATH=~/.npm-global/bin:$PATH
Back on the command line, update your system variables:
source ~/.profile
Test: Download a package globally without using sudo.
npm install -g babel-cli
Instead of steps 2-4 you can also use the corresponding ENV variable (e.g. if you don't want to modify ~/.profile):
NPM_CONFIG_PREFIX=~/.npm-global
Upvotes: 0
Reputation: 10563
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.
Upvotes: 8