56

I would like to have Heroku build my app after I push it so that I don't have to push the build folder up every time I make a change. However Heroku only installs the dependencies from the package.json and grunt (my build tool) and all of its components are in devDependencies. I would like to keep them there where they belong. What's the workaround here?

Flimm
  • 136,138
  • 45
  • 251
  • 267
ionox0
  • 1,101
  • 2
  • 14
  • 21
  • 1
    Could you use a custom buildpack like this? https://github.com/gcpantazis/heroku-buildpack-php-gruntjs – catsby Apr 10 '14 at 20:41

7 Answers7

95

UPDATE: as pointed out in the comments this is no more needed because since 2018 heroku changed its default behaviour and dev dependencies are automatically installed

ORIGINAL ANSWER

Heroku by default installs only the production dependencies, ignoring the development dependencies under devDependencies.

Setting the npm production variable to false do the trick:

heroku config:set NPM_CONFIG_PRODUCTION=false

More info are available at the Heroku Node.js Support page.

Edo
  • 1,539
  • 18
  • 26
  • 3
    This sets your NODE_ENV to development, which may slow down your app. – unblevable Aug 18 '16 at 18:16
  • 7
    @unblevable I don't think so: ``` -----> Node.js app detected -----> Creating runtime environment NPM_CONFIG_LOGLEVEL=error NPM_CONFIG_PRODUCTION=false NODE_ENV=production NODE_MODULES_CACHE=true ``` – bithavoc Sep 08 '16 at 02:30
  • 2
    I did heroku config:set NPM_CONFIG_PRODUCTION=false and retarted dyno and the dev dependencies still won't install – Lev Dec 11 '16 at 01:54
  • Sorry, where should I type "heroku config:set NPM_CONFIG_PRODUCTION=false".? I don't use grunt, npm only. Is it possible to define envvars with package.json – croraf Jun 16 '17 at 13:51
  • 1
    @croraf it looks like you're not familiar with heroku CLI https://devcenter.heroku.com/categories/command-line – Edo Jun 19 '17 at 07:08
  • Hi thanks. Yes I thought it is through their cli, which I dont use. But I found a way without it - through settings tab of the heroku application managment web page. – croraf Jun 19 '17 at 10:41
  • 4
    How do you do this in heroku-prebuild so you can keep the process in PRODUCTION? I thought I could call a bash script and `set` it there, but it doesn't install the devdeps that way. – roberto tomás Jul 03 '17 at 15:30
  • 1
    If anyone's seeing this while using yarn, the equivalent is `YARN_PRODUCTION=false` – kylesimmonds Mar 06 '18 at 14:51
  • just like @valentin commented, since March 2018, heroku installs those by default, and vice versa should be done if you don't want it to https://devcenter.heroku.com/changelog-items/1376 – RawKnee Apr 19 '21 at 15:12
19

Keeping NPM_CONFIG_PRODUCTION true, I used Heroku's script hooks:

"scripts": {
  ...
  "heroku-prebuild": "export NPM_CONFIG_PRODUCTION=false; export NODE_ENV=; NPM_CONFIG_PRODUCTION=false NODE_ENV=development npm install --only=dev --dev",
  "heroku-postbuild": "export NPM_CONFIG_PRODUCTION=true; export NODE_ENV=production;",
   ...
},

(Finally) worked for me.

PixnBits
  • 1,150
  • 10
  • 14
  • 1
    I don't know whether it's an npm version thing, but for me the above solution worked with a different flag: `npm install --production=false`. – Moshisho Mar 20 '22 at 14:32
1
scripts": {
  ...
  "heroku-prebuild": "npm install --only=dev"
}

This was enough for me. Thanks to PixnBits for the hint about heroku-prebuild. Also - my problem was with babel. I ended up moving babel-preset-es2015 and other presets into dependencies otherwise babel complained about presets.

Update: 8/11/2017 I've been having trouble with this. It seems like things have changed (and npm is on 5.3 now). But what I see is that the heroku-prebuild script is getting run, and then the post-install script is getting run (but I was only trying to install -dev).

So what I have been doing that works is to just run:

heroku config:set NPM_CONFIG_PRODUCTION=false

And just leave it set that way. I'd love a better solution.

David Fridley
  • 165
  • 1
  • 10
1

To unintall dependencies you need to do these

  1. Update NPM_CONFIG_PRODUCTION

    heroku config variable set

    NPM_CONFIG_PRODUCTION=false

  2. Add heroku-prebuild:

    scripts": {
      ...
      "heroku-prebuild": "npm install"
    }
or

    scripts": {
      ...
      "heroku-prebuild": "npm install --only=dev"
    }
1

you can use this in your build script "build": "npm install --only=dev" should in case you still want to perform more operations e.g transpiling your code with babel you can do something like this "build": "npm install --only=dev && babel src --out-dir dist --copy-files"

otoloye
  • 677
  • 7
  • 11
1

Since 1 March 2018 Heroku installs devDependencies by default, and then prunes them after the build step is done:

By default, Heroku will install all dependencies listed in package.json under dependencies and devDependencies.

After running the installation and build steps Heroku will strip out the packages declared under devDependencies before deploying the application.

Heroku uses the lockfiles, either the package-lock.json or yarn.lock, to install the expected dependency tree, so be sure to check those files into git to ensure the same dependency versions across environments.

Link

Flimm
  • 136,138
  • 45
  • 251
  • 267
valentin
  • 570
  • 7
  • 12
1

I found this highly confusing. Even though Heroku says that their default since 2018 is to install all dependencies, they also set the env var NODE_ENV=production by default. This is good because it causes/allows for pruning, but it is bad because it means NPM will not install devDependencies.

To avoid this without messing with environment variables and their possible side effects, we can append --production=false to npm and it will install dependencies and devDependencies.

In our case, in package.json in scripts we have a line:

"install": "npm i --prefix ... --production=false"

My answer similar to others above with the additional references that seem to explain why it's not actually working by default like Heroku suggests.

John Lehmann
  • 7,975
  • 4
  • 58
  • 71