heroku failed at the build script but heroku local web is fine

后端 未结 1 679
生来不讨喜
生来不讨喜 2020-12-07 05:50

I followed instructions, pushed repo to heroku and I find it crashed. I use heroku/nodejs as a buildpack.

Procfile:

web: npm run build &&          


        
相关标签:
1条回答
  • 2020-12-07 06:47

    First of all, don't put your "npm run build" step in your Procfile. Doing so will cause that step to run every time your web dyno restarts, which is not what you want.

    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.

    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).

    However, if you need more than npm/yarn install (e.g. if you need to run webpack to bundle client dependencies), then you need to provide the slug compiler with additional instructions for those steps. A good way to do that is by using heroku-specific build steps.

    So, for example, you might want to run webpack in a heroku-postbuild step in your package.json "scripts", e.g.:

    "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. So if you have webpack itself listed in devDependencies, you won't be able to use it in a heroku-postbuild step by default.

    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:

    heroku config:set NPM_CONFIG_PRODUCTION=false   
    

    For more info, see here and here.

    0 讨论(0)
提交回复
热议问题