Attempt to unlock, which hasn't been locked #4815

Closed
suakang opened this Issue Feb 28, 2014 · 22 comments

Projects

None yet
suakang commented Feb 28, 2014

Hi,
I'm trying to download yeoman, and I keep getting 'Attempt to unlock, which hasn't been locked' error for all bower and grunt.

I previously added them in root directory using 'sudo', but I fixed the order of bin locations. I tried to clean cache and run again, but still get the same error message as below.

SuAs-Mac-Pro:~ suakang$ npm install -g bower
npm ERR! Error: Attempt to unlock bower, which hasn't been locked
npm ERR!     at unlock (/usr/local/lib/node_modules/npm/lib/cache.js:1304:11)
npm ERR!     at cb (/usr/local/lib/node_modules/npm/lib/cache.js:866:5)
npm ERR!     at /usr/local/lib/node_modules/npm/lib/cache.js:883:20
npm ERR!     at /usr/local/lib/node_modules/npm/lib/cache.js:1290:7
npm ERR!     at /usr/local/lib/node_modules/npm/node_modules/lockfile/lockfile.js:167:38
npm ERR!     at OpenReq.Req.done (/usr/local/lib/node_modules/npm/node_modules/graceful-fs/graceful-fs.js:144:5)
npm ERR!     at OpenReq.done (/usr/local/lib/node_modules/npm/node_modules/graceful-fs/graceful-fs.js:64:22)
npm ERR!     at Object.oncomplete (fs.js:107:15)
npm ERR! If you need help, you may report this *entire* log,
npm ERR! including the npm and node versions, at:
npm ERR!     <http://github.com/npm/npm/issues>

npm ERR! System Darwin 13.0.0
npm ERR! command "node" "/usr/local/bin/npm" "install" "-g" "bower"
npm ERR! cwd /Users/suakang
npm ERR! node -v v0.10.26
npm ERR! npm -v 1.4.3
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     /Users/suakang/npm-debug.log
npm ERR! not ok code 0
SuAs-Mac-Pro:~ suakang$ npm cache clean
SuAs-Mac-Pro:~ suakang$ npm install
npm WARN package.json suakang@0.0.0 No description
npm WARN package.json suakang@0.0.0 No repository field.
npm WARN package.json suakang@0.0.0 No README data
SuAs-Mac-Pro:~ suakang$ npm install -g yo
npm ERR! Error: Attempt to unlock yo, which hasn't been locked
npm ERR!     at unlock (/usr/local/lib/node_modules/npm/lib/cache.js:1304:11)
npm ERR!     at cb (/usr/local/lib/node_modules/npm/lib/cache.js:866:5)
npm ERR!     at /usr/local/lib/node_modules/npm/lib/cache.js:883:20
npm ERR!     at /usr/local/lib/node_modules/npm/lib/cache.js:1290:7
npm ERR!     at /usr/local/lib/node_modules/npm/node_modules/lockfile/lockfile.

I had a similar issue that I posted on StackOverflow. Maybe this is the answer you're looking for:
http://stackoverflow.com/questions/22152162/npm-cannot-install-dependencies-attempt-to-unlock-something-which-hasnt-been/22207869#22207869

The error message seems quite misleading. If npm is truly trying to unlock a resource that's not locked, npm should ignore it and move on. If the error is arising for a different reason, as mentioned here, it should detect this condition better and suggest a more meaningful solution to the problem at hand.

I have installed nodejs with sudo to /usr/local in Linux. The npm cache in my home directory is owned by root after doing npm install -g, as stated in the SO issue above.

I also noticed that npm install fails with the unlock error, when I run sudo -u jenkins npm install to investigate a broken build. This seems to indicated that npm does not allow sudo usage (or root installation).

I've run into this on one of my dev machines as well....and it IS due to permission settings on the node and npm files. As is indicated above, it's generally recommended that npm NOT be installed via sudo...or as root, but rather have each user on the system who needs npm have their own separate installation.

