1

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

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Thomas T.brhan
  • 63
  • 2
  • 10
  • This seems to be a [bug in jimp](https://github.com/oliver-moran/jimp/issues/280). A quick read tells me the only available solution right now is to lock your jimp version to 0.2.27 or earlier, as it is a regression. I haven't tried this, though. edit: Clarification: Might not be a bug, but rather an intended change oddly introduced in a patch. – River Tam May 31 '18 at 15:56
  • i did try version 0.2.27 i had an error `Uncaught (in promise): TypeError: Jimp.read is not a function` – Thomas T.brhan May 31 '18 at 16:12
  • Something you haven't posted must be going wrong. I just put jimp@0.2.27 in my application, imported in in the browser as you are, and `console.log(Jimp.read)` and got a function back. – River Tam May 31 '18 at 16:21
  • i tried console.log(Jimp) got {} empty object. how do u import it. `import * as Jimp from 'jimp';` – Thomas T.brhan May 31 '18 at 16:35
  • I'm using `import * as Jimp from 'jimp'` and that's working fine. I'm using webpack, but it should not be different in TypeScript/Angular. What happens if you try `require('jimp')`? You might also try `import Jimp from 'jimp'`, which may work because Jimp is exported as CommonJS. I believe either should work. – River Tam May 31 '18 at 16:35
  • `const Jimp = require('jimp');` importing it this way worked for me. can u give a good explanation in the answer section why this works. thankyou. – Thomas T.brhan May 31 '18 at 16:45

3 Answers3

1

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 Jimp@0.2.27 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.

River Tam
  • 3,096
  • 4
  • 31
  • 51
0

Get the compiler error, when doing import * as Jimp from 'jimp';. Turns out Jimp's @type file uses export = syntax, So have to import like below:

import Jimp = require('jimp');

LeOn - Han Li
  • 9,388
  • 1
  • 65
  • 59
-1

In a nodeJS environment and a pure .js file you can do:

import * as JimpObj from 'jimp';

const Jimp = JimpObj.default;


async function analizeImages() {
    const image = await Jimp.read(`image.png`);
    console.dir(image.bitmap.data);
}
Stefan Rein
  • 8,084
  • 3
  • 37
  • 37