最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Grunt imagemin error: Cannot read property 'contents' of undefined - Stack Overflow

programmeradmin2浏览0评论

Grunt imagemin throws the following error when I try to run it:

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

Here's my package.json file:

{
  "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"
  }
}

And here's my 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']);

};

The issue was flagged and evidently resolved in a prior version of imagemin. But the agreed upon solution was to update grunt-contrib-imagemin to version 1.0.0 and imagemin to 4.0.0, which I've done and it still isn't working.

Grunt imagemin throws the following error when I try to run it:

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

Here's my package.json file:

{
  "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"
  }
}

And here's my 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']);

};

The issue was flagged and evidently resolved in a prior version of imagemin. But the agreed upon solution was to update grunt-contrib-imagemin to version 1.0.0 and imagemin to 4.0.0, which I've done and it still isn't working.

Share Improve this question asked Jan 12, 2016 at 23:29 Justin SmithJustin Smith 3851 gold badge4 silver badges18 bronze badges 2
  • 1 The grunt-contrib-imagemin to version 1.0.0 and imagemin to 4.0.0 was working for me yesterday and now it's not working anymore. – Drew Commented Jan 13, 2016 at 1:14
  • There's a GitHub ticket for that, too: github.com/gruntjs/grunt-contrib-imagemin/issues/344 – Matthias Lohr Commented Jan 13, 2016 at 8:54
Add a comment  | 

4 Answers 4

Reset to default 8

Just update the gurnt-contrib-imagemin to 1.0.0 or latest, you may be having older version probably 0.9.x

The GitHub solution (https://github.com/gruntjs/grunt-contrib-imagemin/issues/344), instead of downgrading grunt-contrib-imagemin, is adding "vinyl-fs": "2.2.1" in your package.json.

vinyl-fs seems to be anywhere in the dependency tree. But there was a breaking version change of vinyl-fs from 2.2.1 to 2.3.0, which will brake the build process. So the version should be "forced" to 2.2.1.

I resolved the issue by changing my grunt-contrib-imagemin in my package.json to grunt-contrib-imagemin": "0.9.1"

For me worked updating grunt-contrib-imagemin to version ^1.0.0 and adding dependencies imagemin version ^4.0.0 and vinyl-fs version ^2.1.1

发布评论

评论列表(0)

  1. 暂无评论