Skip Navigation
Show nav
Heroku Dev Center
  • Get Started
  • Documentation
  • Changelog
  • Search
  • Get Started
    • Node.js
    • Ruby on Rails
    • Ruby
    • Python
    • Java
    • PHP
    • Go
    • Scala
    • Clojure
  • Documentation
  • Changelog
  • More
    Additional Resources
    • Home
    • Elements
    • Products
    • Pricing
    • Careers
    • Help
    • Status
    • Events
    • Podcasts
    • Compliance Center
    Heroku Blog

    Heroku Blog

    Find out what's new with Heroku on our blog.

    Visit Blog
  • Log inorSign up
View categories

Categories

  • Heroku Architecture
    • Dynos (app containers)
    • Stacks (operating system images)
    • Networking & DNS
    • Platform Policies
    • Platform Principles
  • Command Line
  • Deployment
    • Deploying with Git
    • Deploying with Docker
    • Deployment Integrations
  • Continuous Delivery
    • Continuous Integration
  • Language Support
    • Node.js
    • Ruby
      • Rails Support
      • Working with Bundler
    • Python
      • Background Jobs in Python
      • Working with Django
    • Java
      • Working with Maven
      • Java Database Operations
      • Working with the Play Framework
      • Working with Spring Boot
      • Java Advanced Topics
    • PHP
    • Go
      • Go Dependency Management
    • Scala
    • Clojure
  • Databases & Data Management
    • Heroku Postgres
      • Postgres Basics
      • Postgres Getting Started
      • Postgres Performance
      • Postgres Data Transfer & Preservation
      • Postgres Availability
      • Postgres Special Topics
    • Heroku Data For Redis
    • Apache Kafka on Heroku
    • Other Data Stores
  • Monitoring & Metrics
    • Logging
  • App Performance
  • Add-ons
    • All Add-ons
  • Collaboration
  • Security
    • App Security
    • Identities & Authentication
    • Compliance
  • Heroku Enterprise
    • Private Spaces
      • Infrastructure Networking
    • Enterprise Accounts
    • Enterprise Teams
    • Heroku Connect (Salesforce sync)
      • Heroku Connect Administration
      • Heroku Connect Reference
      • Heroku Connect Troubleshooting
    • Single Sign-on (SSO)
  • Patterns & Best Practices
  • Extending Heroku
    • Platform API
    • App Webhooks
    • Heroku Labs
    • Building Add-ons
      • Add-on Development Tasks
      • Add-on APIs
      • Add-on Guidelines & Requirements
    • Building CLI Plugins
    • Developing Buildpacks
    • Dev Center
  • Accounts & Billing
  • Troubleshooting & Support
  • Integrating with Salesforce
  • Language Support
  • Node.js
  • Building Node.js Apps with Grunt

Building Node.js Apps with Grunt

English — 日本語に切り替える

Last updated December 09, 2021

Table of Contents

  • Provide a Gruntfile in your project
  • Install Grunt and necessary plugins
  • Specify your Grunt task in a build script
  • Ignore generated files
  • Check the build
  • Deploy to Heroku
  • Summary

Grunt is a generic task-runner for Node.js projects with a huge library of plugins (grunt-contrib-*). By automating tasks, you can streamline processes for your project (and reduce human error on your team).

This article walks through automating Node.js builds with Grunt on Heroku. You can follow along with your own project, or clone our example from GitHub.

Provide a Gruntfile in your project

Grunt is configured via Gruntfile.js, which should exist at the root of your project alongside package.json. For this example, we’ll use a simple Gruntfile:

module.exports = function(grunt) {

  // Project configuration.
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    uglify: {
      options: {
        banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
      },
      build: {
        src: 'src/factorial.js',
        dest: 'build/factorial.min.js'
      }
    }
  });

  // Load the plugin that provides the "uglify" task.
  grunt.loadNpmTasks('grunt-contrib-uglify');

  // Default task(s).
  grunt.registerTask('default', ['uglify']);
};

This configuration provides a single task: uglify, which will build src/factorial.js into a minified build/factorial.min.js.

Install Grunt and necessary plugins

You should always install npm modules locally (without the -g, or global, option), so that all of your project’s dependencies are tracked in package.json. All grunt projects will need grunt and grunt-cli, which you can install like this:

$ npm install grunt grunt-cli

You’ll also need to install any Grunt plugins used by your project. For this example, we’re using grunt-contrib-uglify:

$ npm install --save-exact grunt-contrib-uglify

Specify your Grunt task in a build script

To use Grunt as a build tool, we need the uglify task to be run after npm modules have been installed, but before we start our app. That’s exactly what the build script in the package.json does when deploying on Heroku. Add the following to your package.json:

"scripts": {
    "build": "grunt uglify",
    "start": "node server.js"
  }

Ignore generated files

You shouldn’t track generated files in git; since they depend on already-tracked files, it’s just a recipe for collisions. Add any generated paths to .gitignore so your Grunt tasks don’t trigger changes in your source control:

node_modules
build/*.js

Check the build

You can easily run your automated workflow locally. If you don’t have a project set up, feel free to clone the example:

$ git clone https://github.com/heroku-examples/node-grunt.git
$ cd node-grunt

Now, ensure that the build script generates a minified file in /build:

$ npm install
$ npm run build

> node-grunt@1.0.0 build /Users/jmorrell/workspace/node-grunt
> grunt uglify

Running "uglify:build" (uglify) task
>> 1 file created.

Done, without errors.

Nice!

Deploy to Heroku

$ git push heroku main

If you watch the build output, you’ll see the Grunt task running:

remote:        Running build
remote:
remote:        > node-grunt@1.0.0 build /tmp/build_e414c7cff23f45169616765eae55c51e
remote:        > grunt uglify
remote:
remote:        Running "uglify:build" (uglify) task
remote:        >> 1 file created.
remote:
remote:        Done, without errors.

Summary

Adding grunt functionality is simple: just use the build hook to ensure a grunt task gets executed as part of the build process.

Keep reading

  • Node.js

Feedback

Log in to submit feedback.

Using WebSockets on Heroku with Node.js Deploying a Parse Server to Heroku

Information & Support

  • Getting Started
  • Documentation
  • Changelog
  • Compliance Center
  • Training & Education
  • Blog
  • Podcasts
  • Support Channels
  • Status

Language Reference

  • Node.js
  • Ruby
  • Java
  • PHP
  • Python
  • Go
  • Scala
  • Clojure

Other Resources

  • Careers
  • Elements
  • Products
  • Pricing

Subscribe to our monthly newsletter

Your email address:

  • RSS
    • Dev Center Articles
    • Dev Center Changelog
    • Heroku Blog
    • Heroku News Blog
    • Heroku Engineering Blog
  • Heroku Podcasts
  • Twitter
    • Dev Center Articles
    • Dev Center Changelog
    • Heroku
    • Heroku Status
  • Facebook
  • Instagram
  • Github
  • LinkedIn
  • YouTube
Heroku is acompany

 © Salesforce.com

  • heroku.com
  • Terms of Service
  • Privacy
  • Cookies
  • Cookie Preferences