Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fatal error: Cannot read property 'length' of null #320

Closed
gabrielmaldi opened this issue Sep 17, 2015 · 8 comments
Closed

Fatal error: Cannot read property 'length' of null #320

gabrielmaldi opened this issue Sep 17, 2015 · 8 comments

Comments

@gabrielmaldi
Copy link

I'm getting Fatal error: Cannot read property 'length' of null on images that I've optimized with TinyPNG (but aren't corrupt). Here's an example image:

myimage

ImageMagick's identify outputs myImage.png PNG 29x29 29x29+0+0 8-bit sRGB 28c 409B 0.000u 0:00.000 so the image appears to be valid, but imagemin has trouble with it.

I'm using grunt-contrib-imagemin v0.9.4.

Thanks!

@sindresorhus
Copy link
Member

Does this still happen with 1.0.0?

@nleclerc
Copy link

I get the same error on 1.0.0.

@nleclerc
Copy link

Ah no, not same, it's property 'contents' of undefined.
Caused by a png file also.

@acasademont
Copy link

same here :(

@acasademont
Copy link

btw, it seems to be working on my Debian 7 environment, but 8 fails, weird, any hints?

@davidmaxwaterman
Copy link

It's probably a different situation, but I figured I'd add this here in case anyone else comes across it.

I had this error and through adding console.log()s to print the filename in the imagemin task (node_modules/grunt-contrib-imagemin/tasks/imagemin.js), I found that the error was produced when the task was given a directory rather than a file (ie app/images). This was due to lines in my Gruntfile :

        files: [
          { expand: true, cwd: '.', src: ['app/images/**'], dest: 'build/' },
          { expand: true, cwd: '.', src: ['app/css/images/**'], dest: 'build/' }
        ]

When I changes them to (note the extra /* on the src):

        files: [
          { expand: true, cwd: '.', src: ['app/images/**/*'], dest: 'build/' },
          { expand: true, cwd: '.', src: ['app/css/images/**/*'], dest: 'build/' }
        ]

all worked as before.

@vivekkodira1
Copy link
Contributor

vivekkodira1 commented May 12, 2016

I was still encountering this error despite changing the path as above. Discovered that data[0].contents is returned as null sometimes. Changed the following line to get past it.

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

After
var diffSize = origSize - ( ( data[0].contents && data[0].contents.length) || 0 );

vivekkodira1 added a commit to vivekkodira1/grunt-contrib-imagemin that referenced this issue May 12, 2016
Sometimes data[0].contents is returned as null. This adds a null check to avoid a runtime exception
@kevva
Copy link
Member

kevva commented May 23, 2016

Fixed in 92963f1.

@kevva kevva closed this as completed May 23, 2016
kevva pushed a commit that referenced this issue May 23, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants