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

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?


Solution 1:

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:

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!