16

I am getting this strange error trying to run grunt: TypeError: Object Gruntfile.js has no method 'flatten'

I am new to node.js, npm, grunt, etc. I thought I did a decent install of node, npm, grunt but may be I missed something. Is there a way to verify the install??

$ cat xx
$ grunt

/home/cl/node_modules/grunt/node_modules/findup-sync/lib/findup-sync.js:33
    }).flatten().uniq().value();
       ^
TypeError: Object Gruntfile.js has no method 'flatten'
    at Object.module.exports [as findup] (/home/cl/node_modules/grunt/node_modules/findup-sync/lib/findup-sync.js:33:8)
    at Task.task.init (/home/cl/node_modules/grunt/lib/grunt/task.js:414:16)
    at Object.grunt.tasks (/home/cl/node_modules/grunt/lib/grunt.js:113:8)
    at Object.module.exports [as cli] (/home/cl/node_modules/grunt/lib/grunt/cli.js:38:9)
    at Object.<anonymous> (/usr/lib/node_modules/grunt-cli/bin/grunt:41:20)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)

The offending line/file:

/*
 * findup-sync
 * https://github.com/cowboy/node-findup-sync
 *
 * Copyright (c) 2013 "Cowboy" Ben Alman
 * Licensed under the MIT license.
 */

'use strict';

// Nodejs libs.
var path = require('path');

// External libs.
var glob = require('glob');
var _ = require('lodash');

// Search for a filename in the given directory or all parent directories.
module.exports = function(patterns, options) {
  // Normalize patterns to an array.
  if (!Array.isArray(patterns)) { patterns = [patterns]; }
  // Create globOptions so that it can be modified without mutating the
  // original object.
  var globOptions = Object.create(options || {});
  globOptions.maxDepth = 1;
  globOptions.cwd = path.resolve(globOptions.cwd || '.');

  var files, lastpath;
  do {
    // Search for files matching patterns.
    files = _(patterns).map(function(pattern) {
      return glob.sync(pattern, globOptions);
    }).flatten().uniq().value(); // <--------- OFFENDING LINE
    // Return file if found.
    if (files.length > 0) {
      return path.resolve(path.join(globOptions.cwd, files[0]));
    }
    // Go up a directory.
    lastpath = globOptions.cwd;
    globOptions.cwd = path.resolve(globOptions.cwd, '..');
  // If parentpath is the same as basedir, we can't go any higher.
  } while (globOptions.cwd !== lastpath);

  // No files were found!
  return null;
};

Output of ls -l node_modules/:

$ ls -l node_modules/
total 20
drwxrwxr-x. 6 a a 4096 Oct  2 00:42 grunt
drwxrwxr-x. 4 a a 4096 Oct  2 00:42 grunt-contrib-compass
drwxrwxr-x. 6 a a 4096 Oct  2 00:42 grunt-contrib-jshint
drwxrwxr-x. 6 a a 4096 Oct  2 00:42 grunt-contrib-watch
drwxrwxr-x. 4 a a 4096 Oct  2 00:42 grunt-dustjs

Output of npm list:

