Install sharp without GitHub fetch for Nextjs: Error: Cannot find module 'sharp'

Install sharp without GitHub fetch for Nextjs: Error: Cannot find module 'sharp'

我正在尝试在我的公司系统上使用 Next.js,但每当我 运行“npm 运行 build”时,构建都会失败并显示消息:Error: Cannot find module 'sharp'

我尝试安装 sharp,但出现此错误:

info sharp Downloading https://github.com/lovell/sharp-libvips/releases/download/v8.10.5/libvips-8.10.5-win32-x64.tar.br 
ERR! sharp getaddrinfo ENOTFOUND github.com 
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/install for required dependencies

这可能是因为 github 网址出于安全原因在我的系统终端上被阻止。有没有办法安装 sharp 而无需从 github 获取?我可以在浏览器上访问 GitHub,所以如果从那里下载文件然后以某种方式安装它是可能的,我可以做到这一点。

OS: Windows 10

我有同样的错误。它发生在我身上,因为我有 Apple M1 Mac。我解决了安装 Homebrew 的错误,之后,我不得不使用 Brew install vips 安装 'lipvips'。然后,在我的项目中,npm install sharp,最后,我 运行 npm run build,它对我来说工作得很好。我希望它对你有用!

更新NextJS v10.0.8 fixes this issue!如果您 运行 出现上述错误:升级到 10.0.8(或更高版本)。

NextJS 10.0.5 引入的必需 sharp 依赖项破坏了依赖于底层 libvips 的构建,这在各种 Linux 发行版中都不存在。他们正在努力使依赖项成为可选的。相关 PR here and here. Related issue here。这可能比尝试在您的机器上安装或编译 libvips 更容易(我根本无法让它在我的基于 Amazon EC2 ARM 的机器上运行)。您可以同时降级到 NextJS 10.0.3。我想他们很快就会修好。

我在 mac m1 上遇到了同样的问题。我将节点版本降级到 14,现在可以使用了