Sharp JS 依赖性破坏了 Elastic Beanstalk 上的 Express Server

Sharp JS Dependency breaks Express Server on Elastic Beanstalk

我觉得这没用,因为我的难题已经在几个不同的话题中讨论过,但没有任何效果。

我有一个 ExpressJS/node 服务器部署到 AWS Elastic Beanstalk。几个星期前,当我第一次尝试部署时,我无法 运行ning 直到我终于意识到我的众多依赖项之一(一个名为 Sharp 的神奇图像大小调整工具)正在破坏它。我卸载了它并删除了它在服务器中的使用。一切都很好。但我真的需要它——当我 运行 我的本地设备上的服务器时它工作得很好。

但是当我重新安装和部署时,我得到这个错误:

npm ERR! path /var/app/staging/node_modules/sharp
npm ERR! command failed
npm ERR! command sh -c (node install/libvips && node install/dll-copy && prebuild-install) || (node install/can-compile && node-gyp rebuild && node install/dll-copy)
npm ERR! sharp: Are you trying to install as a root or sudo user? Try again with the --unsafe-perm flag
npm ERR! sharp: Please see https://sharp.pixelplumbing.com/install for required dependencies
npm ERR! sharp: Installation error: EACCES: permission denied, mkdir '/root/.npm'

网络上的大多数答案是在名为 .npmrc 的文件中设置 unsafe-perm=true 作为环境变量,使用 .ebextensions 中的 .config 文件为 root 提供写入权限...在谷歌上搜索任何与 Sharp 和 Elastic Beanstalk 有关的内容,或者我的特定错误都会给我带来无穷无尽的紫色链接。但是没有任何效果。

编辑:我没有继续努力让 Sharp 正常工作,而是找到了一个名为 Jimp 的替代工具。可能不如 Sharp 健壮,但我真的只需要调整大小,它就是这样做的,所以如果其他人因为这个问题而烦恼,考虑让自己省去头疼,然后选择 Jimp。

请参考我在以下 GitHub 问题 (Fails to install on AWS ElasticBeanstalk with node16 #3221) 中提供的“解决方法”解决方案以获得完整解释。

解法:

  1. 在应用程序包的根目录中创建以下 Platform Hooks 路径。
  • .platform/hooks/prebuild
  • .platform/confighooks/prebuild
  1. 创建以下具有执行权限 (chmod +x) 的 bash 脚本 (00_npm_install.sh)。
#!/bin/bash
cd /var/app/staging
sudo -u webapp npm install sharp
  1. 验证应用程序包结构。

例如。示例项目结构:

~/my-app/
├── app.js
├── index.html
├── .npmrc_bkp
├── package.json
├── package-lock.json
├── .platform
│   ├── confighooks
│   │   └── prebuild
│   │       └── 00_npm_install.sh
│   └── hooks
│       └── prebuild
│           └── 00_npm_install.sh
└── Procfile
  1. 部署应用程序!

希望对您有所帮助!