273

I'm trying to set up less on phpstorm so I can compile .less files to .css on save. I have installed node.js and the next step (according to this https://www.jetbrains.com/webstorm/help/transpiling-sass-less-and-scss-to-css.html) is running this command in the terminal

npm install -g less

However when I do this I get these errors

☁  ~  npm install -g less
npm WARN install Couldn't install optional dependency: EACCES: permission denied, mkdir '/Users/brentscholl/.npm/mkdirp/0.5.1'
npm WARN install Couldn't install optional dependency: EACCES: permission denied, mkdir '/Users/brentscholl/.npm/graceful-fs/3.0.8'
npm WARN install Couldn't install optional dependency: EACCES: permission denied, mkdir '/Users/brentscholl/.npm/extend/3.0.0'
npm WARN install Couldn't install optional dependency: EACCES: permission denied, mkdir '/Users/brentscholl/.npm/readable-stream/2.0.4'
npm WARN install Couldn't install optional dependency: EACCES: permission denied, mkdir '/Users/brentscholl/.npm/chalk/1.1.1'
npm WARN install Couldn't install optional dependency: EACCES: permission denied, mkdir '/Users/brentscholl/.npm/xtend/4.0.1'
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
/usr/local/lib
└─┬ less@2.5.3
  ├─┬ errno@0.1.4
  │ └── prr@0.0.0
  ├── image-size@0.3.5
  ├── mime@1.3.4
  ├─┬ promise@6.1.0
  │ └── asap@1.0.0
  └─┬ source-map@0.4.4
    └── amdefine@1.0.0

npm ERR! Darwin 15.0.0
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install" "-g" "less"
npm ERR! node v5.0.0
npm ERR! npm  v3.3.6
npm ERR! path /usr/local/lib/node_modules
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall access

npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR!     at Error (native)
npm ERR!  { [Error: EACCES: permission denied, access '/usr/local/lib/node_modules']
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/usr/local/lib/node_modules' }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.

npm ERR! Please include the following file with any support request:
npm ERR!     /Users/brentscholl/npm-debug.log

I'm a complete noob here and not sure what to do next. Any help would be very appreciated!

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
xslibx
  • 4,249
  • 9
  • 35
  • 52

14 Answers14

500

Using sudo is not recommended. It may give you permission issue later. While the above works, I am not a fan of changing folders owned by root to be writable for users, although it may only be an issue with multiple users. To work around that, you could use a group, with 'npm users' but that is also more administrative overhead. See here for the options to deal with permissions from the documentation: https://docs.npmjs.com/getting-started/fixing-npm-permissions

I would go for option 2:

To minimize the chance of permissions errors, you can configure npm to use a different directory. In this example, it will be a hidden directory on your 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 jshint

If still show permission error run (mac os):

sudo chown -R $USER ~/.npm-global   

This works with the default ubuntu install of:

sudo apt-get install nodejs npm

I recommend nvm if you want more flexibility in managing versions: https://github.com/creationix/nvm

On MacOS use brew, it should work without sudo out of the box if you're on a recent npm version. Enjoy :)

alditis
  • 4,633
  • 3
  • 49
  • 76
Vincent Gerris
  • 7,228
  • 1
  • 24
  • 22
  • Heads up-- on Ubuntu 18.04 I had to log out and log back in before the change was applied automatically too all new terminal windows. – Alexander Nied Oct 20 '18 at 20:59
  • 1
    Alexander, did you make sure you used the source command? That should take care of setting the right environment variables, logging out shouldn't be needed (but works). – Vincent Gerris Oct 22 '18 at 07:26
  • this didnot work $ npm install -g jshint npm ERR! write after end npm ERR! A complete log of this run can be found in: npm ERR! /home/xx/.npm/_logs/2019-02-22T06_35_08_821Z-debug.log – Amjith Feb 22 '19 at 06:35
  • 1
    this works even for `npm i -g yarn`... the `sudo chown...` method doesn't work for that – nonopolarity Feb 04 '21 at 10:04
  • 3
    I really just can't believe something as simple as `npm i -g something` doesn't work for millions of people, and people google and some solutions say use `sudo` and some say `sudo` is dangerous and use hacks and users make some guess as to what maybe ok, and type in whatsoever they don't understand and not know the security consequence. This is the state of our engineering right now? – deeper-understanding Mar 28 '21 at 02:14
  • if anyone is struggling with that solution on macos 11 - I had to use export PATH="${HOME}/.npm-global/bin/:$PATH" instead; my system didn't want to understand what "~" is in PATH EDIT: or just use homebrew as author suggests :) – Łukasz Piaszczyk Aug 26 '21 at 13:48
  • Btw: `nvm` moans about using a prefix as described above (means trouble for this otherwise excellent solution): `Your user’s .npmrc file (${HOME}/.npmrc) has a 'globalconfig' and/or a 'prefix' setting, which are incompatible with nvm.` – Frank N May 26 '22 at 17:47
132

Run these commands in a terminal window (note: DON'T replace the $USER part... thats a linux command to get your user!):

sudo chown -R $USER ~/.npm
sudo chown -R $USER /usr/lib/node_modules
sudo chown -R $USER /usr/local/lib/node_modules
Vagner Rodrigues
  • 1,445
  • 1
  • 8
  • 2
  • 11
    Is it a good idea to make yourself the owner of things in `/usr`? What if someone else wants to install node stuff? – Mark Mar 12 '17 at 05:24
  • 6
    Worked for me on macOS High Sierra, I only needed one of the three commands: `sudo chown -R $USER /usr/local/lib/node_modules`. @Mark That's true, but it's of course no problem if you only have one user. – Fabian von Ellerts Aug 14 '18 at 10:46
  • @FabianvonEllerts Well, it's still easier to accidentally delete system files, since you won't need sudo anymore... I'd just follow best practices even when alone, but it's up to you of course. – Mark Aug 14 '18 at 12:57
  • I'm with Mark, unless you have one user (and even then) you don't want to have the /usr sub directories owned by one user, in that case, better use the group. But perhaps even better, just use brew as I mentioned in my answer, it will work without root out of the box. – Vincent Gerris Oct 08 '19 at 13:30
  • this method doesn't work for `npm i -g yarn` – nonopolarity Feb 04 '21 at 10:05
  • i got no such file directory – famfamfam Mar 25 '22 at 16:10
  • Do not use this, its too dangerous. It messed up the permissions and had to re-install the ubuntu again in my VM. – millisami Aug 04 '22 at 07:01
34

Mac OS X Answer

You don't have write access to the node_modules directory

npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules

Add your User to the directory with write access

  1. Open folder containing node_modules

    open /usr/local/lib/

  2. Do a cmd+I on the node_modules folder to open the permission dialog
  3. Add your user to have read and write access in the sharing and permissions section enter image description here
Roderick Jonsson
  • 1,111
  • 1
  • 10
  • 16
15

enter image description hereUse sudo -i to switch to $root, then execute npm install -g xxxx

Zin Min
  • 3,898
  • 1
  • 20
  • 24
user3393929
  • 172
  • 2
  • 4
12

Reinstall node and npm with Node Version Manger (as per written in npm documentation) to avoid permission errors:

In OSX:

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash

or follow this article:

http://dev.topheman.com/install-nvm-with-homebrew-to-use-multiple-versions-of-node-and-iojs-easily/

Windows users should install nvm-windows. For further help how to install nvm refer the nvm readme.

Then choose for example:

nvm install 8.0.0
nvm use 8.0

Now you can give another try:

npm install -g less
gazdagergo
  • 6,187
  • 1
  • 31
  • 45
11

For my mac environment

sudo chown -R $USER /usr/local/lib/node_modules

solve the issue

Asadut Zaman
  • 273
  • 3
  • 6
4

You can use this command with the root password to add permission for writing for node_module folder:

sudo chown -R $USER /usr/local/lib/node_modules
Vitalii Andrusishyn
  • 3,984
  • 1
  • 25
  • 31
2

I have tried all the suggested solutions but nothing worked.

I am using macOS Catalina 10.15.3

Go to /usr/local/

Select bin folder > Get Info

Add your user to Sharing & Permissions. Read & Write Permissions. enter image description here

And go to terminal and run npm install -g @ionic/cli

It has helped me.

Gohar SUltan
  • 75
  • 1
  • 4
1

sudo chown -R $USER /usr/local/lib/node_modules This command will work

Apurva Aggarwal
  • 296
  • 3
  • 10
  • This is the working command, other answers using sudo with npm -g messed up the system permissions – Marware May 20 '22 at 16:09
1

You can try with sudo, like the following

sudo npm install -g less

If it does not work, you can refer to this link https://ionicframework.com/docs/developing/tips#resolving-permission-errors

Oybek Toshmatov
  • 407
  • 5
  • 11
1

I have got the same problem. updating npm was not working for me either so I just decided to reinstall it.

what worked for me is uninstall node from my system using.

sudo apt-get remove nodejs    
sudo apt-get remove npm

end then reinstall it after update.

sudo apt-get update
sudo apt install npm
ibrahimgb
  • 693
  • 5
  • 13
0

Try installing with sudo. I had a similar problem installing npm package globally. The problem occurs because current use does not have write permissions for making changes in directory.

Didn't work

npm i -g vsce 

Worked

sudo npm i -g vsce
Harshit Gangwar
  • 553
  • 7
  • 14
-2

In linux make sure getting all authority with:

sudo su
Geshe
  • 679
  • 1
  • 9
  • 23
-3

Another option is to download and install a new version using an installer.

https://nodejs.org/en/download/

ABC123
  • 1,037
  • 2
  • 20
  • 44