I had a similar problem with the npm packages 'rgb-hex' and 'hex-rgb' because their main export is done in ES6 format. I was using a Typescript project with Jest. I think the error comes from the Jest end and not Typescript, as Jest works on properly transpiled javascript (according to its own config). The underlying problem is these ES6 style main exports will cause problems with Jest (not tsc or Typescript chain) because you need to tell Jest to transpile.
I found a usable config using the package 'ts-jest' with the following configs, based on simple presets (the only thing to note is the ignore patterns):
tsconfig.json:
{
"compilerOptions": {
"esModuleInterop": true,
"module": "es2020",
"moduleResolution": "nodenext",
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"rootDir": "./",
"allowJs": true
},
"ts-node": {
"esm": true
},
"include": ["./ts-src/**/*"]
}
jest.config.cjs
module.exports = {
verbose: true,
preset: 'ts-jest/presets/js-with-babel-esm',
testEnvironment: 'node',
transformIgnorePatterns: ['/node_modules/(?!(hex\-rgb|rgb\-hex)/)']
};
NOTE: Above, the preset setting is important you want the babel with esm, according to docs of 'ts-jest'.
babel.config.cjs (totally vanilla but included and I think necessary)
module.exports = {
presets: [['@babel/preset-env', {targets: {node: 'current'}}]],
};
Note the ignore patterns for the stuff from node_modules. You're using the negation in regex with a grouping to tell it "yes ignore node_modules for transpiling, aside from these specific package patterns". The reason this stuff comes up with Jest is packages in node_modules usually don't use ES6 exports from their main file, index.js file (or similar). And Jest doesn't usually transform them, because then it would have to scan all of node_modules. So you use those ignore patterns (with somewhat counter-intuitive regex) to transpile specific things in node_modules.
EDIT: the .cjs extensions are because my overall project was using "type": "module"
in the package.json. If you leave these files as .js you'll get a very descriptive error. The .cjs extension tells Node to relax and just use the configs in the current canonical format their frameworks use (which is not ES6... lol JS never change)