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

Error when importing npm package: 'x' is not exported #848

Open
iluuu1994 opened this issue Sep 16, 2021 · 2 comments
Open

Error when importing npm package: 'x' is not exported #848

iluuu1994 opened this issue Sep 16, 2021 · 2 comments

Comments

@iluuu1994
Copy link

This repl is awesome! ❤️ I have a small issue.

https://svelte.dev/repl/820b649c9edd4acd9e9b33d727dd3414?version=3.42.6

<script>
	import {createForm} from 'svelte-forms-lib';
</script>

'dequal' is not exported by https://unpkg.com/[email protected]/lite/index.js, imported by https://unpkg.com/[email protected]/lib/util.js

Looking at the contents of https://unpkg.com/[email protected]/lite/index.js dequal is exported though on the last line.

exports.dequal = dequal;

Any idea what's going on here?

@Conduitry
Copy link
Member

As far as I can tell, there are two places where this specific issue could be attacked. I think they would both be reasonable new features for the REPL, but I don't know which is more likely to be more helpful or easier.

One the one hand, the in-browser bundler code for CommonJS->ESM conversion doesn't support exposing keys on the exports object as named exports - https://github.com/sveltejs/svelte-repl/blob/master/src/workers/bundler/plugins/commonjs.js

On the other hand, unpkg is resolving (redirecting) https://unpkg.com/dequal/lite to https://unpkg.com/[email protected]/lite/index.js (the CJS version) as opposed to https://unpkg.com/[email protected]/lite/index.mjs (the ESM version). I don't know whether it's using the module's exports map (https://unpkg.com/[email protected]/package.json) or not. But the REPL could conceivably handle this on our end, resolving things to the import (ESM) version, which would remove the need for more robust CJS->ESM conversion.

@Rich-Harris Rich-Harris transferred this issue from sveltejs/svelte-repl Dec 16, 2021
@theetrain
Copy link

theetrain commented Dec 21, 2022

Would it be possible to leverage Skypack's dist parameter? I noticed some packages that mix CJS modules and ESM will run into problems when served by Unpkg in the REPL.

Example: https://svelte.dev/repl/00b452b61a424de9b818e79bf029f37f?version=3.55.0
Yields the error:

'enableCache' is not exported by https://unpkg.com/@iconify/[email protected]/dist/functions.js, 
imported by https://unpkg.com/@iconify/[email protected]/dist/Icon.svelte

The error's source: https://unpkg.com/@iconify/[email protected]/dist/functions.js

At the bottom of that file, I can see that exports.enableCache = enableCache; uses CJS modules. The Icon.svelte file importing this function uses ESM syntax, which is why the module cannot be resolved.

Skypack solves this by converting all files in a module to use ES2020. Here's the same module in Skypack: https://cdn.skypack.dev/@iconify/svelte@latest/dist=es2020
Which eventually points to the same enableCache function, but using ESM https://cdn.skypack.dev/-/@iconify/[email protected]/dist=es2019,mode=imports/optimized/@iconify/svelte.js

It's currently not possible to do this in the Svelte REPL:

<script>
	import Icon from "https://cdn.skypack.dev/@iconify/svelte@latest/dist=es2020";
</script>

But if I could, this would help with CJS-to-ESM conversions. Or, the REPL can switch from Unpkg to Skypack.

@benmccann benmccann transferred this issue from sveltejs/sites Nov 12, 2024
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

3 participants