Grunt watch无法重新加载配置文件 - 更改时出错

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Grunt watch无法重新加载配置文件 - 更改时出错相关的知识,希望对你有一定的参考价值。

环境是:Linux 2.6.32-573.12.1.el6.x86_64 x86_64,通过SSH

对于我的咕噜声,我有一个略微非标准的设置,详情如下:

/home/scoots/public_html/
    grunt/
        package.json
        node_modules/
        project1/
            Gruntfile.js
        project2/
            Gruntfile.js
    project1/
    project2/

当我开始grunt监视并对我的Gruntfile.js进行更改时(这不是常见的情况,但仍然可能根据项目的需要而发生),我收到以下错误:

$ cd ~/public_html/grunt/project1
$ grunt watch
Running "watch" task
Waiting...

Reloading watch config...
Fatal error: EACCES: permission denied, scandir '/home'

这很简单(只需取消并重启grunt) - 但这需要我注意。我有时可以尝试继续工作,但是现在可以锁定咕噜咕噜的手表。

此外,当我在本地(Windows)计算机上安装了grunt并从Node.js命令提示符运行它时,这段代码常常会起作用。

作为参考,这是一个展示此问题的示例Gruntfile.js:

module.exports = function(grunt) {
    require('time-grunt')(grunt);
    require('jit-grunt')(grunt);
    grunt.file.setBase('../');

    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        site_dir: {
            site: '../tcl/wp-content/themes/twentysixteen-child',
            jscache: 'wp-content/themes/twentysixteen-child/js/src',
            grunt: 'tcl'
        },
        watch: {
            css: {
                files: ['<%= site_dir.site %>/css/sass/*.scss'],
                tasks: ['sass', 'postcss'],
                options: {
                    spawn: false
                }
            },
            scripts: {
                files: ['<%= site_dir.site %>/js/src/*.js'],
                tasks: ['include_file', 'uglify'],
                options: {
                    spawn: false
                }
            },
            configFiles: {
                files: ['<%= site_dir.grunt %>/Gruntfile.js'],
                options: {
                    reload: true
                }
            }
        },
        sass: {
            options: {
                style: 'expanded', // compressed in postcss
                sourcemap: true,
                sourceMapEmbed: true,
                precision: 8,
                includePaths: ['node_modules/bootstrap-sass/assets/stylesheets']
            },
            dist: {
                files: {
                    '<%= site_dir.grunt %>/css/layout.css': '<%= site_dir.site %>/css/sass/layout.scss'
                }
            }
        },
        postcss: {
            options: {
                map: {
                    inline: false,
                    annotation: '<%= site_dir.grunt %>/css/maps/'
                },
                processors: [
                    require('autoprefixer')({
                        browsers: [
                          "android 2.3",
                          "Android >= 4",
                          "Chrome >= 20",
                          "Firefox >= 24",
                          "Explorer >= 8",
                          "ios >= 6",
                          "Opera >= 12",
                          "Safari >= 6"
                        ]
                    }),
                    require('csswring')()
                ]
            },
            dist: {
                src: '<%= site_dir.grunt %>/css/layout.css',
                dest: '<%= site_dir.site %>/css/layout.min.css'
            }
        },
        include_file: {
            js: {
                cwd: '<%= site_dir.grunt %>/',
                src: [
                    '../<%= site_dir.site %>/js/src/common.js'
                ],
                dest: '<%= site_dir.grunt %>/js/'
            }
        },
        uglify: {
            options: {
                compress: {
                    warnings: true
                },
                mangle: true,
                preserveComments: /(?:^!|@(?:license|preserve|cc_on))/
            },
            build: {
                src: [
                    '<%= site_dir.grunt %>/<%= site_dir.jscache %>/common.js'
                ],
                dest: '<%= site_dir.site %>/js/common.min.js'
            }
        }
    });

    grunt.registerTask('default', ['sass', 'postcss', 'include_file', 'uglify']);
};

和package.json:

{
  "name": "Test",
  "version": "0.1.0",
  "dependencies": {
    "bootstrap-sass": "3.3.7"
  },
  "devDependencies": {
    "autoprefixer": "^6.4.1",
    "csswring": "^5.1.0",
    "grunt": "^1.0.1",
    "grunt-contrib-uglify": "^2.0.0",
    "grunt-contrib-watch": "^1.0.0",
    "grunt-include-file": "^0.1.3",
    "grunt-postcss": "^0.8.0",
    "grunt-sass": "^2.0.0",
    "jit-grunt": "^0.10.0",
    "time-grunt": "^1.4.0"
  }
}

