svelte: Errors after context="module" to module change in Svelte 5
Describe the bug
After updating from 230 to 240, which includes the context="module" to module change, and updating my script tags to just use module, I am getting errors when running npm run dev. The actual code does what I expect though.
Error: Failed to scan for dependencies from entries:
/Users/minds/repos/test2/src/routes/+page.svelte
✘ [ERROR] No matching export in "html:/Users/minds/repos/test2/src/lib/ComponentB.svelte" for import "someExport"
script:/Users/minds/repos/test2/src/lib/ComponentA.svelte?id=0:2:10:
2 │ import { someExport } from '$lib/ComponentB.svelte';
╵ ~~~~~~~~~~
at failureErrorWithLog (/Users/minds/repos/test2/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:1472:15)
at /Users/minds/repos/test2/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:945:25
at runOnEndCallbacks (/Users/minds/repos/test2/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:1315:45)
at buildResponseToResult (/Users/minds/repos/test2/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:943:7)
at /Users/minds/repos/test2/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:955:9
at new Promise (<anonymous>)
at requestCallbacks.on-end (/Users/minds/repos/test2/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:954:54)
at handleRequest (/Users/minds/repos/test2/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:647:17)
at handleIncomingPacket (/Users/minds/repos/test2/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:672:7)
at Socket.readFromStdout (/Users/minds/repos/test2/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:600:7)
Reproduction
Create a new skeleton application using pnpm create svelte@latest and add the following three files:
src/lib/routes/+page.svelte
<script>
import ComponentA from '$/lib/ComponentA.svelte';
</script>
<ComponentA />
src/lib/ComponentA.svelte
<script>
import { someExport } from '$lib/ComponentB.svelte';
function onClick() {
console.log(someExport.foo);
}
</script>
<button onclick={onClick}>Click me</button>
src/lib/ComponentB.svelte
<script module>
export const someExport = { foo: 'bar' };
</script>
Then run pnpm run dev. You will see the beforementioned errors. If I switch back to using context="module", it works again.
Logs
No response
System Info
System:
OS: macOS 14.6.1
CPU: (8) arm64 Apple M1 Pro
Memory: 72.22 MB / 32.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 18.19.1 - ~/.nvm/versions/node/v18.19.1/bin/node
Yarn: 1.22.22 - ~/.nvm/versions/node/v18.19.1/bin/yarn
npm: 10.2.4 - ~/.nvm/versions/node/v18.19.1/bin/npm
pnpm: 9.9.0 - /opt/homebrew/bin/pnpm
bun: 1.1.26 - /opt/homebrew/bin/bun
Browsers:
Chrome: 127.0.6533.122
Safari: 17.6
npmPackages:
svelte: ^5.0.0-next.240 => 5.0.0-next.240
Severity
annoyance
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 2
- Comments: 22 (13 by maintainers)
Commits related to this issue
- fix: detect `<script module>` tag (#2482) This was missing in #2473 Possibly related to sveltejs/svelte#13039 — committed to sveltejs/language-tools by dummdidumm 2 years ago
Is there still someone looking into this? I have been able to reproduce this with the provided repo on many different laptops, from clean states even. Maybe it’s related to the fact that all those laptops are macbooks?
I also have this problem, changing
context="module"tomodulemakes Svelte not find the exported functions anymore.Same error here.
Also, another strange issue: the LSP complains about valid js syntax.
Reverting to
context=modulefixes this oddity.@dominikg likely found the issue and it’s upstream in vite
https://github.com/vitejs/vite/blob/952bae3efcbd871fc3df5b1947060de7ebdafa36/packages/vite/src/node/optimizer/scan.ts#L287
They are using a regex to scan for dependencies…will PR later to vite to fix this
I’ll try to take a look later
This is not an issue in the Vs code extension. The language server in vscode is actually working great with these changes and can autocomplete and jump to the variables without any problems.
The errors happen when you run ‘pnpm run dev’
https://github.com/sublimelsp/LSP-svelte v3.1.4