$ npm list
[email protected] /home/a/prep/main/web/client
├─┬ [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
│ ├─┬ [email protected]
│ │ ├── [email protected]
│ │ └── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ ├─┬ [email protected]
│ │ │ ├── [email protected]
│ │ │ └── [email protected]
│ │ └── [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ ├── [email protected]
│ │ └── [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
│ ├── [email protected]
│ └── [email protected]
├─┬ [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ └── [email protected]
├─┬ [email protected]
│ └─┬ [email protected]
│   ├─┬ [email protected]
│   │ └─┬ [email protected]
│   │   └── [email protected]
│   ├── [email protected]
│   ├─┬ [email protected]
│   │ ├── [email protected]
│   │ └── [email protected]
│   ├── [email protected]
│   └── [email protected]
├─┬ [email protected]
│ ├─┬ [email protected]
│ │ └─┬ [email protected]
│ │   ├─┬ [email protected]
│ │   │ ├── [email protected]
│ │   │ └── [email protected]
│ │   ├── [email protected]
│ │   └─┬ [email protected]
│ │     ├── [email protected]
│ │     └── [email protected]
│ └─┬ [email protected]
│   ├── [email protected]
│   ├── [email protected]
│   ├─┬ [email protected]
│   │ └─┬ [email protected]
│   │   └── [email protected]
│   └── [email protected]
└─┬ [email protected]
  └── [email protected]

Output of cat package.json:

$ cat package.json 
{
  "name": "prepscholar",
  "version": "0.0.0",
  "devDependencies": {
    "grunt": "~0.4.1",
    "grunt-contrib-jshint": "~0.6.4",
    "grunt-dustjs": "~1.1.1",
    "grunt-contrib-watch": "~0.5.3",
    "grunt-contrib-compass": "~0.5.0"
  }
}

Output of cat Gruntfile.js:

$ cat Gruntfile.js 

module.exports = function(grunt) {

  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    jshint: {
      files: ['Gruntfile.js', 'app/js/**/*.js', '!app/js/lib/**/*.js']
    },
    dustjs: {
      compile: {
        src: ['app/templates/**/*.html'],
        dest: 'app/js/templates.js'
      }
    },
    compass: {
      dev: {
        options: {
          sassDir: 'app/sass',
          cssDir: 'app/css',
          imagesDir: 'app/img',
          fontsDir: 'app/fonts',
          javascriptsDir: 'app/js/app',
          outputStyle: 'compressed'
        }
      }
    },
    watch: {
      gruntfile: {
        files: 'Gruntfile.js',
        tasks: ['compile']
      },
      css: {
        files: 'app/sass/**/*.scss',
        tasks: ['compass:dev']
      },
      livereload: {
        options: { livereload: true },
        files: ['app/css/**/*']
      },
      dust: {
        files: 'app/templates/**/*.html',
        tasks: ['dustjs']
      }
    }
  });

  grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.loadNpmTasks('grunt-contrib-compass');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-dustjs');
  grunt.loadNpmTasks('underscore');

  grunt.registerTask('default', ['compile', 'watch']);
  grunt.registerTask('compile', ['dust', 'compass']);
  grunt.registerTask('dust', ['dustjs']);
  grunt.registerTask('lint', ['jshint']);
};
5
  • Can you please post the entire contents of that offending line (findup-sync.js:33)
    – ksimons
    Oct 2, 2013 at 11:26
  • @ksimons thanks for following this. i have posted the offending line + other information that might be useful Oct 2, 2013 at 19:00
  • @ksimons some more information: gruntfile.js and package.json Oct 2, 2013 at 19:03
  • 2
    You need to do: npm install lodash
    – ksimons
    Oct 2, 2013 at 20:53
  • @ksimons still same error after npm install -g lodash as root Oct 2, 2013 at 21:07

4 Answers 4

20

Found that this worked, as per https://github.com/gruntjs/grunt/issues/888

rm -rf node_modules/grunt
npm install grunt

This should work too.

1
  • 1
    Thank you - it worked for me when all the other answers didn't.
    – Daniel
    Mar 3, 2015 at 21:46
19

Try reinstalling your node modules.

  1. Delete the node_modules folder
  2. Do npm cache clean
  3. Do npm install

Hope it helps :)

1
  • rm -rf node_modules && npm cache clean && npm i
    – MDB
    Jan 31, 2017 at 15:24
9

As was written in https://github.com/gruntjs/grunt/issues/888 dist in your .gitignore

Hope that helps

1
  • To clarify: dont just put dist in your .gitignore. Put something as specific as you need it to be. For me, it was ./dist/
    – Alan P.
    Jul 29, 2015 at 20:19
2

That looks like one of your grunt modules is trying to use underscore.js but it's not installed. This should fix your problem:

npm install underscore

Or even better, add underscore as a development dependency in your package.json:

{
  <your existing stuff here>
  "devDependencies": {
    "underscore": "~1.5.2"
  }
}
4
  • it did not help, but +1 anyway Oct 2, 2013 at 6:26
  • Try installing it globally with: npm install -g underscore
    – ksimons
    Oct 2, 2013 at 6:30
  • Ah, I guessed wrong. Try: npm install lodash. Lodash is a drop in replacement for underscore.
    – ksimons
    Oct 2, 2013 at 20:52
  • i logged in as root and installed npm install -g lodash (2.2.0) and it still doesn't work. after that tried as npm install lodash as regular user, and still exactly the same problem. thanks for your continuing suggestions. Oct 2, 2013 at 21:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.