Table of Contents
Grunt is a JavaScriptΒ task runnerΒ which essentially simplifies repetitive tasks by automating compilation, minification, linting, unit testing, etc.
In this article, we are going to understand how to install and configure Grunt for Magento2.
Here are the steps :
Step 1: Install nodejs in our machine
Step 2: Install Grunt CLI
Step 3: Configure In Magento2
Step 4:Β Grunt configuration file
Let’s start the process..
Step1: Install nodejs in our machine
Grunt and Grunt plugins are installed and managed viaΒ npm, theΒ Node.jsΒ package manager.
For Windows:-
Install nodejs – https://nodejs.org/en/
Click to download as shown above. Once download completes, Install it by double clicking on the downloaded file.
Then in the Windows Search box, type node and open it in “Run as administrator”

node -v
For, Linux / Ubuntu:-
sudo apt-get update
sudo apt-get install nodejs
nodejs -v
Step 2: Install Grunt CLI
To install Grunt’s command line interface (CLI) globally. You may need to use sudo (for OSX, *nix, BSD etc) or run your command shell as Administrator (for Windows) to do this.
npm install -g grunt-cliΒ
This will put theΒ grunt
Β command in your system path, allowing it to be run from any directory.
Step 3:Β Working with Magento project
To configure project with Grunt, Go to Magento 2 Root Directory to copy and paste the contents of the following sample files:
- FromΒ package.json.sampleΒ toΒ package.json
- FromΒ Gruntfile.js.sampleΒ toΒ Gruntfile.js
- FromΒ grunt-config.json.sampleΒ intoΒ grunt-config.json
In a command prompt, run the following commands to refresh or install theΒ node.jsΒ project dependency, including Grunt:
cd your_Magento_instance_directory
npm install
npm update
Step 4:Β Grunt configuration file
Copy the contents ofΒ themes.js
Β intoΒ local-themes.js
Β in theΒ dev/tools/grunt/configs/
Β directory.
If installed as described above, Grunt will use the default configuration files located in theΒ dev/tools/grunt/configs/
Β directory. You can define your theme in theΒ local-themes.js
Β file. The following shows an example of how you can define your theme.
<theme>: {
area: '<area>',
name: '<Vendor>/<theme>,
locale: '<language>',
files: [
'<path_to_file1>', //path to root source file
'<path_to_file2>'
],
dsl: 'less'
}
Where the following notation is used:
<Vendor>
: vendor name.<theme>
: your theme code, conventionally should correspond to the theme directory name.<area>
: area code, can be eitherΒfrontend
Β orΒadminhtml
.<language>
: specified in theΒcode_subtag
Β format, for exampleΒen_US
. Only one locale can be specified here.<path_to_file>
: path to the root source file, relative to theΒapp/design/frontend/<Vendor>/<theme>/web
Β directory. You need to specify allΒ root source files of the theme. If your theme [inherits] from a certain theme, and does not contain its own root source files, specify the root source files of the parent theme.
Using custom Grunt configuration files
To use a custom file for Grunt configuration:
Open theΒ grunt-config.json
Β file in the Magento root and set configurations object as follows.
- key: file alias
- value: path to your custom file
Example: If your custom configuration fileΒ local-themes.js
Β is located in theΒ <magento_root>/dev/tools/grunt/configs
Β directory, the following is already set in yourΒ grunt-config.json
:
"themes": "dev/tools/grunt/configs/local-themes.js"
Once these are set correctly, run grunt to watch your changes.
- RunΒ
grunt exec:<theme>
Β from the root directory to republish the symlinks. - RunΒ
grunt watch:<theme>
Β so that grunt will watch for file changes.
Lets take a example to perform these commands:-
Style Changes in Magento 2 Server-Side Compilation Mode via Grunt
- Go to your theme folder,
- Edit the _theme.less file with the following code
@navigation__background: #000;
save the file.
- run below command in the root directory of Magento2
grunt exec: <your_theme>
grunt less: <your_theme>
- Β Refresh your browser and check the changes.
Uses of grunt watch
command :-
grunt watch
command.- In root directory of Magento2, run the below command
grunt watch
So this command is watching for the changes in the less file. So lets make change in the _theme.less file. Lets change the background of the navigation….
- Then Go to your theme folder,
- Edit the _theme.less file with the following code
@navigation__background: @color-blue1;
save the file.
Now file will be executed and compiles automatically on the server.
- Refresh the page once more to view the changes.
Magento 2 Grunt Commands
Use the Grunt commands described below to perform different customization tasks in your Magento 2 project. All commands should be run from the installation directory of Magento 2.
- grunt clean:<theme>. This command removes static files related to your theme in bothΒ pub/staticΒ andΒ varΒ directories.
- grunt exec:<theme>. This one republishes source files symlinks toΒ pub/static/frontend/<Vendor>/<theme>/<locale>.
- grunt less:<theme>Β should be used to compile .css files using symlinks from the aforementioned directory.
- grunt watch. This command is designed for the LiveReload extension. It tracks changes in the source files, recompiles .css files and reloads browser pages.