节点Grunt.js'警告:未定义连接使用'

时间:2014-04-26 23:28:35

标签: javascript node.js express gruntjs

我正在创建一个基本的grunt文件来启动我的express.js服务器。我知道有一个插件只是为了这个,但出于学习目的,我想手工完成。当我运行grunt时,我收到一条连接未定义的消息。但是,就我所知,它已经定义了。

错误讯息:

one@localhost ~/app/yo $ grunt 
Running "default" task
Warning: connect is not defined Use --force to continue.

Aborted due to warnings.
one@localhost ~/app/yo $ 

Gruntfile.js:

module.exports = function(grunt) {
    require('load-grunt-tasks')(grunt);
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),

        watch: {
            servedFiles: {
                files: '<%= pkg.name %>/static/**',
                options: {
                  livereload: true
                } 
            },
        },

        connect: {
          options: {
            port: 8000,
            hostname: '0.0.0.0',
            livereload: 35729
            },
        },
    });
    grunt.registerTask('default', 'start server', function() {
        grunt.task.run(connect);
    }); 
}  

Grunt模块:

one@localhost ~/app/yo $ lr node_modules/
total 68k
drwxr-xr-x 8 one users  8 Apr 26 17:54 .
drwxr-xr-x 4 one users  7 Apr 26 18:23 ..
drwxr-xr-x 2 one users  3 Apr 25 21:11 .bin
drwxr-xr-x 5 one users 13 Apr 25 21:11 express
drwxr-xr-x 5 one users  9 Apr 26 17:54 grunt
drwxr-xr-x 4 one users  7 Apr 26 17:54 grunt-contrib-connect
drwxr-xr-x 4 one users  7 Apr 26 17:54 grunt-contrib-watch
drwxr-xr-x 3 one users  6 Apr 25 21:37 load-grunt-tasks
one@localhost ~/app/yo $ 

1 个答案:

答案 0 :(得分:3)

错误是因为connect未被定义为变量:

grunt.task.run(connect);
//             ^ ReferenceError

如果首先声明它,它可以用作一个:

var connect = 'connect';

但是,它应该是Grunt可用于查找已注册任务的String值:

grunt.task.run('connect');