简体   繁体   English

heroku的构建脚本失败,但是heroku本地网络很好

[英]heroku failed at the build script but heroku local web is fine

I followed instructions, pushed repo to heroku and I find it crashed. 我遵循指示,将回购推送到heroku,发现它崩溃了。 I use heroku/nodejs as a buildpack. 我将heroku / nodejs用作buildpack。

Procfile: 程序文件:

web: npm run build && npm run heroku-server

Scripts: 脚本:

  "scripts": {
    "heroku-server": "cross-env SERVER_PROD_PORT=$PORT SERVER_PROD_HOST=0.0.0.0 npm run server",
    "browser": "cross-env NODE_ENV=development WEBPACK_CONFIG=browser_dev webpack-dev-server --open",
    "build": "cross-env NODE_ENV=production WEBPACK_CONFIG=browser_prod,server_prod webpack --colors",
    "build-analyze": "cross-env BUNDLE_ANALYZER=1 NODE_ENV=production WEBPACK_CONFIG=browser_prod,server_prod webpack --colors",
    "build-browser": "cross-env NODE_ENV=production WEBPACK_CONFIG=browser_prod webpack --colors",
    "build-run": "npm run build && npm run server",
    "build-static": "cross-env NODE_ENV=production WEBPACK_CONFIG=static webpack --colors",
    "build-static-run": "npm run build-static && npm run static",
    "clean": "rimraf dist",
    "lint": "eslint .",
    "server": "node dist/server",

When I run scripts from Procfile or $ heroku local web everything is builing fine. 当我从Procfile或$ heroku本地网络运行脚本时,一切正常。 But pushed repo is throwing the errors in logs: 但是推送回购会在日志中抛出错误:

2018-01-30T22:23:05.780537+00:00 heroku[web.1]: Starting process with command `npm run build && npm run heroku-server`
2018-01-30T22:23:07.502982+00:00 heroku[web.1]: State changed from starting to crashed
2018-01-30T22:23:07.491558+00:00 heroku[web.1]: Process exited with status 1
2018-01-30T22:23:07.292672+00:00 app[web.1]:
2018-01-30T22:23:07.292693+00:00 app[web.1]: > beers@2.10.0 build /app
2018-01-30T22:23:07.292694+00:00 app[web.1]: > cross-env NODE_ENV=production WEBPACK_CONFIG=browser_prod,server_prod webpack --colors
2018-01-30T22:23:07.292695+00:00 app[web.1]:
2018-01-30T22:23:07.421065+00:00 app[web.1]: events.js:137
2018-01-30T22:23:07.421068+00:00 app[web.1]:       throw er; // Unhandled 'error' event
2018-01-30T22:23:07.421070+00:00 app[web.1]:       ^
2018-01-30T22:23:07.421071+00:00 app[web.1]:
2018-01-30T22:23:07.421073+00:00 app[web.1]: Error: spawn webpack ENOENT
2018-01-30T22:23:07.421074+00:00 app[web.1]:     at _errnoException (util.js:1003:13)
2018-01-30T22:23:07.421076+00:00 app[web.1]:     at Process.ChildProcess._handle.onexit (internal/child_process.js:201:19)
2018-01-30T22:23:07.421078+00:00 app[web.1]:     at onErrorNT (internal/child_process.js:389:16)
2018-01-30T22:23:07.421080+00:00 app[web.1]:     at process._tickCallback (internal/process/next_tick.js:152:19)
2018-01-30T22:23:07.421081+00:00 app[web.1]:     at Function.Module.runMain (module.js:703:11)
2018-01-30T22:23:07.421083+00:00 app[web.1]:     at startup (bootstrap_node.js:193:16)
2018-01-30T22:23:07.421085+00:00 app[web.1]:     at bootstrap_node.js:617:3
2018-01-30T22:23:07.426513+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2018-01-30T22:23:07.426855+00:00 app[web.1]: npm ERR! errno 1
2018-01-30T22:23:07.428042+00:00 app[web.1]: npm ERR! beers@2.10.0 build: `cross-env NODE_ENV=production WEBPACK_CONFIG=browser_prod,server_prod webpack --colors`

What am I missing? 我想念什么?

First of all, don't put your "npm run build" step in your Procfile. 首先,不要在Procfile中放置“ npm run build”步骤。 Doing so will cause that step to run every time your web dyno restarts , which is not what you want. 这样做会使该步骤在您的Web dyno每次重新启动时都运行,这不是您想要的。

Instead, you want to run webpack (or whatever else you need for making sure the dependencies you need are present in your app) during slug compilation . 相反,您希望在slug编译过程中运行webpack(或确保应用程序中存在所需依赖项所需的任何其他操作)。

By virtue of the fact that you are using the heroku/node.js buildpack , it will automatically take care of running "npm install" for you (or yarn install, if you choose to use the yarn package manager instead). 由于您使用的是heroku / node.js buildpack ,因此它将自动为您运行“ npm install”(如果您选择使用yarn包管理器 ,则可以执行yarn install)。

However, if you need more than npm/yarn install (eg if you need to run webpack to bundle client dependencies), then you need to provide the slug compiler with additional instructions for those steps. 但是,如果您需要安装npm / yarn以上(例如,如果需要运行webpack来捆绑客户端依赖项),则需要为slug编译器提供这些步骤的附加说明。 A good way to do that is by using heroku-specific build steps . 做到这一点的一种好方法是使用特定heroku的构建步骤

So, for example, you might want to run webpack in a heroku-postbuild step in your package.json "scripts", eg: 因此,例如,您可能想在package.json“脚本”中的heroku-postbuild步骤中运行webpack,例如:

"heroku-postbuid" : "cross-env NODE_ENV=production WEBPACK_CONFIG=browser_prod,server_prod webpack --colors",

However, in addition you need to be aware that by default Heroku won't install any of your package.json devDependencies. 但是,此外,您还需要注意,默认情况下,Heroku不会安装任何package.json devDependencies。 So if you have webpack itself listed in devDependencies, you won't be able to use it in a heroku-postbuild step by default. 因此,如果在devDependencies中列出了webpack本身,则默认情况下将无法在heroku-postbuild步骤中使用它。

The way to workaround that is to either move webpack (together with any other build dependencies you need) to "dependencies" (instead of "devDependencies"), or to set config var NPM_CONFIG_PRODUCTION to false: 解决方法是将webpack(以及所需的任何其他构建依赖项)移动到“ dependencies”(而不是“ devDependencies”),或将config var NPM_CONFIG_PRODUCTION设置为false:

heroku config:set NPM_CONFIG_PRODUCTION=false   

For more info, see here and here . 有关更多信息,请参见此处此处

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM