javascript - Node Grunt.js 'Warning: connect is not defined Use '

标签 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 $ 

最佳答案

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

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

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

var connect = 'connect';

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

grunt.task.run('connect');

关于javascript - Node Grunt.js 'Warning: connect is not defined Use ',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23317629/

相关文章:

javascript - 如何在 Node 模块中构造类?

javascript - Nodejs 函数在内部函数完成之前返回

javascript - 什么是用户代理字符串(github api)

javascript - 当我在 Javascript 中返回 true 时,它​​返回未定义

node.js - 检测 NodeJS 中的 GRPC 服务器故障

javascript - 在 Express JS 中上传和调整图像大小时遇到​​问题

express - Passport js中的闪现消息

node.js - 如何在 MongoDB collection.find() 上获取回调

javascript - 获取 Indexeddb 中未保存记录的数量

javascript - 在 Node.js 服务器响应中从 MongoDB 获取和发送元素