Skip to content

Failure to import components using webpack #98

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

Open
finestgecko opened this issue May 30, 2022 · 1 comment
Open

Failure to import components using webpack #98

finestgecko opened this issue May 30, 2022 · 1 comment

Comments

@finestgecko
Copy link

finestgecko commented May 30, 2022

Describe the bug

I'm using Webpack 5 (via @symfony/webpack-encore) and getting a bunch of errors when I import the components. It seems to be a common problem.

  1. I installed the package with npm install -D @rgossiaux/svelte-headlessui
  2. I followed the Dialog example from the docs, importing the package like this:
import {
    Dialog,
    DialogOverlay,
    DialogTitle,
    DialogDescription,
} from '@rgossiaux/svelte-headlessui'

Which results in lots of errors like this:

export 'DialogOverlay' (imported as 'DialogOverlay') was not found in '@rgossiaux/svelte-headlessui' (module has no exports)
export 'DialogDescription' (imported as 'DialogDescription') was not found in '@rgossiaux/svelte-headlessui' (module has no exports)
export 'Dialog' (imported as 'Dialog') was not found in '@rgossiaux/svelte-headlessui' (module has no exports)

and lots like this:

[ encore ]  ERROR  Failed to compile with 10 errors23:40:34
[ encore ] Module build failed: Module not found:
"./node_modules/@rgossiaux/svelte-headlessui/index.js" contains a reference to the file "./components/dialog".
This file can not be found, please check it for typos or update it if the file got moved.

"./node_modules/@rgossiaux/svelte-headlessui/index.js" contains a reference to the file "./components/disclosure".
[ encore ] This file can not be found, please check it for typos or update it if the file got moved.

and, crucially, this:

Did you mean 'index.js'?
BREAKING CHANGE: The request './components/transitions' failed to resolve only because it was resolved as fully specified
(probably because the origin is strict EcmaScript Module, e. g. a module with javascript mimetype, a '*.mjs' file, or a '*.js' file where the package.json contains '"type": "module"').
The extension in the request is mandatory for it to be fully specified.
Add the extension to the request.

To reproduce

Node.js version:

joel@xenon:~$ node -v
v16.15.0

svelte-headlessui version:

joel@xenon:~/app/server$ cat package.json | grep svelte-headlessui
    "@rgossiaux/svelte-headlessui": "^1.0.2",

webpack-encore version:

joel@xenon:~/app/server$ cat package.json | grep webpack
    "@symfony/webpack-encore": "^2.1.0",

(which internally uses Webpack ^5.7.2)

The fix

One of the errors suggested this:

The extension in the request is mandatory for it to be fully specified.
Add the extension to the request.

Adding .js to the imports seems to solve the problem.


Update: For anyone else facing this problem, a better solution might be to add fullySpecified: false to webpack.config.js.

{
  test: /\.m?js/,
  resolve: {
    fullySpecified: false,
  },
}

(props to this comment for the solution)

@rgossiaux
Copy link
Owner

Thanks for the report. I found this SvelteKit issue which has a lot of useful context as well: sveltejs/cli#205

Since there's a workaround & most Svelte projects don't use webpack, I'm not going to prioritize this just yet. Hopefully in the meantime the guidance for library authors will become a little clearer.

@rgossiaux rgossiaux changed the title The request [...] failed to resolve only because it was resolved as fully specified Failure to import components using webpack May 31, 2022
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