我觉得这毫无用处,因为我的难题已在多个不同的线程中讨论过,但没有任何效果。
我有一个 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'
网上的大多数答案都是在名为 .npmrc 的文件中设置 unsafe-perm=true 作为环境变量,使用 .ebextensions 中的 .config 文件为 root 提供写入权限...谷歌搜索与 Sharp 有关的任何内容和弹性豆茎,或者我的特定错误给我带来了无尽的紫色链接海洋。但没有任何效果。
编辑:我没有继续努力让 Sharp 工作,而是找到了一个名为 Jimp 的替代工具。可能不如 Sharp 强大,但我真的只需要调整大小,而它确实做到了这一点,所以如果其他人在这个问题上焦头烂额,考虑一下让自己省去麻烦,然后选择 Jimp。
请参考我在以下 GitHub 问题中提供的“解决方法”解决方案 (无法在 Node16 的 AWS ElasticBeanstalk 上安装 #3221 https://github.com/lovell/sharp/issues/3221#issuecomment-1126528844)以获得完整的解释。
解决方案:
- 在应用程序包的根目录中创建以下平台挂钩路径。
- .platform/hooks/预构建
- .platform/confighooks/预构建
- 创建以下具有执行权限 (chmod +x) 的 bash 脚本 (00_npm_install.sh)。
#!/bin/bash
cd /var/app/staging
sudo -u webapp npm install sharp
- 验证应用程序包结构。
前任。示例项目结构:
~/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
- 部署应用程序!
希望能帮助到你!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)