Justin Smith
Justin Smith

Reputation: 385

Grunt imagemin error: Cannot read property 'contents' of undefined

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.

Upvotes: 9

Views: 4693

Answers (4)

Imal Hasaranga Perera
Imal Hasaranga Perera

Reputation: 10029

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

Upvotes: 8

A.Stula
A.Stula

Reputation: 11

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

Upvotes: 1

Matthias Lohr
Matthias Lohr

Reputation: 1856

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.

Upvotes: 7

Drew
Drew

Reputation: 524

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

Upvotes: 5

Related Questions