Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running grunt-bower throws _.object is not a function error

I am trying to run grunt-bower task for copying all my 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.

Here's how my Gruntfile.js looks

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"]);
};

and my 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"
  }
}

I tried resetting

var _ = require("lodash");

_.object = _.zipObject;

but this did not work.

Any thoughts or suggestions?

like image 463
Won Oh Avatar asked Apr 24 '16 02:04

Won Oh


1 Answers

The problem comes from the fact that Underscore and lodash don't play as well together as some might think they do. Underscore has _.object, lodash has _.zipObject.

The creators of grunt.util and grunt.utils don't share the same library preferences either, as grunt.utils._ is a reference to Underscore and grunt.util._ is a reference to lodash and so is grunt-legacy-util._, which is what your build is using (according to the stack trace).

That's a bug in grunt-bower, assuming there is _.object when they are clearly trying to support grunt.util._ too and don't have grunt.utils or Underscore as a dependency. Anyways...

Solutions

1. Include grunt.utlis

I don't have much experience on Grunt, much less grunt.utlis, but I suspect that if you grab the grunt-utils package and load it like they guide, grunt-bower should just default to that (and namely, Underscore) instead. At least looking at the source it seems so.

2. Add the _.object method directly

I'm not sure at all if this would work and in any case it seems super hacky, but much like you were trying to do,

grunt.util._.object = _.zipObject

might just do the trick without other dependencies to project. I suggest you try the first one though.

3. Fix the bug in grunt-bower *

Open an issue, maybe create a PR. It should be just a small change to one file anyways, so even forking the repo seems like a solid option if the other two won't work.


Update

There already is an open PR about this that I didn't notice before. Apparently it was made yesterday, so I bet it will get a merge in no time. Little encouragement in the comments section might not hurt though.


*Update 2

The fix is now in the master branch and available through npm in v0.21.1

like image 160
noppa Avatar answered Nov 10 '22 13:11

noppa