218

I run npm install lodash but it throws Error: EACCES: permission denied error. I know it is permission issue but as far as I know, sudo permission is not required for installing node module locally. If I run it with sudo, it gets installed inside ~/node_modules folder. drwxrwxr-x is the file permission of existing folder. I can't figure out what might have gone wrong.

Below is the error message.

npm ERR! tar.unpack untar error /home/rupesh/.npm/lodash/4.13.1/package.tgz
npm ERR! Linux 3.13.0-88-generic
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install" "lodash"
npm ERR! node v4.3.1
npm ERR! npm  v2.14.12
npm ERR! path /home/rupesh/node_modules/lodash
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall mkdir

npm ERR! Error: EACCES: permission denied, mkdir '/home/rupesh/node_modules/lodash'
npm ERR!     at Error (native)
npm ERR!  { [Error: EACCES: permission denied, mkdir '/home/rupesh/node_modules/lodash']
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'mkdir',
npm ERR!   path: '/home/rupesh/node_modules/lodash',
npm ERR!   fstream_type: 'Directory',
npm ERR!   fstream_path: '/home/rupesh/node_modules/lodash',
npm ERR!   fstream_class: 'DirWriter',
npm ERR!   fstream_stack: 
npm ERR!    [ '/usr/local/lib/node_modules/npm/node_modules/fstream/lib/dir-writer.js:35:25',
npm ERR!      '/usr/local/lib/node_modules/npm/node_modules/mkdirp/index.js:47:53',
npm ERR!      'FSReqWrap.oncomplete (fs.js:82:15)' ] }
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!     /home/rupesh/Desktop/es6/npm-debug.log
Panda
  • 1,231
  • 18
  • 27
xruptronics
  • 2,785
  • 3
  • 12
  • 17

29 Answers29

144

This command fix the issue. It worked for me:

sudo npm install -g --unsafe-perm=true --allow-root
Saniya syed qureshi
  • 3,053
  • 3
  • 16
  • 22
  • 1
    For anyone like myself, while this command appears to have worked at the time of writing, for Node `v10.10.0` I got the following error: "npm ERR! Can't install /var/www/project: Missing package name" – Wayne Smallman Sep 12 '18 at 09:55
  • 1
    The flags --unsafe-perm=true --allow-root worked for us using node v10.15.0 to install as user root an existing application where node-sass would not install. Exact command we used to get node-sass installed to local directory node_modules folder was # npm install --save-dev --unsafe-perm=true --allow-root node-sass@latest – Peter Edwards Jan 05 '19 at 12:44
  • You can use root to solve the problem (though this is not recommended). – Chris Marotta Jun 13 '19 at 21:41
  • 1
    I was able to get it to work by using those flags so really the answer is :sudo npm install -g --unsafe-perm=true --allow-root @ – Frank H Aug 04 '20 at 02:40
  • this worked for me: sudo npm install --save -g solgraph --unsafe-perm=true --allow-root – William Mar 06 '21 at 19:55
  • For those who arrive here trying to install with 'yarn' instead of 'npm', just replace it respectively. – Hack06 Apr 25 '22 at 10:14
  • 2
    error ```js ~ $ npm install -g --unsafe-perm=true --allow-root npm ERR! Cannot read properties of undefined (reading 'name') npm ERR! A complete log of this run can be found in: npm ERR! /data/data/com.termux/files/home/.npm/_logs/2022-10-09T09_45_11_327Z-debug-0.log ``` – coco bar Oct 09 '22 at 09:46
79

I have same issue with webpack server installation on globally, Use steps from this Url Solved my issue, my be work for you.

Steps mention above There: Back-up your computer before you start.

Make a directory for global installations:

1. mkdir ~/.npm-global

Configure npm to use the new directory path:

2.npm config set prefix '~/.npm-global'

Open or create a ~/.profile file and add this line:

3.export PATH=~/.npm-global/bin:$PATH

Back on the command line, update your system variables:

4.source ~/.profile

Test: Download a package globally without using sudo.

npm install -g jshint

