Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Grunt registerTask中使用npm模块_Npm_Gruntjs - Fatal编程技术网

在Grunt registerTask中使用npm模块

在Grunt registerTask中使用npm模块,npm,gruntjs,Npm,Gruntjs,我想在新任务上使用npm模块,但没有结果: grunt.registerTask('done', function () { var prepend = require('prepend'); var file = 'app.bundleES6.js', string = '// My string'; prepend(file, string, function(error) { if (error) console

我想在新任务上使用npm模块,但没有结果:

  grunt.registerTask('done', function () {

    var prepend = require('prepend');

    var file = 'app.bundleES6.js',
        string = '// My string';

    prepend(file, string, function(error) {
      if (error)
        console.error(error.message);
      else
        console.log('Yeah');
    });

  });
当我运行
grunt done
时,我没有看到console.log,只是:

Running "done" task

Done, without errors.
您知道如何在Grunt registerTask上使用npm模块吗


谢谢大家!

任务的问题在于它是异步的

您可以注册异步Grunt任务,但必须调用
this.async()
才能获得完成回调,并且必须在任务完成时调用回调(如果任务失败,则传递
false

像这样:

grunt.registerTask('done', function () {

  var callback = this.async();
  var prepend = require('prepend');

  var file = 'app.bundleES6.js',
      string = '// My string';

  prepend(file, string, function(error) {
    if (error) {
      console.error(error.message);
      callback(false);
    }
    else {
      console.log('Yeah');
      callback();
    }
  });
});

任务的问题在于它是异步的

您可以注册异步Grunt任务,但必须调用
this.async()
才能获得完成回调,并且必须在任务完成时调用回调(如果任务失败,则传递
false

像这样:

grunt.registerTask('done', function () {

  var callback = this.async();
  var prepend = require('prepend');

  var file = 'app.bundleES6.js',
      string = '// My string';

  prepend(file, string, function(error) {
    if (error) {
      console.error(error.message);
      callback(false);
    }
    else {
      console.log('Yeah');
      callback();
    }
  });
});

非常感谢。工作完美谢谢!完美地工作