node.js - 如何让 grunt watch 和 grunt nodemon 一起工作

标签 node.js gruntjs grunt-contrib-watch

我在node.js中有一个Web应用程序,我想用nodemon启动,因此每次主脚本更改时,Web应用程序都可以重新启动。 同时,我有我的 CoffeeScript 文件,每次发生任何更改时我都需要重新编译。 我设置了一个 grunt-contrib-watch 任务来监听 app/frontend/*.coffee 文件,以调度咖啡解析器。 然而,这似乎并没有发生,因为 nodemon 任务也在监听。 我在nodemonignore中设置了app/frontend/文件夹。 我还设置了nodemon并观察并发。 尽管如此,每次我编辑 CoffeeScript 时,咖啡任务都没有被执行。

这是我的 Gruntfile

module.exports = function(grunt) {

  // Project configuration.
  grunt.initConfig({
    concurrent: {
        dev: [
            'nodemon',
            'watch'
        ],
        options: {
            logConcurrentOutput: true
        }
    },
    coffee: {
      compile: {
        files: {
          'app/public/app.js': ['app/frontend/*.coffee']
        }
      }
    },
    nodemon: {
      dev: {
        script: 'app/index.js',
        options: {
          ignore: ['app/frontend/**', 'app/public/**']
        }
      }
    },
    watch: {
      scripts: {
        files: 'app/frontend/*.coffee',
        tasks: ['coffee'],
        options: {
          spawn: false
        }
      }
    }
  });

  grunt.loadNpmTasks('grunt-concurrent');
  grunt.loadNpmTasks('grunt-contrib-coffee');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-nodemon');

  // Default task(s).
  grunt.registerTask('default', ['coffee', 'nodemon', 'watch']);

};

最佳答案

您的 Gruntfile 指示 Grunt 作为默认任务按顺序运行 nodemonwatch(因此 watch 永远不会运行,因为 nodemon 永远不会完成)。

您需要在最后一行显式包含并发任务:

grunt.registerTask('default', ['coffee', 'concurrent']);

关于node.js - 如何让 grunt watch 和 grunt nodemon 一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33557351/

相关文章:

node.js - 全栈 Angular 和meanjs之间的区别

node.js - 抓取并迭代没有类的 html 表

javascript - browserify无法调用我捆绑的函数,它没有定义

javascript - 使用 Grunt 时,BrowserSync 在任务完成之前重新加载浏览器

macos - Yeoman 返回 'env: node\r: No such file or directory'

gruntjs - 为什么 grunt-contrib-watch livereload 不起作用?

javascript - 将 getItem 与主键和排序键一起使用

javascript - 使用 grunt-contrib-watch 深度单向同步两个目录。代码有效,但 grunt-contrib-watch 重新初始化时间太慢

javascript - 使用 grunt 更新我的 css 文件

gruntjs - grunt-contrib-watch + grunt-rsync