Grunt.js:加载任务导致TypeError:object不是函数

时间:2014-01-15 18:40:16

标签: javascript node.js coffeescript gruntjs

我正在尝试执行我的grunt watch任务但grunt会抛出错误:

TypeError: object is not a function

我的咖啡脚本编译任务失败了:

 coffee: 
     compile: 
         files: 'path/to/result.js': 'path/to/source.coffee' 

它非常基本,所以我看不出那里出了什么问题。任何想法可能会发生什么?

这是在运行grunt watch -v

之后产生的堆栈跟踪
agconti :: ~/dev/my_project ‹master*› » grunt watch -v                                                                                                                3 ↵
Initializing
Command-line options: --verbose

Reading "Gruntfile.coffee" Gruntfile...OK

Registering Gruntfile tasks.
Reading package.json...OK
Parsing package.json...OK
Loading "Gruntfile.coffee" tasks...ERROR
>> TypeError: object is not a function
>>     at Object.module.exports (/Users/admin/dev/fueled-boilerplate/Gruntfile.coffee:58:88)
>>     at loadTask (/Users/admin/dev/fueled-boilerplate/node_modules/grunt/lib/grunt/task.js:318:10)
>>     at Task.task.init (/Users/admin/dev/fueled-boilerplate/node_modules/grunt/lib/grunt/task.js:430:5)
>>     at Object.grunt.tasks (/Users/admin/dev/fueled-boilerplate/node_modules/grunt/lib/grunt.js:113:8)
>>     at Object.module.exports [as cli] (/Users/admin/dev/fueled-boilerplate/node_modules/grunt/lib/grunt/cli.js:38:9)
>>     at Object.<anonymous> (/usr/local/lib/node_modules/grunt-cli/bin/grunt:43: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)

这是整个Gruntfile.coffee:

module.exports = (grunt) ->

# Project configuration.
    grunt.initConfig
        pkg: grunt.file.readJSON 'package.json'

# CSS processing
        sass: 
            dist: 
                options: 
                    style: 'expanded'
                files: 
                    'assets/build/css/compiled/screen.css' : 'assets/css/screen.scss'

        compass: 
            options: 
                sassDir: 'assets/css/'
                cssDir: 'assets/build/css/compiled/'
                imagesDir: 'assets/images/'
                javascriptsDir: 'assets/build/js/'
                outputStyle: 'expanded'

            dist:""
            server: 
                options: 
                    debugInfo: true

        autoprefixer: 
            options: 
                browsers: ['last 2 version']

            multiple_files:
                expand: true,
                flatten: true,
                src: 'assets/build/css/compiled/screen.css'
                dest: 'assets/build/css/prefixed/'

        modernizr: 
            "devFile": "assets/js/modernizr/modernizr-2.6.2.min.js"
            "outputFile": "assets/build/js/modernizr-custom.js"

        cssmin: 
            combine: 
                files:'assets/build/css/screen.min.css': ['assets/build/css/prefixed/screen.css']

# JavaScript Processing
        coffee: 
            compile: 
                files: 'path/to/result.js': 'path/to/source.coffee' 

        jshint: 
            beforeconcat: ['assets/js/*.js']

        concat: 
            dist: 
                src: [
                    'assets/js/vendor/*.js'
                    'assets/js/*.js']
                dest: 'assets/build/js/main.js'

        uglify: 
            build: 
                src: 'assets/build/js/main.js',
                dest: 'assets/build/main.min.js'

# Image Processing
        imagemin: 
            dynamic: 
                files: [
                    expand: true
                    cwd: "assets/images/"
                    src: ['**/*.{png,gif,jpg}']
                    dest: "assets/images/"
                    ]

# Watch Task
        watch: 
            options:
                livereload: 
                    port: 9000 # Allows you to specify port incase you want to run multiple projects as once.
                    #Allows you to connect an Https server and still have livereload 
                    key: grunt.file.read 'path/to/ssl.key'
                    cert: grunt.file.read 'path/to/ssl.crt'
                    #you can pass in any other options you'd like to the https server, as listed here: http://nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener
            scripts: 
                files: 'assets/js/*/js'
                tasks: ['concat', 'uglify', 'jshint']
                options: 
                    spawn: false

            html: 
                files: ['*.html', '**/*.html']
                tasks: []
                options: 
                    spawn: false


            compass: 
                files: ['assets/css/*.scss', 'assets/css/**/*.scss']
                tasks: ['compass:server', 'autoprefixer', 'cssmin', 'clean']

            images: 
                files: ['assets/images/**/*.{png,gif,jpg}', 'assets/images/*.{png,gif,jpg}']
                tasks: ['imagemin']
                options: 
                    spawn: false

# Connect Task
        connect: 
            server: 
                options: 
                    port: 8000
                    base: './'

# Clean Task
        clean: ['assets/build/css/prefixed/', 'assets/build/css/compiled/']


# Load dependencies
    require('load-grunt-tasks')(grunt)

# Define Tasks
    grunt.registerTask 'default', [
                                    'concat' 
                                    'uglify' 
                                    'sass' 
                                    'imagemin'
                                ]

    grunt.registerTask 'dev', [
                                'connect' 
                                'modernizr' 
                                'watch'
                            ]

加载grunt任务文件:

'use strict';
var globule = require('globule');
var findup = require('findup-sync');
var path = require('path');

function arrayify(el) {
    return Array.isArray(el) ? el : [el];
}

module.exports = function (grunt, options) {
    options = options || {};

    var pattern = arrayify(options.pattern || ['grunt-*']);
    var config = options.config || findup('package.json');
    var scope = arrayify(options.scope || ['dependencies', 'devDependencies', 'peerDependencies']);

    if (typeof config === 'string') {
        config = require(path.resolve(config));
    }

    pattern.push('!grunt', '!grunt-cli');

    var names = scope.reduce(function (result, prop) {
        return result.concat(Object.keys(config[prop] || {}));
    }, []);

    globule.match(pattern, names).forEach(grunt.loadNpmTasks);
};

的package.json:

{
  "name": "fueled-boilerplate",
  "version": "0.1.0",
  "devDependencies": {
    "grunt": "~0.4.1",
    "grunt-contrib-concat": "~0.3.0",
    "grunt-contrib-uglify": "~0.2.7",
    "grunt-contrib-sass": "~0.6.0",
    "grunt-contrib-imagemin": "~0.4.0",
    "grunt-contrib-watch": "~0.5.3",
    "grunt-contrib-connect": "~0.5.0",
    "grunt-autoprefixer": "~0.5.0",
    "grunt-contrib-cssmin": "~0.7.0",
    "grunt-contrib-jshint": "~0.7.2",
    "grunt-contrib-clean": "~0.5.0",
    "grunt-contrib-compass": "~0.7.0",
    "grunt-modernizr": "~0.4.1",
    "grunt-contrib-coffee": "~0.8.0"
  },
  "dependencies": {
    "load-grunt-tasks": "~0.2.0"
  }
}

2 个答案:

答案 0 :(得分:8)

尝试彻底安装npm模块:

rm -rf node_modules && npm cache clean && npm install

答案 1 :(得分:0)

我遇到了同样的问题并设法通过替换代码标识中的标签空间(下面的点)来修复它。

 coffee: 
 ....compile: 
 ........files: 'path/to/result.js': 'path/to/source.coffee'