chunyenHuang / hummusRecipe

A powerful PDF tool for NodeJS based on HummusJS.
https://hummus-recipe.s3.amazonaws.com/docs/Recipe.html
MIT License
339 stars 91 forks source link

Installing on AWS elastic beanstalk #51

Open curtisnn opened 6 years ago

curtisnn commented 6 years ago

Not sure where to put this, as it isn't an issue, just some lessons learned of what I did to get hummus-recipe to work within a NodeJS instance on AWS elastic beanstalk. Hopefully it saves someone some time. There may be advice for handling it better, which I would appreciate.

To install the package I simply ran npm install --save hummus-recipe

When I then deployed to amazon AWS, I got an error when it was trying to install npm that looked like this: (visible in var/log/eb-activity.log)

> hummus@1.0.88 install /tmp/deployment/application/node_modules/hummus
> node-pre-gyp install --fallback-to-build

  node-pre-gyp WARN Pre-built binaries not installable for hummus@1.0.88 and node@8.8.1 (node-v57 ABI, glibc) (falling back to source compile with node-gyp) 
  node-pre-gyp WARN Hit error EACCES: permission denied, mkdir '/tmp/deployment/application/node_modules/hummus/binding' 
  gyp ERR! configure error 
  gyp ERR! stack Error: EACCES: permission denied, mkdir '/tmp/deployment/application/node_modules/hummus/build'

It seems that node-pre-gyp is run as the default user rather than root so didn't have the ability to mkdir. See:

To provide permissions I added a .ebextensions config file: 01_npmrc.config (make sure indentation is correct for YAML file)

files:
    # This is the npm user config file path.
    "/tmp/.npmrc":
        mode: "000755"
        owner: root
        group: root
        content: |
            # Force npm to run node-gyp also as root, preventing permission denied errors
            unsafe-perm=true

When I submitted this change, it was able to install npm but crashed my server with the error here:

stack: 
   [ 'Error: Cannot find module \'jpgjs\'',
     '    at Function.Module._resolveFilename (module.js:513:15)',
     '    at Function.Module._load (module.js:463:25)',
     '    at Module.require (module.js:556:17)',
     '    at require (internal/module.js:11:18)',
     '    at /var/app/current/node_modules/utif/UTIF.js:10:74',
     '    at Object.<anonymous> (/var/app/current/node_modules/utif/UTIF.js:911:3)',
     '    at Module._compile (module.js:612:30)',
     '    at Object.Module._extensions..js (module.js:623:10)',
     '    at Module.load (module.js:531:32)',
     '    at tryModuleLoad (module.js:494:12)',
     '    at Function.Module._load (module.js:486:3)',
     '    at Module.require (module.js:556:17)',
     '    at require (internal/module.js:11:18)',
     '    at Object.<anonymous> (/var/app/current/node_modules/jimp/dist/utils/image-bitmap.js:22:36)',
     '    at Module._compile (module.js:612:30)',
     '    at Object.Module._extensions..js (module.js:623:10)' ] }
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! Test1@0.1.0 start: `node server.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the Test1@0.1.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

AWS kept trying to restart node, so this message continually popped up. (as seen in /var/log/nodejs/nodejs.log. I checked the node_modules folder on the ec2 instance and sure enough the jpgjs folder was missing. This package is required by the utif library used by hummus which is used by hummus-recipe as shown in the package-lock.json after installing hummus-recipe

"utif": {
      "version": "2.0.0",
      "resolved": "https://registry.npmjs.org/utif/-/utif-2.0.0.tgz",
      "integrity": "sha512-9fl1Md7tUTsgjhWCLOra+nalQnDxWme+h0OB7WQsUBZbhrxEmzL6/suCPxI4ujrFAvv6KSu7B/74HhxfydVpfw==",
      "requires": {
        "jpgjs": "github:makr28/jpgjs#c83f107ad725b476a3441d20680a02590d8752cc",
        "pako": "^1.0.5"
      },
...
"jpgjs": {
      "version": "github:makr28/jpgjs#c83f107ad725b476a3441d20680a02590d8752cc",
      "from": "github:makr28/jpgjs"
    },

I am uncertain as to why it didn't install that package? However, to solve that issue, I downloaded the jpgjs folder from https://github.com/makr28/jpgjs and added it to a libs folder in my own repository. Then I added this postinstall script (within the scripts section of package.json) to copy the folder to node_modules.

"postinstall": "cp -R libs/jpgjs node_modules"

Perhaps not so elegant a solution ... but alas, it deploys to AWS elastic beanstalk without any issues! Yay!

chunyenHuang commented 6 years ago

@curtisnn Thanks for sharing. Since I always use docker so I havent seen this issue before. It's good to know. I have added this to FAQ in wiki page

nikodunk commented 5 years ago

For anyone else running in to this permission issue, I solved this on Elastic Beanstalk with this solution script mentioned above or with more detail at https://github.com/ember-fastboot/fastboot-aws/issues/4#issuecomment-272497834 and nothing else – didn't need the postscripts. Didn't run into the jpjs crash described above.

The NPMRC trick from https://stackoverflow.com/questions/46001516/beanstalk-node-js-deployment-node-gyp-fails-due-to-permission-denied/46001517#46001517 didn't solve the permission trick on EB for me, but didn't try it separate from the Elastic Beanstalk script in the paragraph above so can't be 100% sure that it had no effect.

Dockerization didn't solve the permission issue for me.