Grunt imagemin错误:无法读取未定义

时间:2016-01-12 23:29:45

标签: javascript gruntjs grunt-contrib-imagemin imagemin

当我尝试运行它时,Grunt imagemin会抛出以下错误:

Running "imagemin:dynamic" (imagemin) task
Fatal error: Cannot read property 'contents' of undefined

这是我的package.json文件:

{
  "name": "project1",
  "version": "0.1.0",
  "devDependencies": {
    "grunt": "~0.4.1",
    "grunt-contrib-imagemin": "^1.0.0",
    "grunt-contrib-uglify": "^0.11.0",
    "imagemin" : "4.0.0"
  }
}

这是我的Gruntfile.js

module.exports = function(grunt) {

    grunt.initConfig({

        pkg: grunt.file.readJSON('package.json'),

        uglify: {
          build: {
            src: 'views/js/src/main.js',
            dest: 'views/js/build/main.js'
          }
        },

        imagemin: {
          dynamic: {
            files: [{
              expand: true,
              cwd: 'views/images/src/',
              src: ['**/*.{png,jpg,gif}'],
              dest: 'views/images/build/'
            }]
          }
        }

    });

    // 3. Where we tell Grunt we plan to use this plug-in.
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-imagemin');

    // 4. Where we tell Grunt what to do when we type "grunt" into the terminal.
    grunt.registerTask('default', ['uglify', 'imagemin']);

};

在imagemin的早期版本中,问题是flagged and evidently resolved。但是商定的解决方案是将grunt-contrib-imagemin更新为1.0.0版本,将imagemin更新为4.0.0,我已经完成了它仍然无法正常工作。

4 个答案:

答案 0 :(得分:7)

GitHub解决方案(https://github.com/gruntjs/grunt-contrib-imagemin/issues/344),而不是降级grunt-contrib-imagemin,正在您的package.json中添加"vinyl-fs": "2.2.1"

vinyl-fs似乎在依赖树中的任何位置。但是vinyl-fs从2.2.1到2.3.0的版本发生了重大变化,这将使构建过程失效。因此版本应该“强制”为2.2.1。

答案 1 :(得分:7)

只需将gurnt-contrib-imagemin更新为1.0.0或最新版本,您的旧版本可能是0.9.x

答案 2 :(得分:5)

我通过将package.json中的grunt-contrib-imagemin更改为grunt-contrib-imagemin": "0.9.1"

来解决了这个问题

答案 3 :(得分:1)

我工作时将grunt-contrib-imagemin更新为版本^1.0.0并添加依赖项imagemin版本^4.0.0vinyl-fs版本^2.1.1