maplibre-gl bundling
I’m getting an error trying to import a page that calls
import { LngLat } from 'maplibre-gl'
— error says:
I am developing a nuxt module, so the issue may be how things are being bundled. I’m not sure how to troubleshoot further.
The module/repo is here https://dm.nuxt.space/nuxt-maplibre/ — the issue seems to be when installing the module in a new nuxt project. If I just run dev from my repo it works fine.Nuxt MapLibre
Documentation for the Nuxt MapLibre module
2 Replies
Hi @dmarr. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
The error message:
suggests that the way
This kind of error often happens when a package is published in CommonJS format, but you are trying to import it as an ES module, or vice versa. Nuxt 3 uses ESM by default, and if
If the library is not being properly transpiled, you can add it to the
See: Troubleshooting ESM Issues 3. Check the Export in maplibre-gl
Double-check that
If the above does not work, you may need to alias the import to a specific file (such as the CJS build) in your
See: ES Modules Library Author Guide ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
maplibre-gl
is being imported or bundled in your Nuxt module is causing issues with ESM/CJS interop or export resolution.
Troubleshooting Steps
1. Check ESM/CJS CompatibilityThis kind of error often happens when a package is published in CommonJS format, but you are trying to import it as an ES module, or vice versa. Nuxt 3 uses ESM by default, and if
maplibre-gl
is not fully ESM-compatible, you may need to adjust your configuration.
2. Transpile the DependencyIf the library is not being properly transpiled, you can add it to the
build.transpile
array in your consuming Nuxt project's nuxt.config
:
This can help Nuxt handle the module format correctly and resolve exports as expected. This approach is recommended for libraries that are not fully ESM-compatible or that import assets (like CSS or images) that need to be processed by the build toolchain.See: Troubleshooting ESM Issues 3. Check the Export in maplibre-gl
Double-check that
LngLat
is actually exported as a named export in the version of maplibre-gl
you are using. Sometimes, libraries only provide a default export or use a different export structure in CJS vs ESM builds. If it's only available as a property of the default export, you may need to import it like this:
Or, for dynamic import:
See: ES Modules Library Author Guide
4. Aliasing (if needed)If the above does not work, you may need to alias the import to a specific file (such as the CJS build) in your
nuxt.config
:
See: Troubleshooting ESM Issues
Why It Works in Dev But Not When Installed
When running dev
in your module repo, you may be using a different build context or dependency resolution than when your module is installed in a fresh Nuxt project. This can mask issues with how dependencies are bundled or resolved.
Summary
- Add maplibre-gl
to build.transpile
in the consuming Nuxt project.
- Double-check the export style of LngLat
in maplibre-gl
.
- Consider using a default import if named import fails.
- Use aliasing if necessary.
If these steps do not resolve the issue, the knowledge sources do not provide further details specific to maplibre-gl
and Nuxt module bundling, so you may need to investigate the export structure of maplibre-gl
or consult its documentation for ESM compatibility.
See: Troubleshooting ESM IssuesSee: ES Modules Library Author Guide ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
<@139883350096740354> Kapa.ai is still learning and improving, please let me know how I did by reacting below.