Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage in the browser with importmap #507

Open
JH1ller opened this issue Jun 13, 2024 · 1 comment
Open

Usage in the browser with importmap #507

JH1ller opened this issue Jun 13, 2024 · 1 comment

Comments

@JH1ller
Copy link

JH1ller commented Jun 13, 2024

Hey there,

We're using qs in a ts library by importing it like this:

import { type IParseBaseOptions, parse, stringify } from 'qs';

We export our library as an esm bundle which is used in multiple contexts; installed as npm module (and then run in both server/node and client side) as well as directly within a script tag by utilizing import maps.
This looks roughly like this:

<script type="importmap">
  {
    "imports": {
      "axios": "https://unpkg.com/[email protected]/dist/esm/axios.js",
      "qs": "https://unpkg.com/[email protected]/lib/index.js"
    }
  }
</script>
<script type="module">
  import { someFunction } from "./our-library/dist/index.js";
</script>

However, with this setup I get this error in the browser:

Uncaught SyntaxError: The requested module 'qs' does not provide an export named 'parse'

That's probably because qs only provides a CommonJS bundle and not an ESM bundle.
From older issues it looks like you're quite apposed to ESM and will not add this option, but is there any other way I could use this library with an importmap?

My first approach was switching to https://www.npmjs.com/package/query-string which exports an esm bundle, but that library by design doesn't support nested objects.

@ljharb
Copy link
Owner

ljharb commented Jun 13, 2024

What transpiler and/or bundler are you using? import { parse } from 'qs' should work with anything that's configured properly. (for example, typescript's module system is utterly broken unless you've enabled synthetic imports and esModuleInterop)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants