1

I found a great github project and I was hoping to use it in my project. I'm sure I'm going to run into this problem again. So I'm really hoping I can get help on this. The project is located in the URL: https://github.com/lokesh/color-thief. I am trying to import the js file color-thief.js into my project and use it in a TS file or component. I added the file to my scripts[] in the angular.json file. Yet I'm still running into problems accessing the method getColor(). I am using intelliJ. I can't get the intellisense or code-completion to work. Which is furthermore making me think I'm not importing the file correctly.

Here is my ts file.

declare var ColorThief: any;

The path to get to the js file is

'../../js/color-thief.js'

Environmental setup is a nightmare.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Nov
  • 99
  • 1
  • 11

1 Answers1

1

Make sure that in your angular.json file, the script property set something like this.

"scripts": [
  "node_modules/[packageName]/js/color-thief.js",
],

remember to set node_modules and in the script, and import the proper .js file when that global variable is defined.

As far as i can see you need to create that object, according to color-thief package. Like this.

 let colorThief = new ColorThief();
Yoarthur
  • 909
  • 9
  • 20
  • So originally I just installed the file "color-thief.js". This time I npm installed the git project. And used your suggestion. Now when typing console.log(ColorThief) I get f(){}. So it seems it is sort of working? But, now I'm receiving the error that getColor is undefined. – Nov Dec 18 '18 at 16:39
  • Now you're properly importing. I've edited the answer. It seems you need to create ColorThief object – Yoarthur Dec 18 '18 at 16:49
  • idk... now I'm getting this error: ERROR TypeError: _js_color_thief_js__WEBPACK_IMPORTED_MODULE_8___default.a is not a constructor – Nov Dec 18 '18 at 16:57
  • Honestly, I'm sure there's a solution. But, I switched to a different git project that has a better readme and seems to have a bigger following. https://stackoverflow.com/questions/50909276/angular-5-get-colors-from-an-image node-vibrant – Nov Dec 18 '18 at 17:17
  • let me test it. – Yoarthur Dec 18 '18 at 17:21