简体   繁体   English

AWS Elastic Beanstalk - 如何使用npm和webpack构建bundle JS

[英]AWS Elastic Beanstalk - How to build bundle JS using npm and webpack

I have a node js app deployed at elasticbeanstalk. 我在elasticbeanstalk部署了一个节点js app。 Installed and symlinked node, npm and webpack. 已安装且符号链接的节点,npm和webpack。 But when running npm run build-prod which itself calls the script webpack --config /var/app/current/webpack.prod.config.js . 但是当运行npm run build-prod ,它本身调用脚本webpack --config /var/app/current/webpack.prod.config.js Getting the following error with exit status -2. 退出状态-2出现以下错误。 The same happens if I run the webpack command directly too. 如果我直接运行webpack命令也会发生同样的情况。 I'm looking out for solutions. 我正在寻找解决方案。

[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

container config file: 容器配置文件:

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"

Scripts in package.json 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"
  }

And when I uncomment 05 that is run the webpack command directly it ends with error Error: "/var/app/current/site-web/static/assets/app/index.js" is not in the SourceMap. 当我取消注释05直接运行webpack命令时,它以错误结束Error: "/var/app/current/site-web/static/assets/app/index.js" is not in the SourceMap.

The build script is successful at local but blocked on all the ways at production. 构建脚本在本地成功,但在生产的所有方式上都被阻止。 Couldn't figure out how to run webpack command to build the JS on AWS beanstalk environment. 无法弄清楚如何运行webpack命令在AWS beanstalk环境中构建JS。 Isn't the ideal way of building the JS file? 是不是构建JS文件的理想方式?

node: 4.4.6 npm: 2.15.5 webpack: latest node:4.4.6 npm:2.15.5 webpack:latest

I'm still new to elastic beanstalk and having similar issues myself but I noticed your tasks for symlinking node, npm, etc, are in the form of container_commands . 我还是刚接触弹性beanstalk并且自己也有类似的问题,但是我注意到你的符号链接节点,npm等的任务是container_commands的形式。

According to the official docs , "They run after the application and web server have been set up and the application version file has been extracted, but before the application version is deployed." 根据官方文档 ,“它们在应用程序和Web服务器已经设置并且应用程序版本文件已被解压缩但在部署应用程序版本之前运行。”

Maybe try using commands: instead of container_commands: . 也许尝试使用commands:而不是container_commands: . See link I shared. 查看我分享的链接。

Maybe the fact that these commands aren't running until after the application and web server have already been set up is why it may not be working for you? 也许这些命令直到应用程序和Web服务器已经设置之后才运行,这可能是为什么它可能不适合您?

Again, I'm still new to elastic beanstalk but maybe that can help. 再说一次,我还是刚开始使用弹性豆茎,但也许这可能有所帮助。

I had issues with global npm installs (in my case react-scripts). 我遇到了全局npm安装问题(在我看来是react-scripts)。 What I did as a workaround was to install the dependent package as declared in package.json and then symlink to it (located at /tmp/deployment/application/node_modules/.bin on EC2) in the .ebextensions config file: 我所做的解决方法是在宣布安装相关程序包package.json ,然后符号链接到它(位于/tmp/deployment/application/node_modules/.bin在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"

I think the approach is faulty. 我认为这种方法是错误的。 Webpack and Webpack-cli are devDependencies of package.json . WebpackWebpack-clipackage.json devDependencies。
You should set your env variable to NPM_USE_PRODUCTION=false in order for EB to install devDependencies as well. 您应该将env变量设置为NPM_USE_PRODUCTION=false ,以便EB也安装devDependencies

see more here: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/nodejs-platform-packagejson.html 在这里查看更多: https//docs.aws.amazon.com/elasticbeanstalk/latest/dg/nodejs-platform-packagejson.html

Finally, I managed to run the build script at the staging dir. 最后,我设法在staging目录下运行构建脚本。 It's not a permanent solution but it works. 它不是一个永久的解决方案,但它的工作原理。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Elastic Beanstalk AWS 上使用 MySQL 托管节点 js / express 服务器 - How to host a node js / express server with MySQL on Elastic Beanstalk AWS 如何使用Webpack绑定没有npm依赖项的库? - How to bundle library without npm dependencies using webpack? 如何将npm软件包与webpack捆绑在一起并作为对象公开? - How to bundle npm packages with webpack and expose as an object? 使用webpack-dev-server时,webpack build中缺少bundle.js - bundle.js missing from webpack build when using webpack-dev-server 什么是 npm 运行构建命令以及如何使用 webpack 创建捆绑文件 - What is npm run build command and how webpack is used in creating bundle file AWS Elastic Beanstalk-Express.js,更新为empties / uploads文件夹 - AWS Elastic Beanstalk - Express.js, update empties /uploads folder 将节点 js 服务器(在弹性 beanstalk AWS 上)url 更改为 HTTPS - Change Node js server (on elastic beanstalk AWS) url to HTTPS 如何将 html 中的 js 与 webpack 捆绑在一起? - How to bundle js in html with webpack? 如何使用Webpack减少React的bundle.js文件大小 - How to reduce bundle.js file size of react using webpack 试图了解bundle.js如何使用Webpack执行 - Trying to understand how bundle.js executes using Webpack
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM