node.js NPM 无法安装依赖项 - 尝试解锁尚未锁定的内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22152162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:57:48  来源:igfitidea点击:

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

node.jsnpmkarma-runner

提问by TJ Kirchner

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:

我一直在尝试在我的 package.json 文件上运行 npm install,但我遇到了很多麻烦。它一直在我所有的依赖项上说“错误:尝试解锁尚未锁定的 XXX”。这是其中之一:

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:

如果我尝试以 sudo 的身份运行它,它似乎更进一步并开始安装一些软件包,但会弹出一些新的错误:

> [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.

我最近更新了我的节点和 npm 安装。所以也许这与它有关。此外,我的大部分开发工作都是在办公室进行的,今天我正在通过 VPN 工作,所以也许这也与它有关。

Any ideas?

有任何想法吗?

回答by alexoviedo999

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

根据 photusenigma 在: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!):

在终端窗口中运行这些命令(注意 - 不要替换 $USER 部分...这是获取用户的 linux 命令!):

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!)

...并且...如果您使用的是 mac(就像我一样),并且在运行这些命令后仍然看到错误,那么运行最后一个,您应该会很好。(建议您在执行此操作之前尝试测试。除非确实有必要,否则我不喜欢更改整个 /usr/local 目录的权限!)

sudo chown -R $USER /usr/local

回答by TJ Kirchner

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] .npmand I was finally able to run npm installcommands from my projects again!

今天下午我和一位同事一起工作,并找出了问题所在。我的主目录中的“.npm”文件夹由 root 用户而不是我自己拥有。我不确定是什么原因造成的。也许我曾经将 node 或 npm 安装为 root 管理员。无论如何,我只是跑了sudo chown -R [username] .npm,我终于能够npm install再次从我的项目中运行命令了!

回答by Bjarke Walling

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:

在我的情况下,问题是使用没有 HOME 目录的用户调用 npm,因此例如以下命令将失败:

sudo -u someUser npm install

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

解决方案是提供一个 HOME 目录,其中someUser具有写访问权限:

sudo -u someUser HOME=/some/directory npm install

回答by chris

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.

但是,应避免使用第二个命令,因为它会降级系统资源 ( sudo chown -R $USER /usr/local/lib/node_modules)的权限。不是个好主意。

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

记录在案:“usr”/usr/local代表Unix 系统资源

回答by occasl

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

这些都不适合我。我必须通过执行以下操作以 root 身份运行:

sudo su -
sudo npm install forever -g

Then the package installed on Linux Ubuntu 14.04.

然后将软件包安装在 Linux Ubuntu 14.04 上。

回答by Tim White

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

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

回答by mayo

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 !

有效 !

回答by cheolgook

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

我遇到了同样的问题,并试图修复 npm 相关文件和目录的权限/所有权几个小时,但没有运气。

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

突然,我发现我不得不~/.npmrc使用文件cache项指向一个不存在的目录。删除该cache属性以使用默认缓存位置,现在已解决。

回答by Wilmer Saint

Disclaimer

免责声明

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

我是Windows用户。但是,我和我的团队遇到了许多关于 npm installaion 错误的问题。

Problems

问题

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

以下是吸取的经验教训和可能的彻底解决方案的列表,这些解决方案总是拯救我们:

  1. node_modules, the npmlocal installation directory becomes protected from modification by a shortcoming of the OS such as the inability to process paths longer than 255characters.
  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_modulesfolder 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

  1. node_modulesnpm本地安装目录受到保护,不会被操作系统的缺点修改,例如无法处理超过255 个字符的路径。
  2. 如果通过命令行工具删除了该文件夹,它可能仍然看起来好像该文件夹存在于资源管理器中,但在尝试访问它时会出现许多权限错误。
  3. 根据您的防病毒和/或本地策略管理器,您可能能够创建node_modules文件夹,然后将访问权限或权限降级到它,从而导致许多安装错误。
  4. 启用 npm 日志以进一步了解可能存在的问题:

    npm install --loglevel verbose

Radical

激进的

Install rimrafglobally

全局安装rimraf

 npm install rimraf -g

Run rimraf on node_modules:

在 node_modules 上运行rimraf

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.

或缺乏那里。对命令后面的内容要格外小心rimraf。没有警告,没有提示,什么都没有。它只是从地球清洁阶段中删除目录,就好像它从未存在过一样。请自行承担风险尝试。