grunt-contrib-imagemin: Fatal error: Cannot read property 'contents' of undefined

i am getting that error after run: grunt imagemin

Running “imagemin:dist” (imagemin) task Fatal error: Cannot read property ‘contents’ of undefined

the funnest part is: i am using the same code and images from other project for test and in that projects everything works perfect. \o/ i tried use --force but keeps the same error, the task minimize one image but not all.

my gruntfile.js //Image min ===============================

            var imagemin;
            config.imagemin = imagemin = {};

                imagemin.dist = {
                              options: {
                              optimizationLevel: 5,
                              progressive: true,
                          },

                        files: [{
                            expand: true,
                            cwd: 'public/images/',
                            src: ['**/*.{png,jpg,gif}'],
                            dest: 'public/images/min'
                        }]
                 }; 

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 93

Commits related to this issue

Most upvoted comments

I resolved this by bumping grunt-contrib-imagemin in package.json to ^1.0.0, then givin’ it the ol’ npm cache clean && npm install grunt-contrib-imagemin.

this fixes this issue… https://github.com/gruntjs/grunt-contrib-imagemin/pull/329 let’s see how long until it gets merged, in the meantime for those who need to deploy you can create a post install script to run the library update with

#! /usr/bin/env bash
cd node_modules/grunt-contrib-imagemin
npm install imagemin@4.0.0
  1. Delete node_modules
  2. Change package.json to use "grunt-contrib-imagemin": "1.0.0" and "vinyl-fs": "2.3.1"
  3. Do npm install

That’s what did it for me.

@lashan These steps worked for me.

npm i -D imagemin vinyl-fs@2.2.1 works for me.

Adding "vinyl-fs": "2.2.1"as devDepenency fixed the problem for me.

Same for me, everything works in an outdated version, but if I run npm install and use new updates will get this error…

I think is something related to the function used to calculate size differences between source and output files.

It works if I check for undefined data[0] in this anonymous function: node_modules/grunt-contrib-imagemin/tasks/imagemin.js:48

the “fix” since the image size difference still won’t be calculated:

if (data[0] === undefined) {
    grunt.warn('could not calculate size difference' + ' in file ' + file.src[0]);
    return next();
}

the anonymous callback function to put the fix:

fs.stat(file.src[0], function (err, stats) {
                if (err) {
                    grunt.warn(err + ' in file ' + file.src[0]);
                    return next();
                }

                imagemin.run(function (err, data) {
                    console.log(arguments);
                    if (err) {
                        grunt.warn(err + ' in file ' + file.src[0]);
                        return next();
                    }

                    var origSize = stats.size;
                    var diffSize = origSize - data[0].contents.length;

                    totalSaved += diffSize;

                    if (diffSize < 10) {
                        msg = 'already optimized';
                    } else {
                        msg = [
                            'saved ' + prettyBytes(diffSize) + ' -',
                            (diffSize / origSize * 100).toFixed() + '%'
                        ].join(' ');
                    }

                    grunt.verbose.writeln(chalk.green('✔ ') + file.src[0] + chalk.gray(' (' + msg + ')'));
                    process.nextTick(next);
                });
            });

Guess why data argument is undefined when err is null I think is the key to really fix this…