3

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.

  • 1
    same here: I've created an issue on GitHub https://github.com/lovell/sharp/issues/3221 – guidev May 10 '22 at 17:58
  • 1
    The `unsafe-perm` setting is for npm v6 and earlier. For npm v8 and later, you'll need to ensure the directory in which `npm install` is run is owned by the same user that is running `npm install`. https://docs.npmjs.com/cli/v8/using-npm/scripts#user – Lovell Fuller May 10 '22 at 18:41

1 Answers1

8

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:

  1. Create the following Platform Hooks paths in the root directory of your application bundle.
  • .platform/hooks/prebuild
  • .platform/confighooks/prebuild
  1. 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
  1. 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
  1. Deploy the Application!

Hope it helps!

jceduar
  • 111
  • 1
  • I wound up going in a different direction, but seeing that you found yourself in the exact same situation and got it working, I'll assume that the well-documented solution on Github is solid. Will be coming back to this if/when I want to swap out Jimp for Sharp. – Patrick McCullough May 15 '22 at 15:41
  • I have no idea how, but this worked flawlessly for me. Thank you so much! – Raphael Setin Oct 06 '22 at 10:44