运行grunt-bower抛出_.object不是函数错误

时间:2016-04-24 02:53:32

标签: javascript node.js gruntjs lodash

我正在尝试运行grunt-bower任务来复制我所有的bower-components。

Running "bower:dev" (bower) task
TypeError: _.object is not a function
  at Object.exports.getDests (/Users/wonoh/cocApp/node_modules/grunt-bower/tasks/lib/helpers.js:131:14)
  at /Users/wonoh/cocApp/node_modules/grunt-bower/tasks/bower.js:63:35
  at Array.forEach (native)
  at /Users/wonoh/cocApp/node_modules/grunt-bower/tasks/bower.js:59:21
  at /Users/wonoh/cocApp/node_modules/grunt-legacy-util/node_modules/lodash/lodash.js:4040:15
  at baseForOwn (/Users/wonoh/cocApp/node_modules/grunt-legacy-util/node_modules/lodash/lodash.js:2573:24)
  at /Users/wonoh/cocApp/node_modules/grunt-legacy-util/node_modules/lodash/lodash.js:4009:18
  at Function.forEach (/Users/wonoh/cocApp/node_modules/grunt-legacy-util/node_modules/lodash/lodash.js:7634:11)
  at LodashWrapper.object.(anonymous function) [as each] (/Users/wonoh/cocApp/node_modules/grunt-legacy-util/node_modules/lodash/lodash.js:13501:25)
  at Logger.<anonymous> (/Users/wonoh/cocApp/node_modules/grunt-bower/tasks/bower.js:37:17)
  at emitOne (events.js:90:13)
  at Logger.emit (events.js:182:7)
  at Logger.emit (/Users/wonoh/cocApp/node_modules/bower-logger/lib/Logger.js:29:39)
  at /Users/wonoh/cocApp/node_modules/bower/lib/commands/list.js:75:16
  at _fulfilled (/Users/wonoh/cocApp/node_modules/q/q.js:798:54)
  at self.promiseDispatch.done (/Users/wonoh/cocApp/node_modules/q/q.js:827:30)
  at Promise.promise.promiseDispatch (/Users/wonoh/cocApp/node_modules/q/q.js:760:13)
  at /Users/wonoh/cocApp/node_modules/q/q.js:574:44
  at flush (/Users/wonoh/cocApp/node_modules/q/q.js:108:17)
  at _combinedTickCallback (internal/process/next_tick.js:67:7)
  at process._tickCallback (internal/process/next_tick.js:98:9)
Fail to copy lib file for angular-mocks!
TypeError: _.object is not a function
  at Object.exports.getDests (/Users/wonoh/cocApp/node_modules/grunt-bower/tasks/lib/helpers.js:131:14)
  at /Users/wonoh/cocApp/node_modules/grunt-bower/tasks/bower.js:63:35
  at Array.forEach (native)
  at /Users/wonoh/cocApp/node_modules/grunt-bower/tasks/bower.js:59:21
  at /Users/wonoh/cocApp/node_modules/grunt-legacy-util/node_modules/lodash/lodash.js:4040:15
  at baseForOwn (/Users/wonoh/cocApp/node_modules/grunt-legacy-util/node_modules/lodash/lodash.js:2573:24)
  at /Users/wonoh/cocApp/node_modules/grunt-legacy-util/node_modules/lodash/lodash.js:4009:18
  at Function.forEach (/Users/wonoh/cocApp/node_modules/grunt-legacy-util/node_modules/lodash/lodash.js:7634:11)
  at LodashWrapper.object.(anonymous function) [as each] (/Users/wonoh/cocApp/node_modules/grunt-legacy-util/node_modules/lodash/lodash.js:13501:25)
  at Logger.<anonymous> (/Users/wonoh/cocApp/node_modules/grunt-bower/tasks/bower.js:37:17)
  at emitOne (events.js:90:13)
  at Logger.emit (events.js:182:7)
  at Logger.emit (/Users/wonoh/cocApp/node_modules/bower-logger/lib/Logger.js:29:39)
  at /Users/wonoh/cocApp/node_modules/bower/lib/commands/list.js:75:16
  at _fulfilled (/Users/wonoh/cocApp/node_modules/q/q.js:798:54)
  at self.promiseDispatch.done (/Users/wonoh/cocApp/node_modules/q/q.js:827:30)
  at Promise.promise.promiseDispatch (/Users/wonoh/cocApp/node_modules/q/q.js:760:13)
  at /Users/wonoh/cocApp/node_modules/q/q.js:574:44
  at flush (/Users/wonoh/cocApp/node_modules/q/q.js:108:17)
  at _combinedTickCallback (internal/process/next_tick.js:67:7)
  at process._tickCallback (internal/process/next_tick.js:98:9)
