Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jimp: image processing fail on module usage

I am unable to use Jimp.

installed: npm install --save jimp //OK

imported: import * as Jimp from 'jimp'; //OK

usage Jimp.read(someImage) //fail with error

Uncaught (in promise): Error: Node's Buffer() not available

like image 455
Thomas T.brhan Avatar asked Mar 10 '26 13:03

Thomas T.brhan


2 Answers

This is a breaking change in Jimp 0.2.28, and the current solution is to revert to 0.2.27. However, as you've noted in the comments, import will not work for [email protected] for TypeScript.

It looks like with TypeScript, require and import accomplish two different things. So with import, you need to have a declaration file declaring the module so it can determine what the module is at compile-time. If you don't have that, TypeScript will not be able to resolve it at all. require happens at runtime, so TypeScript is uninvolved (and you won't have type information with required modules).

This answer explains it better than I could.

It looks like Jimp 0.2.27 does not define a .d.ts file while 0.2.28 does. I'm unsure why they included so many changes in one patch.

like image 177
River Tam Avatar answered Mar 12 '26 03:03

River Tam


I eventually found the solution :

const JimpLib = require('jimp'); console.log('JimpLib', Object.keys(JimpLib)); // returns different lib features such as loadFont

const Jimp = JimpLib.Jimp; // returns our usual Jimp class representing an image var img = await Jimp.read('/path/image.png');

like image 22
Vincent Moreau Avatar answered Mar 12 '26 02:03

Vincent Moreau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!