运行 grunt-bower throws _.object 不是函数错误
Running grunt-bower throws _.object is not a function error
我正在尝试 运行 g运行t-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;
但这没有用。
有什么想法或建议吗?
我已经有一段时间没有使用 Grunt 了,但我想无论如何我都会解决这个问题。希望这会有所帮助。
调查
我查看了引发错误的 helper.js 文件,发现 this line:
var _ = grunt.utils ? grunt.utils._ : grunt.util._;
然后我查看了 Grunt docs for Grunt.util,发现 Grunt.util._
已被弃用。
然后我找到了这个 repo:
https://github.com/gruntjs/grunt-legacy-util
我在哪里看到这条通知:
With the next major release of Grunt, we will no longer support these APIs. Where possible, please use the recommended modules in their place. If you would like to support or improve any of these APIs, please notify us when you have published a backwards compatible npm module—we will then recommend its usage here.
因此看起来存在兼容性问题: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 是否仍然提供旧版实用程序?如果那是真的,那么我不确定为什么您的构建被破坏了。好吧,无论哪种方式,希望其中的一些信息能为您指明正确的方向。
问题在于 Underscore 和 lodash 不能像某些人认为的那样一起玩。下划线有 _.object
,lodash 有 _.zipObject
.
grunt.util 和 grunt.utils 的创建者也不共享相同的库首选项,如 grunt.utils._ is a reference to Underscore and grunt.util._ is a reference to lodash and so is grunt-legacy-util._,这是您的构建正在使用(根据堆栈跟踪)。
这是 grunt-bower 中的一个错误,假设存在 _.object
,而他们显然也试图支持 grunt.util._,并且没有 grunt.utils 或下划线作为一种依赖。无论如何...
解决方案
1。包括 grunt.utlis
我对 Grunt 的经验不多,更不用说 grunt.utlis,但我怀疑如果你抓住 grunt-utils 包并按照他们的指导加载它,grunt-bower 应该只是默认改为那个(也就是下划线)。至少从源头上看是这样。
2。直接添加_.object方法
我完全不确定这是否可行,而且在任何情况下它看起来都非常 hacky,但很像你想做的,
grunt.util._.object = _.zipObject
可能只是在没有其他项目依赖项的情况下完成了这项工作。不过我建议你试试第一个。
3。 修复 grunt-bower 中的错误 *
打开一个问题,也许创建一个 PR。无论如何,这应该只是一个小改动 to one file,所以如果其他两个都不起作用,那么即使分叉回购似乎也是一个可靠的选择。
更新
There already is an open PR about this that I didn't notice before。显然它是昨天制作的,所以我敢打赌它很快就会合并。评论区的小鼓励可能不会伤害。
*更新 2
修复现在位于 master 分支中,可通过 v0.21.1
中的 npm 获得
我发现将 'grunt-bower' 版本更改为最新版本 (0.21.4) 可以解决此错误。
详细的,例如在grunt-bower@0.21.4的bower.js中,
'_' 定义为 lodash,如下所示:
var _ = require('lodash');
我正在尝试 运行 g运行t-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;
但这没有用。
有什么想法或建议吗?
我已经有一段时间没有使用 Grunt 了,但我想无论如何我都会解决这个问题。希望这会有所帮助。
调查
我查看了引发错误的 helper.js 文件,发现 this line:
var _ = grunt.utils ? grunt.utils._ : grunt.util._;
然后我查看了 Grunt docs for Grunt.util,发现 Grunt.util._
已被弃用。
然后我找到了这个 repo:
https://github.com/gruntjs/grunt-legacy-util
我在哪里看到这条通知:
With the next major release of Grunt, we will no longer support these APIs. Where possible, please use the recommended modules in their place. If you would like to support or improve any of these APIs, please notify us when you have published a backwards compatible npm module—we will then recommend its usage here.
因此看起来存在兼容性问题: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 是否仍然提供旧版实用程序?如果那是真的,那么我不确定为什么您的构建被破坏了。好吧,无论哪种方式,希望其中的一些信息能为您指明正确的方向。
问题在于 Underscore 和 lodash 不能像某些人认为的那样一起玩。下划线有 _.object
,lodash 有 _.zipObject
.
grunt.util 和 grunt.utils 的创建者也不共享相同的库首选项,如 grunt.utils._ is a reference to Underscore and grunt.util._ is a reference to lodash and so is grunt-legacy-util._,这是您的构建正在使用(根据堆栈跟踪)。
这是 grunt-bower 中的一个错误,假设存在 _.object
,而他们显然也试图支持 grunt.util._,并且没有 grunt.utils 或下划线作为一种依赖。无论如何...
解决方案
1。包括 grunt.utlis
我对 Grunt 的经验不多,更不用说 grunt.utlis,但我怀疑如果你抓住 grunt-utils 包并按照他们的指导加载它,grunt-bower 应该只是默认改为那个(也就是下划线)。至少从源头上看是这样。
2。直接添加_.object方法
我完全不确定这是否可行,而且在任何情况下它看起来都非常 hacky,但很像你想做的,
grunt.util._.object = _.zipObject
可能只是在没有其他项目依赖项的情况下完成了这项工作。不过我建议你试试第一个。
3。 修复 grunt-bower 中的错误 *
打开一个问题,也许创建一个 PR。无论如何,这应该只是一个小改动 to one file,所以如果其他两个都不起作用,那么即使分叉回购似乎也是一个可靠的选择。
更新
There already is an open PR about this that I didn't notice before。显然它是昨天制作的,所以我敢打赌它很快就会合并。评论区的小鼓励可能不会伤害。
*更新 2
修复现在位于 master 分支中,可通过 v0.21.1
中的 npm 获得我发现将 'grunt-bower' 版本更改为最新版本 (0.21.4) 可以解决此错误。
详细的,例如在grunt-bower@0.21.4的bower.js中,
'_' 定义为 lodash,如下所示:
var _ = require('lodash');