Fail to copy lib file for angular!
TypeError: _.object is not a function
  at Object.exports.getDests (/Users/wonoh/cocApp/node_modules/grunt-bower/tasks/lib/helpers.js:131:14)
  at /Users/wonoh/cocApp/node_modules/grunt-bower/tasks/bower.js:63:35
  at Array.forEach (native)
  at /Users/wonoh/cocApp/node_modules/grunt-bower/tasks/bower.js:59:21
  at /Users/wonoh/cocApp/node_modules/grunt-legacy-util/node_modules/lodash/lodash.js:4040:15
  at baseForOwn (/Users/wonoh/cocApp/node_modules/grunt-legacy-util/node_modules/lodash/lodash.js:2573:24)
  at /Users/wonoh/cocApp/node_modules/grunt-legacy-util/node_modules/lodash/lodash.js:4009:18
  at Function.forEach (/Users/wonoh/cocApp/node_modules/grunt-legacy-util/node_modules/lodash/lodash.js:7634:11)
  at LodashWrapper.object.(anonymous function) [as each] (/Users/wonoh/cocApp/node_modules/grunt-legacy-util/node_modules/lodash/lodash.js:13501:25)
  at Logger.<anonymous> (/Users/wonoh/cocApp/node_modules/grunt-bower/tasks/bower.js:37:17)
  at emitOne (events.js:90:13)
  at Logger.emit (events.js:182:7)
  at Logger.emit (/Users/wonoh/cocApp/node_modules/bower-logger/lib/Logger.js:29:39)
  at /Users/wonoh/cocApp/node_modules/bower/lib/commands/list.js:75:16
  at _fulfilled (/Users/wonoh/cocApp/node_modules/q/q.js:798:54)
  at self.promiseDispatch.done (/Users/wonoh/cocApp/node_modules/q/q.js:827:30)
  at Promise.promise.promiseDispatch (/Users/wonoh/cocApp/node_modules/q/q.js:760:13)
  at /Users/wonoh/cocApp/node_modules/q/q.js:574:44
  at flush (/Users/wonoh/cocApp/node_modules/q/q.js:108:17)
  at _combinedTickCallback (internal/process/next_tick.js:67:7)
  at process._tickCallback (internal/process/next_tick.js:98:9)
Fail to copy lib file for angular-route!
TypeError: _.object is not a function
  at Object.exports.getDests (/Users/wonoh/cocApp/node_modules/grunt-bower/tasks/lib/helpers.js:131:14)
  at /Users/wonoh/cocApp/node_modules/grunt-bower/tasks/bower.js:63:35
  at Array.forEach (native)
  at /Users/wonoh/cocApp/node_modules/grunt-bower/tasks/bower.js:59:21
  at /Users/wonoh/cocApp/node_modules/grunt-legacy-util/node_modules/lodash/lodash.js:4040:15
  at baseForOwn (/Users/wonoh/cocApp/node_modules/grunt-legacy-util/node_modules/lodash/lodash.js:2573:24)
  at /Users/wonoh/cocApp/node_modules/grunt-legacy-util/node_modules/lodash/lodash.js:4009:18
  at Function.forEach (/Users/wonoh/cocApp/node_modules/grunt-legacy-util/node_modules/lodash/lodash.js:7634:11)
  at LodashWrapper.object.(anonymous function) [as each] (/Users/wonoh/cocApp/node_modules/grunt-legacy-util/node_modules/lodash/lodash.js:13501:25)
  at Logger.<anonymous> (/Users/wonoh/cocApp/node_modules/grunt-bower/tasks/bower.js:37:17)
  at emitOne (events.js:90:13)
  at Logger.emit (events.js:182:7)
  at Logger.emit (/Users/wonoh/cocApp/node_modules/bower-logger/lib/Logger.js:29:39)
  at /Users/wonoh/cocApp/node_modules/bower/lib/commands/list.js:75:16
  at _fulfilled (/Users/wonoh/cocApp/node_modules/q/q.js:798:54)
  at self.promiseDispatch.done (/Users/wonoh/cocApp/node_modules/q/q.js:827:30)
  at Promise.promise.promiseDispatch (/Users/wonoh/cocApp/node_modules/q/q.js:760:13)
  at /Users/wonoh/cocApp/node_modules/q/q.js:574:44
  at flush (/Users/wonoh/cocApp/node_modules/q/q.js:108:17)
  at _combinedTickCallback (internal/process/next_tick.js:67:7)
  at process._tickCallback (internal/process/next_tick.js:98:9)
