Support for ESM config files (with "type": "module")
See original GitHub issueIf 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:
- Created 2 years ago
- Reactions:10
- Comments:18 (8 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
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
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 theesm
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 theexports
field inpackage.json
, which means that you cannot use theesm
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.)