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

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

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;

但是这不起作用。

任何想法或build议?

这个问题来自于Underscore和lodash不像其他人一样玩的事实。 下划线有_.object ,lodash有_.zipObject

grunt.util和grunt.utils的创build者不会共享相同的库首选项,因为grunt.utils._ 是对 Underscore 的引用,而grunt.util._ 是对lodash的引用 , 所以grunt-legacy-util ._ ,这是你的构build使用(根据堆栈跟踪)。

这是一个在grunt-bower中的错误,假设有_.object当他们明确地尝试支持grunt.util._时,也没有grunt.utils或Underscore作为依赖。 无论如何…

解决scheme

1.包含grunt.utlis

我在Grunt上没有太多的经验,更不用说grunt.utlis了,但是我怀疑如果你抓住grunt-utils包并像他们指导的那样加载,grunt-bower应该默认为(也就是Underscore) 。 至less看起来是这样的源头。

2.直接添加_.object方法

我不确定这是否可行,无论如何,这似乎是超级黑客,但很像你想要做的,

 grunt.util._.object = _.zipObject 

可能只是做窍门没有其他依赖项目。 我build议你尝试第一个。

3. 修复grunt-bower中的bug

打开一个问题,也许创build一个PR。 它应该只是一个文件的一个小的改变,所以即使分叉repo似乎是一个坚实的select,如果其他两个不会工作。


更新

已经有一个关于这个我以前没有注意到的公开的公关 。 显然是昨天做的,所以我敢打赌,它会立即得到合并。 尽pipe评论部分的鼓励不大。


*更新2

该修补程序现在位于主分支中,可通过v0.21.1中的 npm 获得

我有一段时间没有使用Grunt,但我想我会解决这个问题。 希望这有助于。

调查

我看了引发错误的helper.js文件,发现这一行 :

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

然后我看了一下Grunt.util的Grunt文档 ,看到Grunt.util._已经被弃用了。

然后我发现这个回购:

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

我看到了这个通知:

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

因此,看起来存在一个兼容性问题:grunt-bower希望grunt.util._存在,但它不存在于最新版本的Grunt中。

解决scheme

您可以使用grunt-legacy-util或简单地grunt.util._ = require('lodash')调用。 这应该解决这个问题。

另外,也许有人应该让咕噜咕噜的维护者知道他的库不再与当前版本的Grunt兼容。

编辑:我只是看着如何新版本的Grunt填充grunt.util,我看到了这一点 :

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

所以现在我很困惑…是新版本的Grunt仍然提供遗留的使用情况? 如果这是真的,那么我不知道为什么你的构build被打破。 那么不pipe怎么说,希望这些信息都能指出你正确的方向。

我发现把“grunt-bower”版本改成最新(0.21.4)可以解决这个错误。

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

 var _ = require('lodash');