Fail to copy lib file for bootstrap!
TypeError: _.object is not a function
  at Object.exports.getDests (/Users/wonoh/cocApp/node_modules/grunt-bower/tasks/lib/helpers.js:131:14)
  at /Users/wonoh/cocApp/node_modules/grunt-bower/tasks/bower.js:63:35
  at Array.forEach (native)
  at /Users/wonoh/cocApp/node_modules/grunt-bower/tasks/bower.js:59:21
  at /Users/wonoh/cocApp/node_modules/grunt-legacy-util/node_modules/lodash/lodash.js:4040:15
  at baseForOwn (/Users/wonoh/cocApp/node_modules/grunt-legacy-util/node_modules/lodash/lodash.js:2573:24)
  at /Users/wonoh/cocApp/node_modules/grunt-legacy-util/node_modules/lodash/lodash.js:4009:18
  at Function.forEach (/Users/wonoh/cocApp/node_modules/grunt-legacy-util/node_modules/lodash/lodash.js:7634:11)
  at LodashWrapper.object.(anonymous function) [as each] (/Users/wonoh/cocApp/node_modules/grunt-legacy-util/node_modules/lodash/lodash.js:13501:25)
  at Logger.<anonymous> (/Users/wonoh/cocApp/node_modules/grunt-bower/tasks/bower.js:37:17)
  at emitOne (events.js:90:13)
  at Logger.emit (events.js:182:7)
  at Logger.emit (/Users/wonoh/cocApp/node_modules/bower-logger/lib/Logger.js:29:39)
  at /Users/wonoh/cocApp/node_modules/bower/lib/commands/list.js:75:16
  at _fulfilled (/Users/wonoh/cocApp/node_modules/q/q.js:798:54)
  at self.promiseDispatch.done (/Users/wonoh/cocApp/node_modules/q/q.js:827:30)
  at Promise.promise.promiseDispatch (/Users/wonoh/cocApp/node_modules/q/q.js:760:13)
  at /Users/wonoh/cocApp/node_modules/q/q.js:574:44
  at flush (/Users/wonoh/cocApp/node_modules/q/q.js:108:17)
  at _combinedTickCallback (internal/process/next_tick.js:67:7)
  at process._tickCallback (internal/process/next_tick.js:98:9)
Fail to copy lib file for jquery!
TypeError: _.object is not a function
  at Object.exports.getDests (/Users/wonoh/cocApp/node_modules/grunt-bower/tasks/lib/helpers.js:131:14)
  at /Users/wonoh/cocApp/node_modules/grunt-bower/tasks/bower.js:63:35
  at Array.forEach (native)
  at /Users/wonoh/cocApp/node_modules/grunt-bower/tasks/bower.js:59:21
  at /Users/wonoh/cocApp/node_modules/grunt-legacy-util/node_modules/lodash/lodash.js:4040:15
  at baseForOwn (/Users/wonoh/cocApp/node_modules/grunt-legacy-util/node_modules/lodash/lodash.js:2573:24)
  at /Users/wonoh/cocApp/node_modules/grunt-legacy-util/node_modules/lodash/lodash.js:4009:18
  at Function.forEach (/Users/wonoh/cocApp/node_modules/grunt-legacy-util/node_modules/lodash/lodash.js:7634:11)
  at LodashWrapper.object.(anonymous function) [as each] (/Users/wonoh/cocApp/node_modules/grunt-legacy-util/node_modules/lodash/lodash.js:13501:25)
  at Logger.<anonymous> (/Users/wonoh/cocApp/node_modules/grunt-bower/tasks/bower.js:37:17)
  at emitOne (events.js:90:13)
  at Logger.emit (events.js:182:7)
  at Logger.emit (/Users/wonoh/cocApp/node_modules/bower-logger/lib/Logger.js:29:39)
  at /Users/wonoh/cocApp/node_modules/bower/lib/commands/list.js:75:16
  at _fulfilled (/Users/wonoh/cocApp/node_modules/q/q.js:798:54)
  at self.promiseDispatch.done (/Users/wonoh/cocApp/node_modules/q/q.js:827:30)
  at Promise.promise.promiseDispatch (/Users/wonoh/cocApp/node_modules/q/q.js:760:13)
  at /Users/wonoh/cocApp/node_modules/q/q.js:574:44
  at flush (/Users/wonoh/cocApp/node_modules/q/q.js:108:17)
  at _combinedTickCallback (internal/process/next_tick.js:67:7)
  at process._tickCallback (internal/process/next_tick.js:98:9)
