node.js - 带有 grunt-nodemon、watch 和 jshint 的 Gruntjs

标签 node.js gruntjs nodemon

我正在尝试使用这 3 个插件运行 GruntJS,以便它可以监视更改,并且首先:检查文件,然后重新加载 express 服务器。 我在下面的配置中遇到的问题是,如果 jshint 对文件进行 lint,nodemon 不会运行,反之亦然。

// Gruntfile.js

// our wrapper function (required by grunt and its plugins)
// all configuration goes inside this function
module.exports = function(grunt) {

  // ===========================================================================
  // CONFIGURE GRUNT ===========================================================
  // ===========================================================================
  grunt.initConfig({

    // get the configuration info from package.json ----------------------------
    // this way we can use things like name and version (pkg.name)
    pkg: grunt.file.readJSON('package.json'),

    // all of our configuration will go here

    // configure jshint to validate js files -----------------------------------
  jshint: {
      options: {
        reporter: require('jshint-stylish') // use jshint-stylish to make our errors look and read good
      },

    // when this task is run, lint the Gruntfile and all js files in src
      build: ['Grunfile.js', 'routes/*.js']
    },

    watch: {

      // for scripts, run jshint and uglify
      scripts: {
        files: 'routes/*.js',
        tasks: ['jshint']
      }
    },

    concurrent: {
      dev: {

        tasks: ['jshint', 'nodemon', 'watch'],
        options: {
          logConcurrentOutput: true
        }
      }
    }, // concurrent

    nodemon: {
      dev: {
        script: './server.js'
      }
    } // nodemon


  });

  // ===========================================================================
  // LOAD GRUNT PLUGINS ========================================================
  // ===========================================================================
  // we can only load these if they are in our package.json
  // make sure you have run npm install so our app can find these
  grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-nodemon');


      grunt.registerTask('default', '', function() {
    var taskList = [
        'jshint',
        'nodemon',
        'watch'
    ];
    grunt.task.run(taskList);
});

};

编辑(澄清):

我第一次运行 grunt 时,jshint 对文件进行 lint,然后 nodemon 启动,jshint 不再进行 lint。

输出:

grunt
Running "default" task

Running "jshint:build" (jshint) task

✔︎ No problems


Running "nodemon:dev" (nodemon) task
[nodemon] v1.2.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node ./server.js`
Express server listening on port 3000

最佳答案

真是个愚蠢的错误。 我没有加载 grunt-concurrent,只是安装了 grunt-concurrent 并将其添加到 Kelz 的函数中,现在它可以工作了:)。 谢谢大家。

最终代码:

// Gruntfile.js

// our wrapper function (required by grunt and its plugins)
// all configuration goes inside this function
module.exports = function(grunt) {
  // ===========================================================================
  // CONFIGURE GRUNT ===========================================================
  // ===========================================================================
  grunt.initConfig({

    // get the configuration info from package.json ----------------------------
    // this way we can use things like name and version (pkg.name)
    pkg: grunt.file.readJSON('package.json'),

    // all of our configuration will go here

    // configure jshint to validate js files -----------------------------------
    jshint: {
      options: {
        reporter: require('jshint-stylish') // use jshint-stylish to make our errors look and read good
      },

      // when this task is run, lint the Gruntfile and all js files in src
      build: ['Grunfile.js', 'routes/*.js']
    },

    watch: {
      // for scripts, run jshint and uglify
      scripts: {
        files: 'routes/*.js',
        tasks: ['jshint']
      }
    }, // watch

    nodemon: {
      dev: {
        script: './server.js'
      }
    }, // nodemon

    concurrent: {
      dev: {
        tasks: ['jshint', 'nodemon', 'watch'],
        options: {
          logConcurrentOutput: true
        }
      }
    } // concurrent
  });

  // ===========================================================================
  // LOAD GRUNT PLUGINS ========================================================
  // ===========================================================================
  // we can only load these if they are in our package.json
  // make sure you have run npm install so our app can find these
  grunt.loadNpmTasks('grunt-concurrent');
  grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-nodemon');

  grunt.registerTask('default', '', function() {
    var taskList = [
        'concurrent',
        'jshint',
        'nodemon',
        'watch'
    ];
    grunt.task.run(taskList);
  });
};

关于node.js - 带有 grunt-nodemon、watch 和 jshint 的 Gruntjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25073931/

相关文章:

node.js - 带列表的 Redis 发布/订阅

javascript - 如何以编程方式读取和解析 gruntfile 以便我可以修改它并再次保存?

gruntjs - VS 2019 gruntfile.js 使用 'sass = require(' node-sass')' 时找不到任务

javascript - 即使单元测试失败,TravisCI 也不会失败

node.js - 带有 IcedCoffeeScript 的 Nodemon

node.js - VS Code 没有为在 Docker 容器中运行的 Node 应用程序命中断点

node.js - 无法将 google-play-scraper 的结果分配给变量

node.js - Express 4-如何使用Session设置额外的自定义cookie?

node.js - 重置已运行的 cron-node 作业的计时器

javascript - nodemon-检查特定文件的更改