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:

> [email protected] 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  [email protected] { [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?

2
  • Here's the package.json file, if that helps... (sigh), nvm, I can't seem to paste it in.. too many characters. Mar 3, 2014 at 16:39
  • what os are you running
    – wayne
    Mar 3, 2014 at 17:37

10 Answers 10

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
9
  • 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, 2014 at 0: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, 2014 at 7: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, 2014 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 2, 2015 at 4:14
  • 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.. Jun 10, 2015 at 15:21
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!

3
  • 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, 2014 at 1: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 <username> .npm Apr 14, 2014 at 23:59
  • Glad I could help :) And, good catch! I'll modify my response to say that. Apr 15, 2014 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
3
  • 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) Aug 20, 2014 at 20:25
  • Works on ChromeOS with the "chronos" user. Mar 14, 2015 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, 2015 at 1: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.

2
  • 1
    This should probably be a comment on the accepted answer rather than posted as a new answer.
    – Kmeixner
    Apr 30, 2015 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, 2015 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.

1
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

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 !

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.

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.

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/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.