Fail to copy lib file for lodash!

Done.

以下是我的Gruntfile.js的外观

module.exports = function(grunt) {
  var _ = require("lodash");
  _.object = _.zipObject;
  grunt.initConfig({
    "bower": {
      "dev": {
        "dest": "dist/vendor/js",
        "css_dest": "dist/vendor/css",
        "fonts_dest": "dist/fonts"
      }
    }
  });

  grunt.loadNpmTasks("grunt-bower");

  grunt.registerTask("default", ["bower"]);
};

和我的package.json

{
  "name": "coc-app",
  "version": "1.0.0",
  "description": "Clash of Clans Application",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "private",
  "dependencies": {
    "body-parser": "^1.15.0",
    "cookie-parser": "^1.4.1",
    "express": "^4.13.4",
    "jade": "^1.11.0",
    "lodash": ">=3.0.0 <4.0.0"
  },
  "devDependencies": {
    "grunt": "^1.0.1",
    "grunt-bower": "^0.21.0",
    "grunt-contrib-concat": "^1.0.1"
  }
}

我尝试重置

var _ = require("lodash");

_.object = _.zipObject;

但这不起作用。

有任何想法或建议吗?

3 个答案:

答案 0 :(得分:9)

问题来自于Underscore和lodash并没有像有些人认为的那样发挥得很好。下划线有_.object,lodash有_.zipObject

grunt.util和grunt.utils的创建者也不共享相同的库首选项,如grunt.utils._ is a reference to Underscore和grunt.util._ is a reference to lodash和{{3} },这是您的构建使用的(根据堆栈跟踪)。

这是grunt-bower中的一个错误,假设有_.object时他们显然也在尝试支持grunt.util._并且没有grunt.utils或Underscore作为依赖项。反正...

解决方案

1。包括grunt.utlis

我对Grunt没有太多经验,更不用说grunt.utlis了,但是我怀疑如果你抓住so is grunt-legacy-util._包并按照他们的指导加载它,grunt-bower应该默认为那个(和即,Underscore)。至少看看来源似乎是这样。

2。直接添加_.object方法

我根本不确定这是否会起作用,无论如何它似乎超级hacky,但就像你试图做的那样,

grunt.util._.object = _.zipObject

可能只是没有项目的其他依赖项。我建议你试试第一个。

3。 修复grunt-bower *

中的错误

打开一个问题,也许创建公关。它应该只是一个小的变化grunt-utils,所以即使分配回购似乎是一个可靠的选择,如果其他两个不起作用。

更新

to one file。显然它是昨天制作的,所以我打赌它会很快得到合并。评论部分的一点鼓励可能不会有害。

*更新2

修复现在位于主分支中,可通过 v0.21.1

中的npm获取

答案 1 :(得分:0)

我暂时没有使用Grunt,但我想我还是会解决这个问题。希望这会有所帮助。

调查

我查看了helper.js文件中的错误,我找到了this line

var _ = grunt.utils ? grunt.utils._ : grunt.util._;

然后我查看了Grunt docs for Grunt.util,看到Grunt.util._已被弃用。

然后我找到了这个回购:

https://github.com/gruntjs/grunt-legacy-util

我看到了这个通知:

  

随着Grunt的下一个主要版本,我们将不再支持这些API。如果可能,请使用推荐的模块。如果您想支持或改进任何这些API,请在发布向后兼容的npm模块时通知我们 - 我们将在此处推荐其使用。

所以看起来存在兼容性问题:grunt-bower期望grunt.util._存在,但它不存在于最新版本的Grunt中。

解决方案

您可以使用grunt-legacy-util或只是投入grunt.util._ = require('lodash')来电。这应该可以解决问题。

此外,有人应该让grunt-bower的维护者知道他的库不再与当前版本的Grunt兼容。

修改:我刚刚调查了Grunt的新版本如何填充grunt.util,我看到了this

var util = require('grunt-legacy-util');
grunt.util = util;

所以现在我感到困惑...... Grunt的新版本仍在提供遗留的工具吗?如果这是真的,那么我不确定为什么你的构建被破坏了。好吧,无论哪种方式,希望这些信息能指出正确的方向。

答案 2 :(得分:0)

我发现将'grunt-bower'版本更改为最新版本(0.21.4)可以解决此错误。

详细地说,例如,在bower.js中的grunt-bower@0.21.4,
'_'被定义为lodash,如下所示:

var _ = require('lodash');