But the fact you're seeing the errors means you're already past doing that, and you need a different approach to fix things now. While there is some argument that node may be safer installed via sudo, some setup scripts seem to break....it seems they lose any "sudo" context part way through, and portions of the script begin getting called in the context of the current user - not sudo.

What to do?

Option 1:
Changing the permissions on the critical node and npm directories (and everything in them) to be as if they had been installed by the current user.

IMPORTANT: Only do this IF you are the primary user of the system, and you're system account is really the only one that needs node & npm! (see Option 2 for an alternate solution if you can't change file permissions as described below, or need to leave the version installed by root "as is". Actually...if you're reading this...it may be worthwhile to check out Option 2 anyway....it may be a better solution - depending on your situation!).

Run these commands in a terminal window (note - DON'T replace the whoami part...thats a linux command to get your user!):

sudo chown -R `whoami` ~/.npm
sudo chown -R `whoami` /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 `whoami` /usr/local

A stackoverflow reference a friend shared with me (but only AFTER I'd spent half a day realizing I needed to do the above!) which essentially says the exact same thing, and I think validates this approach: http://stackoverflow.com/questions/16151018/npm-throws-error-without-sudo

Option 2
If for some reason you can't or don't want to do what's outlined in Option 1, check out this collection of gists. I've not used these personally, but have a couple of colleagues who have in the past, and claimed they worked well.
https://gist.github.com/isaacs/579814

suakang commented Jul 7, 2014

The option 1 fixed my problem!
Thank you so much!

@iarna iarna closed this Sep 16, 2014

Thanks for that helpful tip! I was scratching my head a bit on this issue.

@photusenigma thank you!!!

pxwise commented Nov 29, 2014

Same issue installing yeoman on a Mac OS Mavericks. @photusenigma 's Option 1 with all three commands worked. thanks! 👍

Thanks a lot @photusenigma, I used the first two commands and it fixed my issues on OSX Yosemite.

@othiym23 othiym23 added the support label Dec 12, 2014
@blaggacao blaggacao referenced this issue in phusion/passenger-docker Jan 2, 2015
Closed

breaking npm issue #63

Could you re-open please? It does not seem to be cured... or did I miss a pull request?

<hr>

Someone provided Aspirine, but thats not the same thing.

Gargaroz commented Feb 4, 2015

Hi I'm using Windows 7 Professional, how can I adapt this solution #4815 (comment) to my environment?

Thanks @photusenigma
the option #1 works for me. Just change permission to current user. 👍

Its worth noting that the last command
"sudo chown -R whoami /usr/local"
can be break other services, for instance if you have other projects using a xamp stack where
/usr/local/mysql
or
/usr/local/var/mysql
need to owned by _mysql:admin
hence running the above command can break other projects,
(ask me how i know)
It's an easy to fix if you know which files need which users
(in my case chown -R _mysql:admin /usr/local/var/mysql)
but could be hard to troubleshoot
i definitely recommend running the first two commands and testing before running the last

Permission changes worked for me as well, thanks @photusenigma!

Worked for me! Thanks @photusenigma

Awesome I got mine to work also! Thanks @photusenigma

amponce commented May 19, 2015

Very helpful, option 1 worked for me as well! thanks

Thanks for this - have been scratching my head on macosx

nikolay commented Aug 8, 2015

I'm getting this error on Ubuntu 14.04, node v0.10.40, npm v1.4.28. When I run npm install once more, it works - doesn't work on the initial npm install for some reason. Any ideas?

Changing files permissions worked for me. Thanks a lot

sudo chown -R whoami ~/.npm
sudo chown -R whoami /usr/local/lib/node_modules

One might get same error if $HOME environment variable is not set properly.

179 error makeDirectory homeless?
180 verbose stack Error: Attempt to unlock /opt/automation/deployed/tixchangeNode/node_modules/install, which hasn't been locked
180 verbose stack     at unlock (/opt/automation/deployed/nodejsRole_nodejs-4.3.1-linux-x64/lib/node_modules/npm/lib/utils/locker.js:64:11)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment