'Sharp JS Dependency breaks Express Server on Elastic Beanstalk
I feel like this is useless as my conundrum has been discussed in several different threads, but nothing has worked.
I have an ExpressJS/node server deployed to AWS Elastic Beanstalk. When I first tried to deploy several weeks ago, I couldn't get it running until I finally realized one of my many dependencies (an amazing image resizing tool called Sharp) was breaking it. I uninstalled it and removed its usage in the server. Everything was fine. But I really need it--it works beautifully when I run the server on my local device.
But when I reinstall and deploy, I get this error:
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'
The majority of answers on the web are to set unsafe-perm=true, as an environment variable, in a file named .npmrc, using a .config file in .ebextensions that gives write permissions to root... Googling anything pertaining to Sharp and Elastic Beanstalk, or my specific errors brings up an endless sea of purple links to me. But nothing has worked.
EDIT: Rather than continuing to fight to get Sharp working, I found an alternative tool called Jimp. Might be less robust than Sharp, but I really just need resizing, and it does that, so if anyone else is pulling their hair out over this issue, consider saving yourself the headache, and go with Jimp.
Solution 1:[1]
Please reference my "workaround" solution provided in the following GitHub Issue (Fails to install on AWS ElasticBeanstalk with node16 #3221) for a full explanation.
Solution:
- Create the following Platform Hooks paths in the root directory of your application bundle.
- .platform/hooks/prebuild
- .platform/confighooks/prebuild
- Create the following bash script (00_npm_install.sh) with execute permissions (chmod +x).
#!/bin/bash
cd /var/app/staging
sudo -u webapp npm install sharp
- Validate the Application Bundle Structure.
Ex. Sample project structure:
~/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
- Deploy the Application!
Hope it helps!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | jceduar |