Instead of steps 2-4, you can use the corresponding ENV variable (e.g. if you don't want to modify ~/.profile):

NPM_CONFIG_PREFIX=~/.npm-global
Vishal Patel
  • 1,715
  • 16
  • 26
42

I had problem on Linux. I wrote

chown -R myUserName /home/myusername/myfolder

in my project folder.

WARNING: this is NOT the right way to fix it; DO NOT RUN IT, if you aren't sure of what could be the consequences.

IsmailS
  • 10,797
  • 21
  • 82
  • 134
Sergey
  • 959
  • 8
  • 10
  • 24
    Very unrecommended. That touches all your directories and removes root rights on any of them. If some things need to be set as root, this destroys your current configuration. – Patrick Da Silva Jun 20 '19 at 14:03
  • 3
    I agree @PatrickDaSilva. This messed up my entire server.. – Simon Degn Nov 09 '20 at 00:03
  • It would be safer to use an absolute path, such as chown -R myusername /home/myusername/myfolder instead of ./*. If you run the command in the answer from the root folder it will screw everything up! – raphael75 Dec 22 '20 at 17:12
  • 1
    Anyway in my case it was the right solution. for some reason the Webpack output folder was owned by `root` so now it's owned by me again and it works. I agree you do not do this randomly, but if you look through your folders and are like WTH is this root? Then do this on that folder. – ThaJay May 25 '21 at 13:55
  • 3
    @PatrickDaSilva there is no harm in changing the ownership of a users own files in their home dir to their user. In fact usually thats what you want: chown -R $USER ~/ – ekerner Sep 14 '21 at 13:26
29

Try to give all permission to your project folder with below command

sudo chmod -R 777 /yourProjectDirectoryName

run with

sudo npm install lodash
Raj Gohel
  • 999
  • 7
  • 14
  • 1
    It is a jr solution – Pablo Papalardo Mar 23 '20 at 22:11
  • 2
    You may not even need 777. For me the problem was just the #'s were too low. (775 fixed it.) – Andrew Jun 12 '20 at 02:11
  • 8
    "Setting 777 permissions to a file or directory means that it will be readable, writable and executable by all users and may pose a huge security risk." -https://linuxize.com/post/what-does-chmod-777-mean/ – MillerC Aug 26 '20 at 14:16
19

Creating package.json using npm init solved my issue.

xruptronics
  • 2,785
  • 3
  • 12
  • 17
  • But I am not sure why package.json file is required while installing node module as package.json file is not updated or used in my case because I have not used --save or --save-dev or similar command. – xruptronics Jul 12 '16 at 16:26
  • this is weird. i think some thing was messed up in your npm conf and doing init reset it, anyways it should be a mystery forever as i couldn't find a single reference to a similar problem elsewhere. – AJS Jul 13 '16 at 09:37
  • Had a similar issue with npm 6.9.0 and this worked for me too. – Chris Marotta Jun 13 '19 at 21:39
  • How to do this if package.json is auto-generated, for example, when using create-react-app? – stuckoverflow Dec 17 '22 at 13:36
  • But what if we get the issue during `npm init` . `Error: EACCES: permission denied, open '/var/www/html/wp-content/themes/mytheme/package.json` – user1207289 Jul 03 '23 at 12:57
16

It doesn't have write permissions for others (r-x). Try with

chmod a+w <folder>

and repeat.

Juanjo Salvador
  • 1,073
  • 1
  • 12
  • 33
16
sudo chown -R $(whoami) /usr/local/bin

Makes current user owner of the /usr/local/bin folder. Permissions on this folder were the trouble.

Cedric Ipkiss
  • 5,662
  • 2
  • 43
  • 72
14

A related issue:

Wasted 3 hours spanning several days.

On a AWS EC2 machine, below worked:

sudo chown -R $(whoami) /home/ubuntu/.cache
sudo chown -R $(whoami) /home/ubuntu/.config
sudo chown -R $(whoami) /home/ubuntu/.local
sudo chown -R $(whoami) /home/ubuntu/.npm
sudo chown -R $(whoami) /home/ubuntu/.pm2

Hope that helps.

Manohar Reddy Poreddy
  • 25,399
  • 9
  • 157
  • 140
13

This solved my issue straight away - mac Mojave 10.14.6 - PhpStorm.

Unhandled rejection Error: EACCES: permission denied, mkdir '/Users/myname/.npm/_cacache/index-v5/fb/5a'

sudo chown -R $USER:$GROUP ~/.npm
sudo chown -R $USER:$GROUP ~/.config

Original post: https://stackoverflow.com/a/50639828

ulittle
  • 156
  • 1
  • 6
9

LUBUNTU 19.10 / Same issue running: $ npm start

dump: Error: EACCES: permission denied, open '/home/simon/xxx/pagebuilder/resources/scripts/registration/node_modules/.cache/@babel/register/.babel.7.4.0.development.json' at Object.fs.openSync (fs.js:646:18) at Object.fs.writeFileSync (fs.js:1299:33) at save (/home/simon/xxx/pagebuilder/resources/scripts/registration/node_modules/@babel/register/lib/cache.js:52:15) at _combinedTickCallback (internal/process/next_tick.js:132:7) at process._tickCallback (internal/process/next_tick.js:181:9) at Function.Module.runMain (module.js:696:11) at Object. (/home/simon/xxxx/pagebuilder/resources/scripts/registration/node_modules/@babel/node/lib/_babel-node.js:234:23) at Module._compile (module.js:653:30) at Object.Module._extensions..js (module.js:664:10) at Module.load (module.js:566:32)

Looks like my default user (administrator) didn't have rights on node-module directories.

This fixed it for me!

$ sudo chmod a+w node_modules -R ## from project root

Simon P
  • 337
  • 3
  • 8
8

From what i can see in your logs you posted:

npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'mkdir',
npm ERR!   path: '/home/rupesh/node_modules/lodash',
npm ERR!   fstream_type: 'Directory',
npm ERR!   fstream_path: '/home/rupesh/node_modules/lodash',
npm ERR!   fstream_class: 'DirWriter',

directory /home/rupesh/node_modules/ doesn't have necessary permissions to create directory so run chown -r rupesh:rupesh /home/rupesh/node_modules/ this should solve it.

d-_-b
  • 21,536
  • 40
  • 150
  • 256
AJS
  • 1,993
  • 16
  • 26
  • Doing chown -R rupesh:rupesh /home/rupesh/node_modules/ and running npm install lodash again installed lodash inside ~/node_module folder.But this time it does not throw error.But it is not where I intend to install. – xruptronics Jul 12 '16 at 09:45
  • yes i know you should create the project in your home directory rather than Desktop. npm for some reason is installing your modules in `/home/rupesh/node_modules` for some reason. Any way working in your home directory should be ideal anyway – AJS Jul 12 '16 at 09:51
  • I think ~/Projects/es6 is equivalent to ~/Desktop/es6 structure wise.What I am having trouble figuring out is why node_modules is not created inside es6 folder and why lodash(or whatever) module is not installed inside that. I had not faced this issue earlier but what went wrong today,that is what i want to figure out. – xruptronics Jul 12 '16 at 10:15
  • yup i read my previous two comment and realized the stupidity :), well i looked around and couldn't find anyone else having the same problem. Is your code able to find `loadash` module?. – AJS Jul 12 '16 at 11:18
6

If you getting an error like below

Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/<PackageName>/vendor'

I suggest using the below command to install your global package

sudo npm install -g <PackageName> --unsafe-perm=true --allow-root
Nisharg Shah
  • 16,638
  • 10
  • 62
  • 73
4

FWIW I had the same symptoms, but with a different package. Creating package.json and running npm init did NOT solve my issue.

On this system, apparently new folders in this location were being created with root permissions. During npm install, new folders are created. This caused npm install to fail partway, even with sudo.

The solution was to run npm install app in a different location with no root umask.

4

I solved this issue by changing the permission of my npm directory. I went to the npm global directory for me it was at

/home/<user-name>

I went to this directory by entering this command

cd /home/<user-name>

and then changed the permission of .npm folder by entering this command.

sudo chmod -R 777 ".npm"

It worked like a charm to me. But there is a security flaw with this i.e your global packages directory is accessible to all the levels.

Aniruddh Agarwal
  • 900
  • 1
  • 7
  • 22
  • 2
    Why would you allow the entire world to access this particular folder? – Mooncake Jul 27 '19 at 06:49
  • Whatever you are hoping to accomplish, **`chmod 777` is *wrong* and *dangerous.*** You absolutely do not want to grant write access to executable or system files to all users under any circumstances. You will want to revert to sane permissions ASAP (for your use case, probably `chmod 755`) and learn about the Unix permissions model before you try to use it again. If this happened on a system with Internet access, check whether an intruder could have exploited this to escalate their privileges. – tripleee Jan 25 '22 at 10:46
4

Try using this: On the command line, in your home directory, create a directory for global installations:

mkdir ~/.npm-global

Configure npm to use the new directory path:

npm config set prefix '~/.npm-global'

In your preferred text editor, open or create a ~/.profile file and add this line:

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

On the command line, update your system variables:

source ~/.profile

Test installing package globally without using sudo, Hope it helps

AyushKatiyar
  • 1,000
  • 8
  • 15
3

After trying anything, I followed this video from NPM. It solved for me smoothly! https://www.youtube.com/embed/bxvybxYFq2o

Instructions as follow (on older Mac):

  1. In the terminal, inside your user directory, create this folder mkdir .npm-global. This will be the new folder where all your global NPM installations will go, instead of the default .npm which has permission issues.
  2. cd .npm-global/
  3. Run npm config set prefix /Users/your_user/.npm-global
  4. Set the Path for the system to know where to find the packages: Run vi ~/.profile. If you don't have this file, run touch ~/.profile first, then vi ~/.profile. Write export PATH=/Users/your_user/.npm-global/bin:$PATH at the very top. You could also open this file with a text editor (as it's a hidden file, use SHIFT + CMD + . to show it).
  5. Back to the terminal, inside the /.npm-global folder, run source ~/.profile

You should now be ready to install your package globally.

However, the above seems not to be working on newer OS, like BigSure.

This should solve:

sudo chown -R `whoami` ~/.npm
sudo chown -R `whoami` /usr/local/lib/node_modules
Pat Racco
  • 421
  • 1
  • 4
  • 9
2

First install without -g (global) on root. After try using -g (global) It worked for me.

Alex M
  • 2,756
  • 7
  • 29
  • 35
Renjith
  • 21
  • 4
  • 1
    Worked!! On Mac moved my code folder to my root folder: HardDrive/ , and "npm install" ran flawlessly. Then I moved my code folder back to where I wanted it inside HardDrive/users/myUserName/Projects/code , ran "npm install" again, and this time, it worked. – Josh Mar 12 '19 at 10:11
2

Here's the solution for GNU/Linux (Debian) users (Replace USERNAME with your username):

sudo chown -R $USER:$(id -gn $USER) /home/USERNAME/.config

J-Snor
  • 89
  • 1
  • 6
1

I tried most of these suggestions but none of them worked. Then I ran npm clean-install and it solved my issues.

MoKG
  • 266
  • 3
  • 13
1

I tried a few of the answers from above on my Mac running Bigsur 11.4 and did not work. But what worked was the following instructions posted on npmjs docs website. https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally

ultrasounder
  • 585
  • 1
  • 7
  • 16
0

Remove dist folder and this solve my problem!!

Hekmat
  • 153
  • 5
0

On Windows it ended up being that the port was already in use by IIS.

Stopping IIS (Right-click, Exit), resolved the issue.

Atron Seige
  • 2,783
  • 4
  • 32
  • 39
0

Just change the owner of the global node_modules directory to be your user:

sudo chown -R $USER:$GROUP /usr/local/lib/node_modules
didil
  • 693
  • 8
  • 22
0

node recommends executing following:

 sudo chown -R $USER:$(id -gn $USER) /home/venkatesh/.config

If you execute

npm config

You will see something like this

│                   npm update check failed                   │
│             Try running with sudo or get access             │
│            to the local update config store via             │
│ sudo chown -R $USER:$(id -gn $USER) /home/venkatesh/.config │

It worked for me.

Manasvini Ganesh
  • 483
  • 5
  • 16
0

Opening CMD(Windows WSl in this case) as Administrator worked out for me.I am using NodeJS Version 14.15.1

Sihat Afnan
  • 742
  • 7
  • 14
0

The permission denied error was persistent after all chown command. However, updating npm sudo npm install -g npm@8.3.2 and changing path to PATH="$PATH" worked for me.

Pratik Agarwal
  • 300
  • 4
  • 16
0

In my case i just did the following.

  1. Create the folder if it doesn't exist. sudo mkdir ./my-folder

  2. Add permissions to the folder sudo chown -R 1001 ./my-folder

James Mwase
  • 828
  • 1
  • 16
  • 29
0

For me I was running this as root:

npm ci --unsafe-perm=true --allow-root; sudo chown -R www-data:www-data /my/project/root/node_modules; npm run build

and getting

OptimizeCssAssetsWebpackPluginError: EACCES: permission denied, open '/my/project/root/static_dev/img/ProximaNova-Regular.otf'
/my/project/root/static_dev/img/ProximaNova-Regular.otf

ProximaNova font was owned by root, but I was running npm as root. And I had the flags to allow root to run.

$ ls -l /my/project/root/static_dev/img/ProximaNova-Bold.otf
-rw-r--r-- 1 www-data www-data 63808 Jun 30 03:49 /my/project/root/static_dev/img/ProximaNova-Bold.otf

Okay, it doesn't make any sense, but none of the answers here explain why this is occurring. Change permissions on the font file, click my heals together three times, close my eyes and say "There is no place like home"

$ chown -R www-data:www-data /my/project/root

Then the npm command above worked. It would be really nice if front end tech was less cryptic.

MagicLAMP
  • 1,032
  • 11
  • 26
-1

Install nvm curl -sL https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.0/install.sh -o install_nvm.sh Install a version of node v14 using nvm install 14 Then use the node version nvm use 14 Now you can remove / delete your node_modules folder and package-lock.json file and reinstall the packages using npm install and that's it you should be okay now

Jaba
  • 39
  • 3