194

I've been trying to run an npm install on my package.json file, but I'm having a lot of trouble. It keeps saying "Error: Attempt to unlock XXX, which hasn't been locked" on all my dependences. Here's one of them:

Error: Attempt to unlock tbd@~0.6.4, which hasn't been locked
        at unlock (/usr/local/lib/node_modules/npm/lib/cache.js:1304:11)
        at cb (/usr/local/lib/node_modules/npm/lib/cache.js:646:5)
        at /usr/local/lib/node_modules/npm/lib/cache.js:655:20
        at /usr/local/lib/node_modules/npm/lib/cache.js:1290:7
        at /usr/local/lib/node_modules/npm/node_modules/lockfile/lockfile.js:167:38
        at OpenReq.Req.done (/usr/local/lib/node_modules/npm/node_modules/graceful-fs/graceful-fs.js:144:5)
        at OpenReq.done (/usr/local/lib/node_modules/npm/node_modules/graceful-fs/graceful-fs.js:64:22)
        at Object.oncomplete (fs.js:107:15)

If I try to run it as sudo, it seems to get further and start installing some packages, but some new errors popup instead:

> chokidar@0.8.1 postinstall /Users/tkirchner/Documents/Projects/mm-datatable/node_modules/karma/node_modules/chokidar
> node setup-deps.js

shell-init: error retrieving current directory: getcwd: cannot access parent directories: Permission denied

node.js:811
    var cwd = process.cwd();
                      ^
Error: EACCES, permission denied
    at Function.startup.resolveArgv0 (node.js:811:23)
    at startup (node.js:58:13)
    at node.js:902:3
npm ERR! error rolling back Error: ENOTEMPTY, rmdir '/Users/tkirchner/Documents/Projects/mm-datatable/node_modules/karma/node_modules/q'
npm ERR! error rolling back  karma@0.10.9 { [Error: ENOTEMPTY, rmdir '/Users/tkirchner/Documents/Projects/mm-datatable/node_modules/karma/node_modules/q']
npm ERR! error rolling back   errno: 53,
npm ERR! error rolling back   code: 'ENOTEMPTY',
npm ERR! error rolling back   path: '/Users/tkirchner/Documents/Projects/mm-datatable/node_modules/karma/node_modules/q' }
npm ERR! Error: ENOENT, chown '/Users/tkirchner/Documents/Projects/mm-datatable/node_modules/karma/node_modules/socket.io/lib/socket.io.js'

I recently updated my node and npm installations. So maybe that has something to do with it. Also, most of my development has been at the office and today I'm working over VPN, so maybe that has something to do with it too.

Any ideas?

TJ Kirchner
  • 4,321
  • 6
  • 24
  • 26

10 Answers10

421

As per photusenigma at: https://github.com/npm/npm/issues/4815

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/local/lib/node_modules

...and...if you're on a mac (like I am), and still see errors after running these commands, then run this last one and you should be good. (Recommend you try testing before you do this one. I don't like changing the permissions on the ENTIRE /usr/local directory unless it really seems necessary!)

sudo chown -R $USER /usr/local
alexoviedo999
  • 6,761
  • 1
  • 26
  • 17
  • 7
    Worked for me thanks! My node_modules folder was in a different spot though, maybe this'll help someone else ```sudo chown -R `whoami` /usr/lib/node_modules/``` – Justen Nov 16 '14 at 00:52
  • 4
    Strangely, recursive `chown` didn't change the permissions in `~/.npm/_locks` for me. I got mine working by running `sudo chown -R myname ~/.npm/_locks` – Sitati Nov 30 '14 at 07:02
  • 7
    Be aware that this solution is not nice for multi user platforms. The first command is ok, the second make a standard user owner of system files. /usr/local/lib/node_modules is only used by npm with --global option, that means using sudo also. Unfortunately, some install scripts using npm mix both global and non global npm commands... Creating this mess. – Fafaman Dec 19 '14 at 22:09
  • 5
    Agreed, all you need is the first command. Avoid the others as those really shouldn't be owned by your user. – pyrospade Feb 02 '15 at 04:14
  • THANK YOU! saved a lot of time. – user461112 Apr 29 '15 at 20:02
  • sudo chown -R $USER ~/.npm + sudo chown -R $USER /usr/local solved the issue. Basically the whole problem is the write access to these folders. – siddhusingh May 29 '15 at 11:51
  • 2
    is there a way to reverse the "effects" of the second line `sudo chown -R $USER /usr/local/lib/node_modules`? I ran it before reading that it's not a good idea.. – shaneparsons Jun 10 '15 at 15:21
  • Changing ownership of /usr/local? Are you sure you know what you're doing? – Gui Prá Jun 21 '15 at 21:17
  • Very helpful! Just a small difference I had to make... since I was running in a venv, `.npm` was actually in the directory of the application I was running rather than `~/.npm` – cchapman Nov 23 '16 at 16:59
153

I worked with a co-worker this afternoon and figured out what the problem was. My ".npm" folder in my home directory was owned by the root user instead of myself. I'm not sure what happened to cause that. Maybe I installed node or npm as the root admin at one point. In any case I just ran sudo chown -R [username] .npm and I was finally able to run npm install commands from my projects again!

