39

Note: Due to security concerns, please don't use the marked solution but instead the highest voted one!


original question:

I am trying to install monaca with this command.

npm install -g monaca

But right after getting these errors:

npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules 
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!  { Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR!   stack: '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' }

Any idea how to solve this problem? Thank you

erikbstack
  • 12,329
  • 21
  • 76
  • 113
  • See [what the **npm** docs have to say about this](https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally). – djvg Sep 23 '21 at 08:24

8 Answers8

98

add following lines to ~/.bashrc after installing npm:

npm set prefix ~/.npm
PATH="$HOME/.npm/bin:$PATH"
PATH="./node_modules/.bin:$PATH"

Execute following line after changes:

source ~/.bashrc

and as mentioned by @contemplator avoid using sudo

Dmitriy Fialkovskiy
  • 2,845
  • 8
  • 29
  • 41
Shivam ashtikar
  • 1,108
  • 6
  • 7
  • 3
    **NOTE: Do not use `sudo`** If using [Zsh](https://ohmyz.sh/) add the lines above to `~/.zshrc` after installing npm. Execute following line after changes: `source ~/.zshrc` – Corey Coto Sep 18 '19 at 02:16
  • 6
    The more zshrc-like way of doing this is `npm set prefix ~/.npm; path+=$HOME/.npm/bin; path+=./node_modules/.bin` (using path+= rather than appending the existing path to each line like in bash) – shacker Dec 30 '19 at 21:54
  • what about [fish](https://fishshell.com/)? – Tim4497 Mar 31 '22 at 12:01
18

Note: It is highly recommended to avoid using sudo with npm!

Using sudo is not recommended. It may give you permission issue later. While the above works, use these instructions to fix your issue permanently.

contemplator
  • 346
  • 3
  • 11
  • Also, using sudo runs untrusted code that some rando uploaded to npm, with root permissions on your system. – Guss Jan 28 '21 at 08:48
3

To all the warnings telling not to use sudo above, I'd add the following solution that worked pretty well for me while installing n, node version manager

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

This was taken from here: https://poopcode.com/missing-write-access-to-usr-local-lib-node-modules/

PS: for my specific use-case I also needed to run this one afterwards

sudo chown -R $USER /usr/local/bin/

kissu
  • 24,311
  • 11
  • 36
  • 67
  • It seems odd that the preferred way to fix this is to make a subpath of /usr not be owned by root. Does npm not have an equivalent of pip's `--user` flag? – tsbertalan Apr 21 '22 at 15:38
  • Actually, it does have such a mechanism--something like `npm config set prefix '~/.npm-global'`. See https://stackoverflow.com/questions/33725639/npm-install-g-less-does-not-work-eacces-permission-denied/40905762#40905762 – tsbertalan Apr 21 '22 at 15:41
2

This command will change the owner (chown) recursively (-R) for the current user in the specified directory

sudo chown -R $USER /usr/local/lib/node_modules
Richard Hulse
  • 10,295
  • 2
  • 32
  • 36
barmalej
  • 21
  • 2
0

Well, I used --save-dev and installed it not globally or using -g, the main problem occurs while you want it to write on default node_modules folder.

It solved my problem after 4 hours of checking multiple issues.

I even suggest you to use the npm init and make a package.json for a better dependency checking and then run npm install afterward. this video helps you for this https://www.youtube.com/watch?v=rTsz09zRuTU

Ebrahim
  • 1,554
  • 2
  • 24
  • 29
0

Please dont use sudo.

I don't know the context of your environment, but I got the error on a server where Plesk was running.

Maybe the following command will help (via SSH) to check the permissions:

plesk repair fs example.com.

Manual: https://docs.plesk.com/en-US/12.5/administrator-guide/plesk-administration/plesk-repair-utility/plesk-repair-utility-file-system.74668/

In my case the node_modules folder was copied via FTP and therefore it had the wrong corrections. If necessary, you can remove this and install it via Plesk using the Npm installation button.

Info: The button only appears if the document contains a package.json with information.

Julian
  • 313
  • 6
  • 15
-5

it is very simple you can use

sudo npm install -g kazam

or

su -

then

npm install -g kazam

explenation

su -

makes you as root ,who have permission to read , write and delete in all users click here for the screen shot showing the error and the solution in the update of npm

this is for ubuntu i don't know is it work for other os
Joel
  • 31
  • 1
  • 4
-16

An inadvisable way to fix the issue would be to use sudo:

sudo npm install -g monaca

However it would be better to find a way around this without using sudo.

npm install -g less does not work

Jon Winstanley
  • 22,404
  • 20
  • 73
  • 111
high roller
  • 850
  • 1
  • 11
  • 34