question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for ESM config files (with "type": "module")

See original GitHub issue

If a project’s package.json has "type": "module", Karma fails to load the config file. I’m wondering if there would be interest in supporting ESM config files in addition to CJS (and the other currently supported flavors).

While this might not be complete or the desired solution, it looks like something like this could add ESM config support: https://github.com/tschaub/karma/commit/99f1a92ec85a1dd2efbcd0faf1ad653e4b29cf8e

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:10
  • Comments:18 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
npetruzzellicommented, Dec 17, 2021

It appears that, even though it’s not in the documentation

It is documented in the public API page: http://karma-runner.github.io/6.3/dev/public-api.html#karmaconfig

Though it may be worth adding a link to it from the config file page, in the Overview section, just after the line that describes the export:

Within the configuration file, the configuration code is put together by setting module.exports to point to a function which accepts one argument: the configuration object.

1reaction
thw0rtedcommented, Dec 14, 2021

It appears that, even though it’s not in the documentation, Karma can already take a config function whose return value is a Promise. That allows you to simply write

// karma.conf.js
module.exports = function(config) {
    return import("./karma.conf.mjs").then(val => config.set(val));
}

// karma.conf.mjs
import fooPlugin from "foo-plugin";

export default function(config) {
  config.set({
    ...,
    plugins: [fooPlugin]
  });
}

I needed this when I updated to Angular 13; why I needed it is kind of an epic tale. I’m using Karma with karma-webpack; and my webpack configuration uses the AngularWebpackPlugin. Previously I imported my webpack config file, which was written in ES module format, via the esm package. As of Angular 13, manual webpack configs now require that you pass their shipped code through Babel (!), configured with a linker plugin. The linker plugin (@angular/compiler-cli/linker/babel) is exposed via the exports field in package.json, which means that you cannot use the esm package, so my old setup is no longer possible.

I figured out the solution above, to use Node’s own MJS support to dynamically import the Karma config file, which needs to be able to somehow import the Webpack config file. By writing my actual Karma config in MJS, I can simply write import webpackConfig from "./webpack.config.mjs"; and Node handles the heavy lifting, no Karma patches required. (The above works for me in Node 14, Karma 6.3.9.)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - ECMAScript Modules in Node.js - TypeScript
Node.js supports a new setting in package.json called type . "type" can be set to either "module" or "commonjs" . ... This setting...
Read more >
Support ESM in next.config.js - Stack Overflow
1 Answer 1 · 3. having next.config.mjs and type: 'module' in package.json results in error: require() of ES Module /usr/app/.next/server/pages/_ ...
Read more >
How we employed the new ECMAScript Module Support in ...
What is the new standard to serve both an ECMAScript Module (ESM) as well as Commonjs ... How to exempt files from the...
Read more >
ESM Support | ts-jest - GitHub Pages
To use ts-jest with ESM support: ... your Jest config to avoid issues that Jest doesn't transform files correctly. ... module.exports = {...
Read more >
Configuring Vite
Note Vite supports using ES modules syntax in the config file even if the project is not using native Node ESM, e.g. type:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found