3

My package.json has:

  "scripts": {
    "start": "node_modules/.bin/coffee server.coffee",
    "test": "NODE_ENV=test node test/runner.js",
    "coverage": "NODE_ENV=test COVERAGE=1 node test/runner.js -R html-cov test/ > ./test/coverage.html",
    "testw": "fswatch -o test src | xargs -n1 -I{} sh -c 'coffeelint src server.coffee ; npm test'",
    "db:drop": "node scripts/drop-tables.js",
    "encryptConfig": "node_modules/.bin/coffee config/encrypt.coffee",
    "decryptConfig": "node_modules/.bin/coffee config/decrypt.coffee",
    "postinstall": "npm run decryptConfig"
  },

When I deploy to Elastic Beanstalk, I'd like to run the postinstall, but apparently it doesn't do that. Okay, no problem.

I created a file called .ebextensions/00.decrypt.config which has:

commands:
  00-add-home-variable:
    command: sed -i 's/function error_exit/export HOME=\/root\n\nfunction error_exit/' /opt/elasticbeanstalk/hooks/appdeploy/pre/50npm.sh

container_commands:
  02-decrypt-config:
    command: $NODE_HOME/bin/npm run decryptConfig

However this doesn't seem to run either. What am I doing incorrectly?

Shamoon
  • 41,293
  • 91
  • 306
  • 570

2 Answers2

2

I figured out a workaround for this issue. The npm binary on an EB instance is located in /opt/elasticbeanstalk/node-install/node-{version}. You should make sure this is present in your PATH first.

00_setpath.config

commands:
  01_set_path:
    command: echo 'export PATH=$PATH:`ls -td /opt/elasticbeanstalk/node-install/node-* | head -1`/bin' >> /root/.bash_profile
  02_set_path:
    command: export PATH=$PATH:`ls -td /opt/elasticbeanstalk/node-install/node-* | head -1`/bin

As you can see I am appending to .bash_profile and also adding PATH to the current shell. The former should be sufficient for your purpose. I added the second one as I'm using the npm command within a script in my package.json, and it seems these scripts are run inside the same shell. TL/DR: You should now be able to use npm in both places.

As for your npm scripts, try using prestart instead of postinstall.

Richard Francis
  • 441
  • 3
  • 5
1

A few suggestions:

  • Try to enclose your commands in quotes, that's a requirement
  • Also, not sure if $NODE_HOME is working - could you run simple test like echo $NODE_HOME > /tmp/test.txt?
sap1ens
  • 2,877
  • 1
  • 27
  • 30