如何为AWS Elastic Beanstalk部署运行npm脚本?

问题描述 投票:2回答:2

我的package.json具有:

  "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"
  },

当我部署到Elastic Beanstalk时,我想运行postinstall ,但是显然它没有这样做。 好的没问题。

我创建了一个名为.ebextensions/00.decrypt.config的文件,该文件具有:

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

但是,这似乎也没有运行。 我做错了什么?

node.js amazon-web-services elastic-beanstalk
2个回答
2
投票

我想出了解决此问题的方法。 EB实例上的npm二进制文件位于/opt/elasticbeanstalk/node-install/node-{version} 。 您应该先确保它出现在PATH

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

如您所见,我将附加到.bash_profile并将PATH添加到当前shell。 前者应足以满足您的目的。 我在package.json的脚本中使用npm命令时添加了第二个脚本,似乎这些脚本在同一shell中运行。 TL / DR:您现在应该可以在两个地方使用npm了。

至于您的npm脚本,请尝试使用prestart而不是postinstall


1
投票

一些建议:

  • 尝试将命令括在引号中,这是必需的
  • 另外,不确定$ NODE_HOME是否正常工作-您可以运行简单的测试,例如echo $ NODE_HOME> /tmp/test.txt吗?
© www.soinside.com 2019 - 2024. All rights reserved.