NPM cannot install dependencies - Attempt to unlock something which hasn't been locked

node.jsNpmKarma Runner

node.js Problem Overview


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?

node.js Solutions


Solution 1 - node.js

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

Solution 2 - node.js

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!

Solution 3 - node.js

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

Solution 4 - node.js

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.

Solution 5 - node.js

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.

Solution 6 - node.js

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

Solution 7 - node.js

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 !

Solution 8 - node.js

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.

Solution 9 - node.js

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTJ KirchnerView Question on Stackoverflow
Solution 1 - node.jsalexoviedo999View Answer on Stackoverflow
Solution 2 - node.jsTJ KirchnerView Answer on Stackoverflow
Solution 3 - node.jsBjarke WallingView Answer on Stackoverflow
Solution 4 - node.jschrisView Answer on Stackoverflow
Solution 5 - node.jsoccaslView Answer on Stackoverflow
Solution 6 - node.jsTim WhiteView Answer on Stackoverflow
Solution 7 - node.jsmayoView Answer on Stackoverflow
Solution 8 - node.jscheolgookView Answer on Stackoverflow
Solution 9 - node.jsWilmer SHView Answer on Stackoverflow