kit icon indicating copy to clipboard operation
kit copied to clipboard

Vitest fails after SvelteKit Update: Cannot find package '__sveltekit'

Open vekunz opened this issue 2 years ago • 14 comments

Describe the bug

If I try to update our project from SvelteKit 1.3.10 to the latest version 1.8.3, our Vitest unit tests are failing with a kind of strange error.

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Suites 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
 FAIL  src/routes/page.test.ts [ src/routes/page.test.ts ]
Error: Cannot find package '__sveltekit' imported from C:\path-to-repo\node_modules\@sveltejs\kit\src\runtime\client\utils.js
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
Serialized Error: {
  "code": "ERR_MODULE_NOT_FOUND",
}
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/1]⎯

Reproduction

I created a minimal reproduction repo based on a create-svelte project. https://github.com/vekunz/sveltekit-vitest-error

Logs

No response

System Info

System:                                                 
    OS: Windows 10 10.0.19045                             
    CPU: (12) x64 Intel(R) Core(TM) i7-9850H CPU @ 2.60GHz
    Memory: 9.34 GB / 31.79 GB                            
  Binaries:
    Node: 16.19.0 - C:\Program Files\nodejs\node.EXE
    npm: 8.13.2 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Chrome: 109.0.5414.122
    Edge: Spartan (44.19041.1266.0), Chromium (110.0.1587.50)
    Internet Explorer: 11.0.19041.1566
  npmPackages:
    @sveltejs/adapter-auto: 2.0.0 => 2.0.0
    @sveltejs/kit: 1.8.3 => 1.8.3
    svelte: 3.55.1 => 3.55.1
    vite: 4.1.4 => 4.1.4

Severity

blocking an upgrade

Additional Information

No response

vekunz avatar Feb 22 '23 10:02 vekunz

Hi there, I've got the same problem and also created a repository to prepare an issue: https://github.com/nilsroehrig/sveltekit-import-problem.

In my case it happens when the code under test imports goto from $app/navigation. Doesn't really matter wether it's used or not. Simply importing already breaks the according tests. The issue appears when switching from @sveltejs/kit<=1.3.10 to >=1.4.0 in the following form:

Error: Cannot find package '$internal' imported from /path/to/repository/node_modules/@sveltejs/kit/src/runtime/client/utils.js

Serialized Error: {
  "code": "ERR_MODULE_NOT_FOUND",
}

Upgrading to >=1.6.0 the issue presents itself like in vekunz' description.

Steps to reproduce

  1. Clone the repo mentioned above
  2. npm install
  3. npm run test:unit
  4. Watch tests being green
  5. Set @sveltejs/kit to version 1.4.0
  6. Repeat steps 2 and 3
  7. Watch tests being red with error message mentioned above
  8. Set @sveltejs/kit to version >=1.6.0 or higher
  9. Repeat steps 2 and 3
  10. Watch tests being red with error message described by vekunz

nilsroehrig avatar Feb 22 '23 14:02 nilsroehrig

@benmccann I believe this is related to one of the recent changes (#9022), which I thought would increase interop with things like Vitest?

dummdidumm avatar Feb 22 '23 14:02 dummdidumm

Error: Cannot find package '$internal' imported from /path/to/repository/node_modules/@sveltejs/kit/src/runtime/client/utils.js

Serialized Error: {
  "code": "ERR_MODULE_NOT_FOUND",
}

Upgrading to >=1.6.0 the issue presents itself like in vekunz' description.

This $internal error was the reason we waited this long with an update. This was fixed in SvelteKit with version 1.5.7. But now this other error (__sveltekit) is present.

vekunz avatar Feb 22 '23 15:02 vekunz

It turns out that Vitest doesn't use Vite itself, but vite-node which doesn't call resolveId on externalized packages. Vitest doesn't process imports inside node_modules packages by default. As a workaround, you can inline the package with deps.inline or ssr.noExternal (though I expect that may cause other problems) or enable deps.registerNodeLoader to process all imports (which is apparently much slower)

benmccann avatar Feb 22 '23 20:02 benmccann

Ideally Vitest would fix vite-node, but we probably can't count on that happening anytime soon.

Are you sure? Would it be lots of work? Cause from my experience, vitest devs are very responsive.

janosh avatar Feb 25 '23 15:02 janosh

deps.inline or ssr.noExternal don't seem to help (if I'm putting them in the right place). Adding


  test: {
    environment: `jsdom`,
    css: true,
    coverage: {
      reporter: [`text`, `json-summary`],
    },
+    deps: {
+      inline: [`@sveltejs/kit`],
+    },
  },

raises

ReferenceError: __sveltekit_1fr7sov is not defined
 ❯ __sveltekit/paths:3:16
 ❯ node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@sveltejs/kit/src/runtime/client/utils.js:5:31
      3| import { assets } from '__sveltekit/paths';
      4| import { version } from '__sveltekit/environment';
      5| import { PRELOAD_PRIORITIES } from './constants.js';
       |                               ^
      6| 
      7| /* global __SVELTEKIT_APP_VERSION_FILE__, __SVELTEKIT_APP_VERSION_POLL_INTE…
 ❯ node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@sveltejs/kit/src/runtime/client/singletons.js:3:31

janosh avatar Feb 25 '23 15:02 janosh

deps.inline or ssr.noExternal don't seem to help (if I'm putting them in the right place). Adding deps.inline raises ReferenceError: __sveltekit_1fr7sov is not defined

It does help, but it only fixes one of two bugs that are present. The original bug report here had:

Cannot find package '__sveltekit' imported from

The issue there is that vitest doesn't resolve the __sveltekit virtual modules unless @sveltejs/kit is in ssr.noExternal.

When you set it you get the slightly different error message:

ReferenceError: __sveltekit_1fr7sov is not defined

That's because you're now encountering the second issue. https://github.com/sveltejs/kit/pull/8957 introduced a global variable which is undefined when you use a component alone outside of an application

I don't know if there's a workaround on the user side right now, but we'll get it fixed this week

benmccann avatar Feb 26 '23 03:02 benmccann

I have a svelte-kit project that uses a mono repo structure with lerna and yarn workspaces. Using svelte-kit version 1.5 works wonderfully, but 1.8.5 and 1.8.6 both spit out the: ReferenceError: __sveltekit_1fr7sov is not defined after running a prod build and preview. Is this relevant to this issue even though I'm not not using vitest in anyway?

astev89 avatar Feb 27 '23 20:02 astev89

I am still facing this issue after moving to version 1.9.1 from 1.7.2

Uncaught (in promise) ReferenceError: __sveltekit_1wwp0ph is not defined at singletons.b9817564.js:1:95

laxadev avatar Feb 28 '23 22:02 laxadev

The virtual module uses optional chaining now:

export const base = __sveltekit_pdvjxu?.base ?? "";
export const assets = __sveltekit_pdvjxu?.assets ?? base;

benmccann avatar Feb 28 '23 23:02 benmccann

Thanks y'all! Problem solved. 😄

janosh avatar Mar 01 '23 14:03 janosh

Got the issue again after upgrade to vitest >= v4.0.0. I reverted to vitest v3.2.4 and it worked fine

kushuh avatar Oct 26 '25 17:10 kushuh

Has anyone found a fix for this? It's blocking the upgrade to Vitest 4.0 for us.

Stadly avatar Nov 02 '25 19:11 Stadly

The issue we faced was solved by first updating to Vite 7. Not sure if that solves it for everyone? If so, this can be closed :)

Stadly avatar Nov 30 '25 18:11 Stadly