chroma.js
chroma.js copied to clipboard
Import as ES6 module
Hello,
When I import Chroma as a module:
import * as chroma from "chroma-js";
let minRandomColor = chroma.random();
let maxRandomColor = chroma.random();
let colors = chroma.scale([minRandomColor, maxRandomColor]).padding(0.05).colors(10);
let rgbColor = chroma(colors[i]).rgb();
...I get the following error regarding the last line:
Uncaught TypeError: chroma is not a function
Any idea how I should import or call chroma?
Have you tried just printing out what's in chroma?
- Let
1.mjsbeimport * as chroma from "chroma-js"; console.log(chroma) - Run
node --experimental-modules --es-module-specifier-resolution=node 1.mjs.
You will see that everything you want is actually in default. So instead of import * as chroma you just import chroma:
import chroma from "chroma-js"
let minRandomColor = chroma.random();
let maxRandomColor = chroma.random();
let colors = chroma.scale([minRandomColor, maxRandomColor]).padding(0.05).colors(10);
let rgbColor = chroma(colors[1]).rgb();
console.log(rgbColor)
I'm having trouble with this also, trying to use chroma in a Svelte component (with vite):
import chroma from 'chroma-js';
let f = chroma.scale() //VS code tells me .scale is not a function
See below, this is all I get on chroma. However chroma('#D4F880').darken().hex() does work.

Confirmed this is a problem related to svelte/vite. The import works with no problem in a simple node app.