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.

Module imports without extension fail loading

See original GitHub issue

I’ve configured regarding the documentation as follows

karma.config.js

    files: [
      { pattern: 'test/**/*.js', type: 'module' },
      { pattern: 'src/**/*.js', type: 'module', included: false }
    ],

Then when using a working mocha test with imports without .js extensions, like

import TestModule from '../../src/TestModule';

…the execution with karma fails with:

...
26 05 2021 15:09:57.267:WARN [web-server]: 404: /base/src/TestModule
Chrome Headless 90.0.4430.212 (Mac OS 10.15.7) ERROR
  Uncaught Error loading test/Test.js
  at http://localhost:9876/context.html:286:271

Importing ‘…/…/src/TestModule.js’ does work, but is not a solution as the TestModule.js itself imports without extensions. The implementation of TestModule.js shall not be changed.

Is this a bug in Karma runner or did a missed an important configuration here? Help appreciated

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
devoto13commented, May 26, 2021

Seems to work as intended: https://stackoverflow.com/a/55289496/1377864

In a browser with support for ES Modules (ESM), no extensions will be added to the URL that you import - if your file for example has .js extension, you have to write import {x} from “./file.js”.

Unless you told it to do so, Karma will not do any file processing, so it will just load the file “as is” in the browser.

0reactions
devoto13commented, May 26, 2021

No, there is no way to do it without plugins. Vanilla Karma just loads your JS code in the browser without any extra processing and therefore JS should be in the format which the browser understands.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How does JavaScript import find the module without an ...
In Node.js versions which support ESM, the runtime will not search for extensions, but it will resolve modules from node_modules by name.
Read more >
CommonJS modules | Node.js v19.3.0 Documentation
Attempting to do so will throw a ERR_REQUIRE_ESM error. Use import() instead. The .mjs extension is reserved for ECMAScript Modules which cannot be...
Read more >
Dynamic module import doesn't work in webextension content ...
Hi, maybe I'm doing something wrong, but I'm not able to import a module in a webextension content script via the dynamic import....
Read more >
Module Methods - webpack
The ES2015 Loader spec defines import() as method to load ES2015 modules dynamically on ... It is not possible to use a fully...
Read more >
Documentation - Module Resolution - TypeScript
We'll cover non-relative imports next. Finally, if the compiler could not resolve the module, it will log an error. In this case, the...
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