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

Pat*_*ugh 3 node.js express amazon-elastic-beanstalk sharp

我觉得这毫无用处,因为我的难题已在多个不同的线程中讨论过,但没有任何效果。

我有一个 ExpressJS/节点服务器部署到 AWS Elastic Beanstalk。当我几周前第一次尝试部署时,我无法让它运行,直到我最终意识到我的许多依赖项之一(一个名为 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'
Run Code Online (Sandbox Code Playgroud)

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

编辑:我没有继续努力让 Sharp 工作,而是找到了一个名为 Jimp 的替代工具。可能不如 Sharp 强大,但我真的只需要调整大小,而它确实做到了这一点,所以如果其他人在这个问题上焦头烂额,考虑一下让自己省去麻烦,然后选择 Jimp。

小智 8

请参考以下 GitHub 问题中提供的我的“解决方法”解决方案(无法在带有 node16 的 AWS ElasticBeanstalk 上安装 #3221)以获取完整说明。

\n

解决方案:

\n
    \n
  1. 在应用程序包的根目录中创建以下平台挂钩路径。
  2. \n
\n
    \n
  • .platform/hooks/预构建
  • \n
  • .platform/confighooks/预构建
  • \n
\n
    \n
  1. 创建以下具有执行权限 (chmod +x) 的 bash 脚本 (00_npm_install.sh)。
  2. \n
\n
#!/bin/bash\ncd /var/app/staging\nsudo -u webapp npm install sharp\n
Run Code Online (Sandbox Code Playgroud)\n
    \n
  1. 验证应用程序包结构。
  2. \n
\n

前任。示例项目结构:

\n
~/my-app/\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 app.js\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 index.html\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 .npmrc_bkp\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 package.json\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 package-lock.json\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 .platform\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 confighooks\n\xe2\x94\x82   \xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 prebuild\n\xe2\x94\x82   \xe2\x94\x82       \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 00_npm_install.sh\n\xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 hooks\n\xe2\x94\x82       \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 prebuild\n\xe2\x94\x82           \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 00_npm_install.sh\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 Procfile\n
Run Code Online (Sandbox Code Playgroud)\n
    \n
  1. 部署应用程序!
  2. \n
\n

希望能帮助到你!

\n