首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >遇到一个奇怪的错误: Object Gruntfile.js没有方法'flatten‘

遇到一个奇怪的错误: Object Gruntfile.js没有方法'flatten‘
EN

Stack Overflow用户
提问于 2013-10-02 12:30:09
回答 4查看 18K关注 0票数 16

我在尝试运行grunt时遇到了这个奇怪的错误:TypeError: Object Gruntfile.js has no method 'flatten'

我是新手node.js,npm,grunt等。我认为我做了一个很好的节点,npm,grunt安装,但可能是我错过了一些东西。有没有办法验证安装??

$ 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)

有问题的行/文件:

/*
 * 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;
};

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

npm list的输出

$ npm list
prepscholar@0.0.0 /home/a/prep/main/web/client
├─┬ grunt@0.4.1
│ ├── async@0.1.22
│ ├── coffee-script@1.3.3
│ ├── colors@0.6.2
│ ├── dateformat@1.0.2-1.2.3
│ ├── eventemitter2@0.4.13
│ ├─┬ findup-sync@0.1.2
│ │ └── lodash@1.0.1
│ ├─┬ glob@3.1.21
│ │ ├── graceful-fs@1.2.3
│ │ └── inherits@1.0.0
│ ├── hooker@0.2.3
│ ├── iconv-lite@0.2.11
│ ├─┬ js-yaml@2.0.5
│ │ ├─┬ argparse@0.1.15
│ │ │ ├── underscore@1.4.4
│ │ │ └── underscore.string@2.3.3
│ │ └── esprima@1.0.4
│ ├── lodash@0.9.2
│ ├─┬ minimatch@0.2.12
│ │ ├── lru-cache@2.3.1
│ │ └── sigmund@1.0.0
│ ├─┬ nopt@1.0.10
│ │ └── abbrev@1.0.4
│ ├─┬ rimraf@2.0.3
│ │ └── graceful-fs@1.1.14
│ ├── underscore.string@2.2.1
│ └── which@1.0.5
├─┬ grunt-contrib-compass@0.5.0
│ ├── async@0.2.9
│ ├── dargs@0.1.0
│ └── tmp@0.0.21
├─┬ grunt-contrib-jshint@0.6.4
│ └─┬ jshint@2.1.10
│   ├─┬ cli@0.4.5
│   │ └─┬ glob@3.2.6
│   │   └── inherits@2.0.1
│   ├── console-browserify@0.1.6
│   ├─┬ minimatch@0.2.12
│   │ ├── lru-cache@2.3.1
│   │ └── sigmund@1.0.0
│   ├── shelljs@0.1.4
│   └── underscore@1.4.4
├─┬ grunt-contrib-watch@0.5.3
│ ├─┬ gaze@0.4.2
│ │ └─┬ globule@0.1.0
│ │   ├─┬ glob@3.1.21
│ │   │ ├── graceful-fs@1.2.3
│ │   │ └── inherits@1.0.0
│ │   ├── lodash@1.0.1
│ │   └─┬ minimatch@0.2.12
│ │     ├── lru-cache@2.3.1
│ │     └── sigmund@1.0.0
│ └─┬ tiny-lr@0.0.4
│   ├── debug@0.7.2
│   ├── faye-websocket@0.4.4
│   ├─┬ noptify@0.0.3
│   │ └─┬ nopt@2.0.0
│   │   └── abbrev@1.0.4
│   └── qs@0.5.6
└─┬ grunt-dustjs@1.1.1
  └── dustjs-linkedin@2.0.3

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

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']);
};
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19130018

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档