How to Use Grunt in Magento 2

In Magento 2, Grunt serves as a powerful automation tool that streamlines front-end development by handling repetitive tasks that would otherwise require manual intervention.

Instead of manually refreshing, recompiling, and cleaning folders every time you make a change to a design file, Grunt does it for you in the background.

It primarily bridges the gap between development and browser compatibility by automatically compiling Less files into readable CSS whenever a change is saved, while its “Watch” functionality keeps the system active to detect edits, recompile only the necessary files, and refresh the browser in real time.

Beyond styling, Grunt maintains environment health through its “clean” command, which instantly wipes cached or “stuck” static files from the pub/static and var/view_preprocessed directories to ensure the latest code is always visible. 

Additionally, it enforces code quality by running JavaScript linting, automatically checking for syntax errors to ensure all custom scripts comply with Magento’s coding standards.

Let us go through the steps to use Grunt in Magento 2

Steps to use Grunt in Magento 2 

To implement Grunt in your Magento 2 development workflow, follow the guide below.

Phase 1: Environment Setup

Before Magento can utilize Grunt, you must have to establish the necessary runtime environment on your local machine.

Step 1: Install Node.js – Begin by installing a stable version of Node.js from the official website. This acts as the engine for all JavaScript-based automation tools.

Step 2: Deploy Grunt CLI – To run Grunt from any directory in your terminal, install the Command Line Interface (CLI) globally using the following command:

npm install -g grunt-cli

Step 3: Initialize Project Dependencies – Navigate to your Magento root directory to install and update the required Node.js packages:

cd <your_Magento_root_directory>

npm install

npm update

Phase 2: Theme Configuration

Magento needs to know which theme Grunt should monitor. You must register your theme in the Grunt configuration file located at dev/tools/grunt/configs/themes.js.

Add your theme details to the module.exports section using this structure:

module.exports = {

    ...

    <theme_name>: {

        area: 'frontend',

        name: '<Vendor>/<theme>',

        locale: 'en_US',

        files: [

            'css/styles-m', // Path to root source files

            'css/styles-l'

        ],

        dsl: 'less'

    },

    ...

};

Configuration Details:

  • <theme_name>: Use your theme’s directory name as the unique key.
  • locale: Set your store’s primary language (e.g., en_US). Note that Grunt only processes one locale at a time.
  • files: Specify the paths to your root source files, relative to web/ in your theme folder. Ensure you include all primary Less files.

Phase 3: Execution & Commands

Once configured, you can use the following commands to automate your front-end tasks:

  • grunt clean: Clears the theme-specific static files within the pub/static and var directories to ensure a fresh build.
  • grunt exec: Generates symlinks for your source files into the pub/static directory, allowing Magento to reference your theme assets.
  • grunt less: Processes the symlinks to compile all .less files into executable .css files.
  • grunt watch: The most used command for active development. It monitors your .less files for changes; upon saving, it automatically recompiles the CSS and notifies you in the terminal, keeping your store’s design updated in real-time.

In summary, integrating Grunt into your Magento 2 workflow transforms front-end development from a manual, repetitive process into an automated, high-efficiency system. 

Sanjay Jethva

Article by

Sanjay Jethva

Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe. His passion for Magento 2 and Shopify solutions has made him a trusted source for...