自定义NodeJS Grunt命令

我有一个自定义的任务,看起来像这样:

grunt.registerTask('list', 'test', function() { var child; child = exec('touch skhjdfgkshjgdf', function (error, stdout, stderr) { console.log('stdout: ' + stdout); console.log('stderr: ' + stderr); if (error !== null) { console.log('exec error: ' + error); } }); }); 

这工作,但是当我尝试运行pwd命令,我没有得到任何输出。 最终的目标是能够用grunt编译sass文件,而我认为这样做的最好方法是通过运行命令行命令通过grunt编译sass,但是我想获得某种输出到屏幕上工作正常。 这段代码是否有任何理由不打印通过grunt / nodejs运行unix命令的结果?

exec()是asynchronous的,所以你需要告诉grunt,并在完成时执行callback:

 grunt.registerTask('list', 'test', function() { // Tell grunt the task is async var cb = this.async(); var child = exec('touch skhjdfgkshjgdf', function (error, stdout, stderr) { if (error !== null) { console.log('exec error: ' + error); } console.log('stdout: ' + stdout); console.log('stderr: ' + stderr); // Execute the callback when the async task is done cb(); }); }); 

从咕噜文档: 为什么我的asynchronous任务不完成?