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.

Dynamic imports don't work when `exports` specified in package.json

See original GitHub issue

Bug report

What is the current behavior?

For example, highlight.js has the following in exports of package.json:

{
	"exports": {
		"./lib/languages/*": {
			"require": "./lib/languages/*.js",
			"import": "./es/languages/*.js"
		}
	}
}

I tried to import a language dynamically using the following code in a Vue 3 / TS 4 app:

const langName = ref('')
const message = ref('')
const module = await import(
  `highlight.js/lib/languages/${langName.value}`
)
message.value = module.default.name

This leads to the following error:

 error  in ./src/App.vue?vue&type=script&lang=ts

Module not found: Error: Package path ./lib/languages is not exported from package <path_to_proj>/node_modules/highlight.js (see exports field in <path_to_proj>/node_modules/highlight.js/package.json)

If the current behavior is a bug, please provide the steps to reproduce.

  1. Clone minimum reproduction repo and npm install.
  2. Run the project with npm run serve.
  3. See error.

What is the expected behavior?

Replacing ${langName.value} with a literal value works and imports the file as expected:

const langName = ref('')
const message = ref('')
const module = await import(
  `highlight.js/lib/languages/javascript`
)
message.value = module.default.name

Dynamic import should work equally well.

Workarounds

The comment https://github.com/highlightjs/highlight.js/issues/3223#issuecomment-886143417 contains possible workarounds, if that helps to narrow the problem.

Other relevant information: webpack version: 5 Node.js version: 14 Operating System: mac OS Additional tools: TypeScript 4, Vue 3, Babel 7

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:17
  • Comments:17 (5 by maintainers)

github_iconTop GitHub Comments

13reactions
joshgoebelcommented, Dec 15, 2021

So how are people dealing with this in real life other than begging package maintainers to stop using imports?

It would be nice to have a workaround to suggest to our users, particular if this is not going to be fixed anytime soon.

3reactions
andrecutmcommented, Jan 19, 2022

So how are people dealing with this in real life other than begging package maintainers to stop using imports?

It would be nice to have a workaround to suggest to our users, particular if this is not going to be fixed anytime soon.

To overcome this in an Angular app, I’ve used a relative path to the loaded module, for example:

import(`@angular/common/locales/${this.userLanguageCode}.js`)

became

import(`../../../../node_modules/@angular/common/locales/${this.userLanguageCode}.mjs`)

(ignore path differences, it should not matter)

App builds and successfully loads the languages again

Read more comments on GitHub >

github_iconTop Results From Across the Web

Package.json exports with webpack 5 - dynamically imported ...
Since everything is dynamic, I am using the CopyWebpackPlugin to include the locales in the dist folder. This works as expected locally, but ......
Read more >
Dynamic imports - The Modern JavaScript Tutorial
The import(module) expression loads the module and returns a promise that resolves into a module object that contains all its exports. It can...
Read more >
How to Bypass ES Modules Errors in Next.js with Dynamic ...
What are dynamic imports? Dynamic imports is a feature of Next. js that allows you to work with JavaScript modules conveniently in the...
Read more >
import - JavaScript - MDN Web Docs - Mozilla
There is also a function-like dynamic import() , which does not require ... /modules/my-module.js const a = 1; export { a as "a-b"...
Read more >
API - ESBuild
By default esbuild will not bundle the input files. ... Bundling with esbuild only works with statically-defined imports (i.e. when the import path...
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