PyCharm 2024.1 Help

Grunt

PyCharm integrates with the Grunt JavaScript Task Runner. PyCharm parses Gruntfile.js files, recognizing definitions of tasks and targets, shows tasks and targets in a tree view, lets you navigate between a task or a target in the tree and its definition in the Gruntfile.js file, and supports running and debugging tasks and targets.

With PyCharm, you can run Grunt tasks from tasks trees in the dedicated Grunt tool window, or using a Grunt run configuration, or as a before-launch task. PyCharm shows the result of executing a task in the Run tool window. The tool window shows the Grunt output, reports the errors occurred, lists the packages or plugins that have not been found, etc. The name of the last executed task is displayed on the title bar of the tool window.

Before you start

Make sure you have Node.js on your computer.

Installing Grunt

To use Grunt in a PyCharm project, you need two packages:

  • A globally installed grunt-cli package (Grunt command-line interface) for executing Grunt commands.

  • A grunt package installed as development dependency to build the project tasks tree and provide coding assistance while editing the Gruntfile.js or Gruntfile.coffee file. Learn more about Gruntfile.js from the Grunt official website.

Install grunt-cli globally

  • In the embedded Terminal (Alt+F12) , type:

    npm install -g grunt-cli

Install Grunt in a project

  • In the embedded Terminal (Alt+F12) , type:

    npm install grunt --save-dev

Running Grunt tasks from the tasks tree

PyCharm lets you run Grunt tasks easily and fast right from the tasks tree in the Grunt tool window. PyCharm automatically creates a temporary run configuration which you can save and use later, if necessary.

Open the Grunt tool window

When you build a tree of tasks for the first time during a PyCharm session, the Grunt tool window is not opened yet.

  • Select the required Gruntfile.js file in the Project tool window or open it in the editor and choose Show Grunt Tasks from the context menu.

Build a tree of tasks from the Grunt tool window

  1. In the Grunt tool window, click the Add button on the toolbar and choose the required Gruntfile.js file from the list. By default, PyCharm shows the Gruntfile.js file in the root of your project.

  2. If you have another Gruntfile.js file, click Choose Gruntfile.js and select the Gruntfile.js file you need in the dialog that opens. PyCharm adds a new node with the path to the chosen Gruntfile.js file on its title and builds a tasks tree under the new node.

Re-build a tree

  • Switch to the required node and click Reload Scripts on the toolbar.

Sort the tasks in a tree by their names

  • Click Settings on the toolbar, choose Sort by from the menu, and then choose Name.

    By default, a tree shows the tasks in the order in which they are defined in Gruntfile.js (option Definition order).

Run a task or a target

  • Double-click the task or target.

  • Select the task or target in the tree and press Enter or choose Run <task name> from the context menu.

Note that when you run tasks from a tasks tree, the force execution and verbose mode options are not available. As a result, you cannot configure PyCharm, for example, to ignore warnings or provide a detailed log. To use these options, run tasks or targets from a run configuration as described in Running and debugging tasks according to a run configuration below.

Run the default task

  • Select the root node in the tree, and choose Run default from the context menuu of the selection.

Run several tasks or targets

  • Use the multiselect mode: hold Shift (for adjacent items) or Ctrl (for non-adjacent items) keys and select the required tasks or targets, then choose Run or Debug from the context menu of the selection.

Jump to the definition of a task or a target

  • Select the required task or target in the tree, and choose Jump to source from the context menu of the selection.

Running and debugging tasks according to a run configuration

Besides using temporary run configurations that PyCharm creates automatically when you run tasks or targets from tasks tress, you can create and launch your own Grunt.js run configurations.

Create a Grunt.js run/debug configuration

  1. Go to Run | Edit Configurations. Alternatively, select Edit Configurations from the list on the toolbar.

    Open the Edit Configurations dialog

    In the Edit Configurations dialog that opens, click the Add button (the Add button) on the toolbar and select Grunt.js from the list. The Run/Debug Configuration: Grunt.js dialog opens.

  2. Specify the name of the run configuration, the tasks to run (use blank spaces as separators), the location of the Gruntfile.js file where these tasks are defined, and the path to the globally installed grunt-cli package.

  3. Specify the Node.js interpreter to use.

    If you choose the Project alias, PyCharm will automatically use the project default interpreter from the Node interpreter field on the Node.js page . In most cases, PyCharm detects the project default interpreter and fills in the field itself.

    You can also choose another configured local or remote interpreter or click the Browse button and configure a new one.

    Optionally specify the Node.js-specific option parameters and the environment variables to be passed to Node.js.

Run the tasks

  • From the Run/Debug Configurations list on the toolbar, select the newly created configuration and click the Run button next to it. PyCharm displays the task output in the Run tool window.

Debug the tasks

  1. Create a Grunt.js run/debug configuration as described above.

  2. Open the Gruntfile.js file in the editor and set the breakpoints in it where necessary.

  3. To start a debugging session, select the required debug configuration from the list on the main toolbar and click the Debug button next to the list or select Run | Debug <configuration name> from the main menu.

  4. In the Debug tool window that opens, analyze the suspended task execution, step through the task, and so on, as described in Examine suspended program and Step through the program.

Run a Grunt task as a before-Launch task

  1. Open the Run/debug configurations dialog dialog by selecting Run | Edit Configurations from the main menu, and select the required configuration from the list or create it anew by clicking the Add button and choosing the relevant run configuration type.

  2. In the dialog that opens, click the Add button in the Before launch area and choose Run Grunt task from the list.

  3. In the Grunt task dialog that opens, specify the Gruntfile.js where the required task is defined, select the task to execute, and specify the arguments to pass to the Grunt tool.

    Specify the location of the Node.js interpreter, the parameters to pass to it, and the path to the grunt-cli package.

Running Grunt tasks automatically

If you have some tasks or targets that you run on a regular basis, you can add the corresponding run configurations to a list of startup tasks. The tasks will be executed automatically on the project start-up.

  1. In the Settings dialog (Ctrl+Alt+S) , go to Tools | Startup Tasks.

  2. On the Startup Tasks page that opens, click the Add button on the toolbar.

  3. From the list, choose the required Grunt run configuration. The configuration is added to the list.

    If no applicable configuration is available in the project, click the Add button` and choose Edit Configurations. Then define a configuration with the required settings on the Run/Debug configuration page that opens. When you save the new configuration it is automatically added to the list of startup tasks.

Last modified: 25 June 2024