neoneye
  • 50,398
  • 25
  • 166
  • 151
TJ Kirchner
  • 4,321
  • 6
  • 24
  • 26
  • 11
    It also helps if you execute the same command on `~/tmp/` as well, or whatever location Node uses as a temp location – Jason Mar 16 '14 at 01:12
  • 1
    Thank you for your answer! Saved me some headache as the error message is bad. But I will say, that npm must be doing this by default because I'm pretty sure I didn't install it under root myself. And, for completeness, you have to chown -R .npm – crowmagnumb Apr 14 '14 at 23:59
  • Glad I could help :) And, good catch! I'll modify my response to say that. – TJ Kirchner Apr 15 '14 at 13:24
21

In my case the issue was invoking npm with a user that does not have a HOME directory, so for example the following command would fail:

sudo -u someUser npm install

The solution is to provide a HOME directory, where someUser has write access:

sudo -u someUser HOME=/some/directory npm install
Bjarke Walling
  • 371
  • 2
  • 6
  • Upvoted because this did actually work for me, so kudos and thanks. I'm deploying to production and the user did't even have a .npm directory (that I could find, anyway) – Stuart Watt Aug 20 '14 at 20:25
  • Works on ChromeOS with the "chronos" user. – Kyle Coberly Mar 14 '15 at 23:44
  • I was able to work around this by specifying `sudo -u someUser -i`, which starts a new login shell. I looked into this some more and the caching code in npm uses the npm.cache variable, which I'm guessing defaults to `$HOME/.npm/`. sudo can also have different behavior with what the HOME variable gets set to depending upon the configuration and the presence or absence of command line options. – jgibson Mar 18 '15 at 01:00
11

Had the same issue and fixed it by changing the persmissions as per the accepted answer:

sudo chown -R $USER ~/.npm

However, the second command should be avoided as it downgrades the permissions of a system resource (sudo chown -R $USER /usr/local/lib/node_modules). Not a good idea.

For the record: "usr" in /usr/local stands for Unix System Resources.

chris
  • 1,787
  • 1
  • 13
  • 13
  • 1
    This should probably be a comment on the accepted answer rather than posted as a new answer. – Kmeixner Apr 30 '15 at 16:37
  • The author of that answer knows this but hasn't updated his answer, so I say a separate one is much warranted. – Gui Prá Jun 21 '15 at 21:18
4

None of this worked for me. I had to run literally as root by doing the following:

sudo su -
sudo npm install forever -g

Then the package installed on Linux Ubuntu 14.04.

occasl
  • 5,303
  • 5
  • 56
  • 81
2

The following command should fix permission issues:

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

You can read about another officially recommended solutions here:

https://docs.npmjs.com/getting-started/fixing-npm-permissions

Tim White
  • 118
  • 9
1

My solution:

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

in my case was :

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

But I was getting the same problem, finally after

npm cache clean

it worked !

mayo
  • 3,845
  • 1
  • 32
  • 42
0

I had the same problem and tried to fix the permission/ownership of npm related files and directories for hours but had no luck with that.

Suddenly I found that I had ~/.npmrc file with cache entry pointing to a non-existing directory. Removed that cache property to use the default cache location and now it's solved.

cheolgook
  • 107
  • 2
  • 2
0

Disclaimer

I am a Windows user. However, my team and I have come across a number of issues regarding npm installaion errors.

Problems

The following is a list of lessons learned and a possible radical solution that has always rescued us:

  1. node_modules, the npm local installation directory becomes protected from modification by a shortcoming of the OS such as the inability to process paths longer than 255 characters.
  2. If the folder is erased by means of a command line tool it may still appear as if the folder exists in the explorer that when trying to access it gives a number of permission errors.
  3. Depending on your antivirus and/or local policy manager you may be able to create the node_modules folder and later relegated access or permissions to it resulting in a number of installation errors.
  4. Enable npm logs to gain further insight into possible problems with:

    npm install --loglevel verbose

Radical

Install rimraf globally

 npm install rimraf -g

Run rimraf on node_modules:

rimraf yourDir/node_modules

Then try running:

npm install

Warning!

Or lack there of. Be extremely careful about what follows the command rimraf. There are no warnings, no prompts, there is nothing. It simply erases the directory from the phase of the earth clean, as if it was never there. Try it at your own risk.

Wilmer SH
  • 1,417
  • 12
  • 20
0

for me, it was my proxy... and make sure to delete package-lock.json. this worked for me on my mac / unix based system:

npm config rm proxy
npm config rm https-proxy
npm config delete proxy
npm config delete https-proxy
npm config --global rm proxy
npm config --global rm https-proxy
npm config set registry "http://registry.npmjs.org"
npm config set strict-ssl false

npm install 

run: scutil --proxy

you should get dictionary list...

then get these values from that list:

HTTPProxy : 127.0.0.1 HTTPPort : 8118

then include them in this command:

npm config set proxy http://127.0.0.1:8119

then include this:

npm config set https-proxy https://123.0.0.1:8118 

reference: https://www.sneppets.com/angular/how-to-make-npm-install-command-to-work-behind-proxy/

seems
  • 92
  • 1
  • 11