我无法使用 npm 安装Sharp

Posted

技术标签:

【中文标题】我无法使用 npm 安装Sharp【英文标题】:I cant install sharp using npm 【发布时间】:2020-02-11 12:16:39 【问题描述】:

我无法使用sudo npm install sharp 安装Sharp 库。

终端输出:

sharp@0.23.1 安装 /Users/kozakiewicz/node_modules/sharp (节点安装/libvips && 节点安装/dll 复制 && 预构建安装) || (node-gyp 重建 && 节点安装/dll-copy)

info sharp Using cached /Users/kozakiewicz/.npm/_libvips/libvips-8.8.1-darwin-x64.tar.gz
ERR! sharp EACCES: permission denied, mkdir '/Users/kozakiewicz/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 '/Users/kozakiewicz/node_modules/sharp/build'
gyp ERR! System Darwin 18.7.0
gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /Users/kozakiewicz/node_modules/sharp
gyp ERR! node -v v10.16.3
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok 
npm WARN enoent ENOENT: no such file or directory, open '/Users/kozakiewicz/package.json'
npm WARN gatsby-plugin-sass@2.1.14 requires a peer of gatsby@^2.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN gatsby-plugin-sass@2.1.14 requires a peer of node-sass@^4.9.0 but none is installed. You must install peer dependencies yourself.
npm WARN sass-loader@7.3.1 requires a peer of webpack@^3.0.0 || ^4.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN gatsby-plugin-sharp@2.2.27 requires a peer of gatsby@^2.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN gatsby-source-contentful@2.1.36 requires a peer of gatsby@^2.0.33 but none is installed. You must install peer dependencies yourself.
npm WARN gatsby-source-filesystem@2.1.22 requires a peer of gatsby@^2.2.0 but none is installed. You must install peer dependencies yourself.
npm WARN gatsby-transformer-sqip@2.1.25 requires a peer of gatsby@^2.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN gatsby-transformer-sqip@2.1.25 requires a peer of gatsby-transformer-sharp@^2.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN sass-loader@7.1.0 requires a peer of webpack@^3.0.0 || ^4.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN kozakiewicz No description
npm WARN kozakiewicz No repository field.
npm WARN kozakiewicz No README data
npm WARN kozakiewicz No license field.

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! sharp@0.23.1 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.23.1 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!     /Users/kozakiewicz/.npm/_logs/2019-10-14T21_07_02_376Z-debug.log

【问题讨论】:

您使用 sudo 弄乱了已安装的 npm 包的权限。不要将 sudo 与 npm 一起使用。无论如何,如果您在项目中安装了sharp local,您现在可以绕过Users/kozakiewicz/node_modules 中的损坏安装。 【参考方案1】:

使用 rm -rf node_modules 删除所有依赖项,同时删除 /home/USER/.npm/_libvips/libvips-8.8.1-linux-x64.tar.gz 并重试 这应该可以解决。

【讨论】:

【参考方案2】:

这有几个可能的原因,让我印象深刻的是节点版本。

我将node --version 设置为14.0.0,它应该与环境中的设置相同。

    安装nvm(节点版本管理器) 查找您需要使用的节点版本 nvm use 10 或将 10 替换为您的节点版本值。

【讨论】:

【参考方案3】:

从工作机器上复制一个 libvips-8.8.1-linux-x64.tar.gz 并粘贴到 /home/USER/.npm/_libvips/ 解决了这个问题

【讨论】:

【参考方案4】:

也许您正在使用root 用户运行。

试试npm config -g set unsafe-perm true,虽然有点危险

【讨论】:

【参考方案5】:

使用 sudo 运行您的命令,例如 sudo [您的命令]

【讨论】:

以上是关于我无法使用 npm 安装Sharp的主要内容,如果未能解决你的问题,请参考以下文章

我无法使用 npm 安装包裹

无法使用 npm 安装 @angular/cli

我无法使用 npm 命令行安装任何东西

无法使用 npm 安装任何东西(反应)

为啥某些依赖项无法使用 npm 安装,而是使用 bower 安装,反之亦然

无法使用 NPM 安装“discord-music-bot”