NPM doesn't install module dependencies when deploying a Grunt app to heroku

前端 未结 3 924
无人及你
无人及你 2021-02-15 12:14

I\'v made a static single page site using grunt. I\'m now trying to deploy it to heroku using the heroku-buildpack-nodejs-grunt for node grunt.

Below is a pic of my roo

3条回答
  •  花落未央
    2021-02-15 13:11

    I am coming pretty late to the game here but I have used a couple methods and thought I would share.

    Option 1: Get Heroku to Build

    This is not my favorite method because it can take a long time but here it is anyway.

    Heroku runs npm install --production when it receives your pushed changes. This only installs the production dependencies.

    You don't have to change your environment variables to install your dev dependencies. npm install has a --dev switch to allow you to do that.

    npm install --dev
    

    Heroku provides an article on how you can customize your build. Essentially, you can run the above command as a postinstall script in your package.json.

    "scripts": {
      "start": "node index.js",
      "postinstall": "npm install --dev && grunt build"
    }
    

    I think this is cleaner than putting dev dependencies in my production section or changing the environment variables back and forth to get my dependencies to build.

    Also, I don't use a Procfile. Heroku can run your application by calling npm start (at least it can now almost two years after the OP). So as long as you provide that script (as seen above) Heroku should be able to start your app.

    As far as your ruby dependency, I haven't attempted to install a ruby gem in my node apps on Heroku but this SO answer suggests that you use multi buildpack.

    Option 2: Deploy Your Dependencies

    Some argue that having Heroku build your application is bad form. They suggest that you should push up all of your dependencies. If you are like me and hate the idea of checking in your node_modules directory then you could create a new branch where you force add the node_modules directory and then deploy that branch. In git this looks like:

    git checkout -b deploy
    git add -f node_modules/
    git commit -m "heroku deploy"
    git push heroku --force deploy:master
    git checkout master
    git branch -D deploy
    

    You could obviously make this into a script so that you don't have to type that every time.

    Option 3: Do It All Yourself

    This is my new favorite way to deploy. Heroku has added support for slug deploys. The previous link is a good read and I highly recommend it. I do this in my automated build from Travis-CI. I have some custom scripts to tar my app and push the slug to Heroku and its fast.

提交回复
热议问题