AWS Elastic Beanstalk – 如何使用npm和webpack构buildbundle JS

我有一个节点js应用程序部署在elasticbeanstalk。 安装和符号链接节点,npm和webpack。 但是在运行npm run build-prod ,它自己调用脚本webpack --config /var/app/current/webpack.prod.config.js 。 以退出状态-2获取以下错误。 如果我也直接运行webpack命令,也会发生同样的情况。 我正在寻找解决scheme。

 [2016-07-26T06:57:36.301Z] INFO [9731] - [Application update app-5c81-160726_122417@24/AppDeployStage0/EbExtensionPostBuild/Infra-EmbeddedPostBuild/postbuild_0_site_web/Command 06_npm_run_build_prod] : Activity execution failed, because: > site-web@1.0.0 build-prod /tmp/deployment/application > webpack --config /var/app/current/webpack.prod.config.js npm ERR! Linux 4.4.14-24.50.amzn1.x86_64 npm ERR! argv "/opt/elasticbeanstalk/node-install/node-v4.4.6-linux-x64/bin/node" "/bin/npm" "run" "build-prod" npm ERR! node v4.4.6 npm ERR! npm v2.15.5 npm ERR! file sh npm ERR! path sh npm ERR! code ELIFECYCLE npm ERR! errno ENOENT npm ERR! syscall spawn sh npm ERR! site-web@1.0.0 build-prod: `webpack --config /var/app/current/webpack.prod.config.js` npm ERR! spawn sh ENOENT npm ERR! npm ERR! Failed at the site-web@1.0.0 build-prod script 'webpack --config /var/app/current/webpack.prod.config.js'. npm ERR! This is most likely a problem with the site-web package, npm ERR! not with npm itself. npm ERR! Tell the author that this fails on your system: npm ERR! webpack --config /var/app/current/webpack.prod.config.js npm ERR! You can get information on how to open an issue for this project with: npm ERR! npm bugs site-web npm ERR! Or if that isn't available, you can get their info via: npm ERR! npm ERR! npm owner ls site-web npm ERR! There is likely additional logging output above. npm ERR! Linux 4.4.14-24.50.amzn1.x86_64 npm ERR! argv "/opt/elasticbeanstalk/node-install/node-v4.4.6-linux-x64/bin/node" "/bin/npm" "run" "build-prod" npm ERR! node v4.4.6 npm ERR! npm v2.15.5 npm ERR! code ELIFECYCLE 

容器configuration文件:

 container_commands: 01_node_symlink: command: "ln -sf `ls -td /opt/elasticbeanstalk/node-install/node-* | head -1`/bin/node /bin/node" 02_npm_symlink: command: "ln -sf `ls -td /opt/elasticbeanstalk/node-install/node-* | head -1`/bin/npm /bin/npm" 03_npm_install_global_packages: command: "npm install webpack webpack-cli -g" 04_webpack_symlink: command: "ln -sf `ls -td /opt/elasticbeanstalk/node-install/node-* | head -1`/bin/webpack /bin/webpack" #05_webpack_run_build_prod: #command: "webpack --config /var/app/current/webpack.prod.config.js --progress --colors" 06_npm_run_build_prod: command: "npm run build-prod" 

package.json中的脚本

 "scripts": { "build": "webpack --config webpack.local.config.js --progress --colors", "build-local": "webpack --config webpack.prod.config.js --progress --colors", "build-prod": "webpack --config /var/app/current/webpack.prod.config.js", "server": "node app.js", "dev-server": "node dev-app.js" } 

而当我取消注释05是直接运行webpack命令它以错误结束Error: "/var/app/current/site-web/static/assets/app/index.js" is not in the SourceMap.

构build脚本在本地是成功的,但在生产的所有方面都被阻塞了。 无法弄清楚如何运行webpack命令在AWS beanstalk环境中构buildJS。 不是构buildJS文件的理想方法吗?

节点:4.4.6 npm:2.15.5 webpack:最新

我对弹性beanstalk还有一些新的认识,但是我注意到你的任务是用symlinking节点,npm等等,都是以container_commands的forms出现的。

根据官方文档 ,“在应用程序和Web服务器build立之后运行,应用程序版本文件已被提取,但在应用程序版本部署之前运行”。

也许尝试使用commands:而不是container_commands: 请参阅我分享的链接。

也许事实上,这些命令不运行,直到应用程序和Web服务器已经build立之后,为什么它可能不适合你?

再次,我还是新的弹性豆茎,但也许可以帮助。

我遇到了全局的npm安装问题(在我的例子中是react-scripts)。 我做的一个解决方法是安装package.json声明的相关软件包,然后在.ebextensionsconfiguration文件中对其进行符号链接(位于EC2上的.ebextensions ):

  03_react_scripts_symlink: command: "ln -sf /tmp/deployment/application/node_modules/.bin/react-scripts /bin/react-scripts" 04_npm_run_build_prod: command: "sudo npm run build" 

最后,我设法在临时目录中运行构build脚本。 这不是一个永久的解决scheme,但它的工作。

 05_webpack_run_build_prod: command: "cd /tmp/deployment/application && sudo webpack --config webpack.prod.config.js --progress --colors"