Using a node module within a Grunt Task fails

懵懂的女人 提交于 2020-01-06 13:11:33

问题


I'm trying to extract meta data from files read within a Grunt task.

executing: node test.js on this file:

var exif = require('exif2');

exif('fixtures/forest.png', function (err, o) {
    console.log(arguments);
});

Produces the expected output

However, executing the grunt process: grunt projectJSON

module.exports = function (grunt) {
    var exif = require('exif2');
    return grunt.registerMultiTask("projectJSON", "Creates project JSON file.", function () {
        exif('fixtures/forest.png', function (err, o) {
            console.log(arguments);
        });
    });

}

** note that I am just testing with the fixtures/forest.png file

Produces no output whatsoever. The callback isn't even fired.

When I console.log exif, I get: [Function]

What am I missing? I think that the doesn't work is because of the grunt task, but I have no idea how to fix it. Wrapping it in a try-catch block produces nothing.


回答1:


You need to make your projectJSON task asynchronous - Grunt is exiting before your exif callback is being invoked.

Have a look at the Grunt documentation on asynchronous tasks.

This is how you can make your task asynchronous:

module.exports = function (grunt) {
    var exif = require('exif2');

    grunt.registerMultiTask("projectJSON", "Creates project JSON file.", function () {
        // Make task asynchronous.
        var done = this.async();

        exif('fixtures/forest.png', function (err, o) {
            console.log(arguments);

            // Invoke the task callback to continue with
            // other Grunt tasks.
            done();
        });
    });

}


来源:https://stackoverflow.com/questions/23667853/using-a-node-module-within-a-grunt-task-fails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!