Search code examples
javascriptexpressherokupackage.jsonenvironment

Heroku - Deploy in Dev or Production


I have an express API which I am hosting on Heroku. I created two projects: one project points at my Master Branch (Prod) and the other at my Development branch (Dev).

I have two scripts in my package JSON:

  "scripts": {
    "devStart": "export NODE_ENV=development && nodemon server.js",
    "start": "export NODE_ENV=production && node server.js"
  }

How can I make it so, that the development branch runs "DevStart" and Master runs "start". Currently Prod is working fine as node start is the default script.

I understand I can put commands in my procfile, however since both Dev and Prod use the same codebase, I would need to change this with each commit. Is there a dynamic way to do this?


Solution

  • Ok, This might not be the correct way but here is how I did it.

    I seen you can set the NODE_ENV variable for the Heroku app using terminal:

    heroku config:set NODE_ENV=development --app {app-name}

    My package JSON though was overwriting this when the app deployed.

      "scripts": {
        "devStart": "export NODE_ENV=development && nodemon server.js",
        "start": "export NODE_ENV=production && node server.js"
      },
    

    I changed it to:

      "scripts": {
        "localStart": "export NODE_ENV=development && nodemon server.js",
        "start": "node server.js"
      },
    

    This now resolves the problem. No need for the Procfile or to setup environment variables within Heroku.

    On Heroku I have two apps, one for production which I don't change the NODE_ENV variable as it defaults to production, and another for Development, which I do change the NODE_ENV variable to 'Development'.

    Each app is hooked up to a different branch within the same code base ( Development(Development) & Master(Production) ). Any deploys to either branch will cause a rebuild using the correct NODE_ENV variable for each respective branch.