无法安装Sharp

32

在运行sudo npm i sharp --save时,我收到以下错误信息:

> sharp@0.21.3 install /home/server/node_modules/sharp
> (node install/libvips && node install/dll-copy && prebuild-install) || (node-gyp rebuild && node install/dll-copy)

info sharp Using cached /home/ronny/.npm/_libvips/libvips-8.7.0-linux-x64.tar.gz
ERR! sharp EACCES: permission denied, mkdir '/home/server/node_modules/sharp/vendor'
info sharp Attempting to build from source via node-gyp but this may fail due to the above error
info sharp Please see https://sharp.pixelplumbing.com/page/install for required dependencies
gyp ERR! configure error 
gyp ERR! stack Error: EACCES: permission denied, mkdir '/home/server/node_modules/sharp/build'
gyp ERR! System Linux 4.18.0-13-generic
gyp ERR! command "/usr/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /home/server/node_modules/sharp
gyp ERR! node -v v8.11.4
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok 
npm WARN server@1.0.0 No description
npm WARN server@1.0.0 No repository field.

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! sharp@0.21.3 install: `(node install/libvips && node install/dll-copy && prebuild-install) || (node-gyp rebuild && node install/dll-copy)`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the sharp@0.21.3 install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/ronny/.npm/_logs/2019-01-28T20_33_46_476Z-debug.log

我在另一台机器上运行了相同的项目,在那里,安装工作得很好。 但是在我的另一台机器上(都运行Linux Ubuntu),我遇到了上述错误。

我找不到任何解决这个错误的方法。 这就是为什么我在这里寻求帮助的原因。


gyp ERR! stack 错误: EACCES:权限被拒绝,mkdir '/home/server/node_modules/sharp/build' (需要使用SUDO吗?) - Caspar Kleijne
我使用了 sudo 命令,所以应该拥有权限。 - Florian Ludewig
25个回答

37

1
我也发现这篇博客文章对于理解这个问题很有帮助。简单来说,npm在安装模块时尝试切换到用户帐户,因为以root身份安装的模块只能由root进程使用。将常规进程作为root运行被认为是不安全的,因此出现了这个参数名称。 - drali
为了让这对我起作用,我使用以下命令:'pnpm install --ignore-scripts=false --verbose sharp' - undefined

16

实际上这对我有帮助,使用节点14.20.1版本。

npm install sharp --ignore-scripts

14

最简单的方法是:

brew uninstall vips

然后:

npm install

2
我们能否揭示一下它为什么可行呢?为什么会有赞成票? - philipjc

9

对于 Apple M1 电脑,可通过以下命令安装vips:brew install vips 然后运行 npm install --unsafe-perm

如果仍然存在问题,请参考类似问题并执行以下步骤:

  • xcode-select --install
  • brew install gcc
  • brew reinstall vips
  • brew info vips
  • npm i

8

7

Node v13.14.0+存在问题,该问题将得到解决。

npm install sharp@0.23.2 

如果您正在使用 sharp 0.22.1,请将 Node 版本降级至 >=10.0.0。

问题在版本 0.28.3 中仍然存在。 - Mild Fuzz
你需要降级Sharp或Nodejs版本。你需要检查兼容性,因为较高的版本经常会引起冲突。 - Trinh Hieu
你需要降级Sharp或Nodejs版本。你需要检查兼容性,因为较高的版本经常会引起冲突。 - undefined

4

我通过将node版本降级到10.0.0,解决了这个错误。确实有效!


在我的情况下,从v16.13.0降级到v14.16.1解决了这个问题。 - andrew

4

删除 package-lock.jsonnode_modules

然后尝试安装包 npm install

对我有效。


它对我有效, 我的Node版本是v14.17.0 npm版本是7.17.0 sharp版本是"^0.27.0" - asraful009

3

尝试使用yarn而不是npm,对我来说很有效(在docker中,官方node镜像16.x)

yarn add sharp

2
首先,错误提示是“权限被拒绝”。在sharp文档中使用sudo时,建议使用npm install --unsafe-perm。您需要使用sudo安装它。
以下是我的建议:
  • 进入/home/ronny/.npm/_libvips/libvips-8.7.0-linux-x64.tar.gz(与您的错误代码相符)
  • 删除文件:libvips-8.7.0-linux-x64.tar.gz
  • 在终端中键入:npm install --unsafe-perm(这将安装已删除的库)
  • npm install sharp
然后我认为它应该可以正常工作了。这些步骤解决了我的问题,我遇到了相同的问题。希望能有所帮助。谢谢。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接