开始使用Gulp接收此TypeError:无法读取属性' length'未定义的

时间:2015-11-19 15:43:10

标签: javascript gulp

这似乎发生在imagemin-svgo节点模块中。在注释掉try语句时,问题就会消失。我似乎无法弄清楚为什么我从这段代码收到错误。我一直在寻找答案,一切似乎都是最新的。我在GitHub上找到了这个模块,然后把它放在那里,取而代之的是它似乎没有解决任何问题,我仍然得到同样的错误。以下是我收到的错误以及我认为是原因的代码。请帮忙,欢迎任何见解。提前谢谢。

/theme_name/node_modules/imagemin-svgo/index.js

收到的错误:

events.js:141

  throw er; // Unhandled 'error' event
  ^

TypeError: Cannot read property 'length' of undefined
at Object.exports.fn (/Users/lalice/Desktop/Development/Drupal_sites/Drupal_seven/uncsog/docroot/sites/www.sog.unc.edu/themes/sog/node_modules/svgo/plugins/minifyStyles.js:47:24)
at /Users/lalice/Desktop/Development/Drupal_sites/Drupal_seven/uncsog/docroot/sites/www.sog.unc.edu/themes/sog/node_modules/svgo/lib/svgo/plugins.js:59:45
at Array.filter (native)
at monkeys (/Users/lalice/Desktop/Development/Drupal_sites/Drupal_seven/uncsog/docroot/sites/www.sog.unc.edu/themes/sog/node_modules/svgo/lib/svgo/plugins.js:46:39)
at /Users/lalice/Desktop/Development/Drupal_sites/Drupal_seven/uncsog/docroot/sites/www.sog.unc.edu/themes/sog/node_modules/svgo/lib/svgo/plugins.js:66:17
at Array.filter (native)
at monkeys (/Users/lalice/Desktop/Development/Drupal_sites/Drupal_seven/uncsog/docroot/sites/www.sog.unc.edu/themes/sog/node_modules/svgo/lib/svgo/plugins.js:46:39)
at perItem (/Users/lalice/Desktop/Development/Drupal_sites/Drupal_seven/uncsog/docroot/sites/www.sog.unc.edu/themes/sog/node_modules/svgo/lib/svgo/plugins.js:77:12)
at /Users/lalice/Desktop/Development/Drupal_sites/Drupal_seven/uncsog/docroot/sites/www.sog.unc.edu/themes/sog/node_modules/svgo/lib/svgo/plugins.js:18:24
at Array.forEach (native)

index.js文件中的代码:

    try {
        var svgo = new SVGO(opts);

        svgo.optimize(file.contents.toString('utf8'), function (res) {
            if (!res.data || !res.data.length) {
                return;
            }

            res.data = res.data.replace(/&(?!amp;)/g, '&');
            res.data = new Buffer(res.data);

            if (res.data.length < file.contents.length) {
                file.contents = res.data;
            }
        });
    } catch (err) {
        err.fileName = file.path;
        cb(err);
        return;
    }

0 个答案:

没有答案