Gatsby new 安装 sharp 包失败并中断

7

我无法启动一个Gatsby的脚手架项目。执行new命令过程在初始化期间崩溃,导致错误和无法编译的项目。

以下是命令:

> gatsby new gatsby-test

以下过程开始,在尝试安装sharp包时,执行npm安装时失败。
info Creating new site from git:
Cloning into 'gatsby-test'...
remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (10/10), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 2309 (delta 4), reused 10 (delta 4), pack-reused 2299
Receiving objects: 100% (2309/2309), 12.62 MiB | 7.98 MiB/s, done.
Resolving deltas: 100% (1487/1487), done.
success Created starter directory layout
info Installing packages...

> sharp@0.23.2 install /home/developer/projects/gatsby-test/node_modules/sharp
> (node install/libvips && node install/dll-copy && prebuild-install) || (node-gyp rebuild && node install/dll-copy)

info sharp Downloading https://github.com/lovell/sharp-libvips/releases/download/v8.8.1/libvips-8.8.1-linux-x64.tar.gz
fs.js:114
    throw err;
    ^

Error: EACCES: permission denied, copyfile '/tmp/1890-libvips-8.8.1-linux-x64.tar.gz' -> '/home/developer/.npm/_libvips/libvips-8.8.1-linux-x64.tar.gz'
    at Object.copyFileSync (fs.js:1728:3)
    at WriteStream.<anonymous> (/home/developer/projects/gatsby-test/node_modules/sharp/install/libvips.js:104:16)
    at WriteStream.emit (events.js:198:13)
    at lazyFs.close (internal/fs/streams.js:207:14)
    at FSReqWrap.args [as oncomplete] (fs.js:140:20)
make: Entering directory '/home/developer/projects/gatsby-test/node_modules/sharp/build'
  TOUCH Release/obj.target/libvips-cpp.stamp
  CXX(target) Release/obj.target/sharp/src/common.o
In file included from ../src/common.cc:25:0:
/usr/include/vips/vips8:35:10: fatal error: glib-object.h: No such file or directory
 #include <glib-object.h>
          ^~~~~~~~~~~~~~~
compilation terminated.
sharp.target.mk:129: recipe for target 'Release/obj.target/sharp/src/common.o' failed
make: *** [Release/obj.target/sharp/src/common.o] Error 1
make: Leaving directory '/home/developer/projects/gatsby-test/node_modules/sharp/build'
gyp ERR! build error 
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack     at ChildProcess.onExit (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:194:23)
gyp ERR! stack     at ChildProcess.emit (events.js:198:13)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:248:12)
gyp ERR! System Linux 4.15.0-70-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/developer/projects/gatsby-test/node_modules/sharp
gyp ERR! node -v v10.17.0
gyp ERR! node-gyp -v v5.0.5
gyp ERR! not ok 
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.9: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@2.1.2 (node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.1.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! sharp@0.23.2 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.2 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/developer/.npm/_logs/2019-11-18T16_45_16_026Z-debug.log

 ERROR 

Command failed with exit code 1: npm install



  Error: Command failed with exit code 1: npm install

  - error.js:56 makeError
    [lib]/[gatsby-cli]/[execa]/lib/error.js:56:11

  - index.js:114 handlePromise
    [lib]/[gatsby-cli]/[execa]/index.js:114:26

  - next_tick.js:68 process._tickCallback
    internal/process/next_tick.js:68:7

你知道这里发生了什么吗?我看到过去有一些与sharp有关的问题,但据说现在已经是一个关闭和解决的问题。我错过了什么吗?


gatsby 项目维护者推荐使用 yarn 构建项目。npm 经常会导致这些无法解释的构建错误。我知道你应该有选择包管理器的权利。从我的经验来看,自从我使用 yarn 后,我再也没有遇到过这些错误了。 - EliteRaceElephant
1
@EliteRaceElephant 但是所有的文档教程和示例都是使用npm完成的,yarn是官方推荐吗? - aviya.developer
我也通过从npm切换到yarn来解决了这个问题。 - Juliet Rubin
5个回答

5
您可以在终端中运行以下命令以解决此问题...
rm -rf /Users/{username}/.npm/_libvips

brew install vips 

rm -rf node_modules

npm install

3
以下是需要翻译的内容:

以下这行代码:

Error: EACCES: permission denied, copyfile '/tmp/1890-libvips-8.8.1-linux-x64.tar.gz' -> '/home/developer/.npm/_libvips/libvips-8.8.1-linux-x64.tar.gz'

建议您在将 tmp 文件移动到用户本地的 .npm 目录时出现了权限错误。如果不知道哪个用户运行了该命令等更多上下文信息,很难确定原因。


以下是需要翻译的行:

fatal error: glib-object.h: No such file or directory

建议您检查是否正确安装了 sharpvips

npm install sharp

对于Ubuntu:

sudo apt-get install -y libvips-tools

对于Mac:

brew update vips

对于 Arch Linux/Manjaro:

sudo pacman -S libvips

sharp 怎么样?使用 npm view sharp version 进行测试。 - Nick Bull
但是sharp应该全局安装吗? gatsby new <dir>期望目录不存在。我如何将任何npm模块安装到其中? - aviya.developer
另外,尝试运行 sudo gatsby new gatsby-test,看看会发生什么。如果这个命令可以正常运行而没有错误,那么很可能是权限问题。如果是这种情况,我猜测是因为使用 sudo 安装了 Gatsby(例如,sudo npm i -g gatsby)。 - Nick Bull
npm view sharp version outputs: 0.23..3 - aviya.developer
@aviya.developer,如果你的问题与sharp依赖项有关,那么你可能没什么问题。个人建议下一步检查权限是否正确。 - Nick Bull
显示剩余4条评论

1

1
为了解决这个问题,我运行了以下命令:
  • rm -rf node_modules
  • yarn cache clean
  • yarn add --dev yarn-upgrade-all
  • npx yarn-upgrade-all
然后,我重新安装了所有在升级所有软件包的命令中从package.json中删除的必要软件包。 之后,我运行了gatsby buildgatsby develop

0

尝试使用sudo命令。如果您愿意将root权限应用于安装。


请在代码中使用注释来提出此类建议。 - Drashti Kheni

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