编辑:启用详细/调试:

$ grunt -v -d watch
Initializing
Command-line options: --verbose, --debug=1

Reading "Gruntfile.js" Gruntfile...OK

Registering Gruntfile tasks.
Reading package.json...OK
Parsing package.json...OK
Initializing config...OK
Loading "Gruntfile.js" tasks...OK
+ default

Running tasks: watch

Loading "grunt-contrib-watch" plugin

Registering "/home/scoots/public_html/grunt/node_modules/grunt-contrib-watch/tasks" tasks.
Loading "watch.js" tasks...OK
+ watch

Running "watch" task
[D] Task source: /home/scoots/public_html/grunt/node_modules/grunt-contrib-watch/tasks/watch.js
Waiting...
Verifying property watch exists in config...OK
Verifying property watch.css.files exists in config...OK
Verifying property watch.scripts.files exists in config...OK
Verifying property watch.config.files exists in config...OK
Watching ../tcl/wp-content/themes/twentysixteen-child/css/sass/__bookings.scss for changes.
Watching ../tcl/wp-content/themes/twentysixteen-child/css/sass/__variables.scss for changes.
Watching ../tcl/wp-content/themes/twentysixteen-child/css/sass/_bootstrap-custom.scss for changes.
Watching ../tcl/wp-content/themes/twentysixteen-child/css/sass/_contactform7.scss for changes.
Watching ../tcl/wp-content/themes/twentysixteen-child/css/sass/_lightslider.scss for changes.
Watching ../tcl/wp-content/themes/twentysixteen-child/css/sass/_lity.scss for changes.
Watching ../tcl/wp-content/themes/twentysixteen-child/css/sass/_zoom.scss for changes.
Watching ../tcl/wp-content/themes/twentysixteen-child/css/sass/layout.scss for changes.
Watching ../tcl/wp-content/themes/twentysixteen-child/js/src/bootstrap.js for changes.
Watching ../tcl/wp-content/themes/twentysixteen-child/js/src/common.js for changes.
Watching ../tcl/wp-content/themes/twentysixteen-child/js/src/instafeed.js for changes.
Watching ../tcl/wp-content/themes/twentysixteen-child/js/src/lightslider.js for changes.
Watching ../tcl/wp-content/themes/twentysixteen-child/js/src/lity.js for changes.
Watching ../tcl/wp-content/themes/twentysixteen-child/js/src/scrollreveal.min.js for changes.
Watching ../tcl/wp-content/themes/twentysixteen-child/js/src/zoom.js for changes.
Watching tcl/Gruntfile.js for changes.
Watching tcl/css for changes.
Watching tcl/js for changes.
Watching tcl/wp-content for changes.
Clearing require cache for "tcl/Gruntfile.js" file...OK

[[[[I edit the Gruntfile here]]]]

Reloading watch config...
Fatal error: EACCES: permission denied, scandir '/home'
答案

文件glob中的前导斜杠有时会导致此错误,但是grunt不会抛出任何错误。这个警告只是一个沉默的失败。

确保您的“文件”配置不会导致此问题,并且此<%= site_dir.site%>模板不会导致null或为空。

watch: {
            css: {
                files: ['<%= site_dir.site %>/css/sass/*.scss'],
                tasks: ['sass', 'postcss'],
                options: {
                    spawn: false
                }
            },
            scripts: {
                files: ['<%= site_dir.site %>/js/src/*.js'],
                tasks: ['include_file', 'uglify'],
                options: {
                    spawn: false
                }
            },
            configFiles: {
                files: ['<%= site_dir.grunt %>/Gruntfile.js'],
                options: {
                    reload: true
                }
            }
        }

以上是关于Grunt watch无法重新加载配置文件 - 更改时出错的主要内容,如果未能解决你的问题,请参考以下文章

带有 grunt-nodemon、watch 和 jshint 的 Gruntjs

文件更改时如何自动重新加载Django?

grunt使用watch和livereload的Gruntfile.js的配置

Grunt 之 watch 和 livereload

在TypeScript中保存代码后是否可以运行grunt命令?

如何与 grunt 共享配置?