This repo demonstrates an issue affecting SvelteKit: sveltejs/kit#5952
When SvelteKit server-renders a page, it includes a <script>
tag that is effectively this:
<script type="module">
import { start } from '/node_modules/@sveltejs/kit/src/runtime/client/start.js';
// ...
</script>
This module, which is inside the @sveltejs/kit
package but not exposed via pkg.exports
, imports a module that is exposed via pkg.exports
:
import { error } from '../../index/index.js';
If user code imports the same module...
import { error } from '@sveltejs/kit';
...then ideally it would resolve to the same module. Instead, the latter import gets a ?v=${browserHash}
suffix:
/node_modules/@sveltejs/kit/src/runtime/index/index.js
/node_modules/@sveltejs/kit/src/runtime/index/index.js?v=xyz123
Because of that, the module is initialised twice, and instanceof
checks fail.
This repro demonstrates the same thing happening in a much simpler form. After installing dependencies, run npm run dev
and visit localhost:5173. The expectation is that the test
module will be instantiated once, but it's instantiated twice — once from index.html
...
<script type="module" src="/node_modules/test/index.js"></script>
...and once from src/index.js
:
import 'test';