Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Grunt FAQ


May 25, 2021 Grunt


Table of contents


Problems

How do I install grunt?

For general installation instructions, read the Quick Start guide. If you need more details after reading, you can read the more detailed Installation Grunt guide.

When will I be able to use the 'something' feature in development?

The Installation Grunt guide describes how to install a published and unresoled version of Grunt.

Can Grunt work on Windows?

Grunt works well on windows because node .js and npm work well on windows. Typically, the problem is Cygwin because it bundles an older version of .js.

The best way to avoid this problem is to use msysGit installer to install git and node.js installer to install node and npm and then replace Cygwin with built-in Windows command prompt or PowerShell.

Why can't my asynchronous tasks be completed?

This is because you forgot to call the this.async method to tell Grunt that your task is executed asynchronously. To simplify the model, Grunt uses the coding style of synchronous mode, which you can switch to asynchronous mode by calling this.async() in a task.

Note that if task fails to execute, you can pass false done() to inform Grunt.

Case:

grunt.registerTask('asyncme', 'My asynchronous task.', function() {
  var done = this.async();
  doSomethingAsync(done);
});

How do I enable tab key auto-complement in shell?

In order to add tab key auto-complement to grunt, you can add the following line of code to your ~/.bashrc file:

eval "$(grunt --completion=bash)"

Of course, let's npm install -g grunt using npm install -g grunt. Because Grunt currently only supports bash commands.

Do I want multiple tasks to share parameters?

Although each task can use its own parameters, there are several ways to allow you to share parameters across multiple taskes.

"Dynamic" task alias

This is the preferred method for sharing parameters for multiple tasks

Since the task alias is simple, a normal task can use grunt.task.run to have a function as a "dynamic" task alias. In the following case, grunt build:001 is executed on the command line, and the end result is the execution of bar:001 baz:001 foo:001

grunt.registerTask('build', 'Run all my build tasks.', function(n) {
  if (n == null) {
    grunt.warn('Build num must be specified, like build:001.');
  }
  grunt.task.run('foo:' + n, 'bar:' + n, 'baz:' + n);
});

-- Options

Multiple tasks share parameters by using grunt.option. Here's an example where executing grunt deploy --target=staging on the command line would return grunt.option('target') "staging"

grunt.registerTask('upload', 'Upload code to specified target.', function(n) {
  var target = grunt.option('target');
  // do something useful with target here
});
grunt.registerTask('deploy', ['validate', 'upload']);

Note that boolean-type parameters can use a key with no value. For example, executing grunt deploy --staging command line grunt.option('staging') true

Global and configuration

In other cases, you may want to expose a setting configuration or a global value method. In this case, you can set its parameters as a global object or the value of the project configuration when you register a task.

In the following example, running grunt set_global:name:peter set_config:target:staging deploy causes the value of global.name "peter" and grunt.config('target') "staging" global.name From this, it is deploy task can use these values.

grunt.registerTask('set_global', 'Set a global variable.', function(name, val) {
  global[name] = val;
});

grunt.registerTask('set_config', 'Set a config property.', function(name, val) {
  grunt.config.set(name, val);
});

How do I get trace information for the call stack when an error occurs?

You can see the trace information for the call stack using the --stack parameter. For example: grunt task --stack .

Why is there a "Maximum call stack size exceeded "

You may have renamed alias and other tasks that you created for a task. F or example: grunt.registerTask('uglify', ['uglify:my_target']); It grunt.registerTask('myUglify', ['uglify:my_target']);

How do I uninstall or remove unwanted plug-ins?

There are at least two ways. O ne method is to npm uninstall [GRUNT_PLUGIN] --save-dev which removes the specified node_modules package.json file and the node_modules directory. Another method is to manually remove package.json file and then execute npm prune instruction.

Error "Fail to install with npm error: No compatible version found"

Make sure you have the latest stable versions of NPM and Node .JS.


Issues related to grunt 0.3

In Windows Grunt 0.3, why does my JS editor open when I try to run grunt?

If you're in gruntfile's directory, Windows tries to execute that file when you enter grunt. So you need to grunt.cmd

Another option is to use DOSKEY command to create a Grunt macro, please refer to this article. This allows grunt grunt.cmd

You can use the DOSKEY like this:

DOSKEY grunt=grunt.cmd $*