kit
kit copied to clipboard
Cannot import $env/dynamic/private into client-side code should include context
Describe the problem
Importing $env/dynamic/private from src/hooks.ts throws an error about the import not supported in client side code, but it doesn't say that the error is coming from src/hooks.ts.
Here is the output from SvelteKit:
5:27:33 PM [vite] Internal server error: Cannot import $env/dynamic/private into client-side code
at LoadPluginContext.load (file:///Users/adam/work/eighty4/music-lesson-studio/web/node_modules/.pnpm/@[email protected]_@[email protected][email protected][email protected]_@typ_nktueinoag7c77ghffmdk4wvdm/node_modules/@sveltejs/kit/src/exports/vite/index.js:400:12)
at PluginContainer.load (file:///Users/adam/work/eighty4/music-lesson-studio/web/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/node/chunks/dep-mCdpKltl.js:48754:17)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async loadAndTransform (file:///Users/adam/work/eighty4/music-lesson-studio/web/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/node/chunks/dep-mCdpKltl.js:51548:22)
at async viteTransformMiddleware (file:///Users/adam/work/eighty4/music-lesson-studio/web/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/node/chunks/dep-mCdpKltl.js:61557:24) (x4)
Describe the proposed solution
Error message should specify the source file in userland code causing the problem.
5:27:33 PM [vite] Internal server error: Cannot import $env/dynamic/private into client-side code which is done by src/hooks.ts
at LoadPluginContext.load (file:///Users/adam/work/eighty4/music-lesson-studio/web/node_modules/.pnpm/@[email protected]_@[email protected][email protected][email protected]_@typ_nktueinoag7c77ghffmdk4wvdm/node_modules/@sveltejs/kit/src/exports/vite/index.js:400:12)
at PluginContainer.load (file:///Users/adam/work/eighty4/music-lesson-studio/web/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/node/chunks/dep-mCdpKltl.js:48754:17)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async loadAndTransform (file:///Users/adam/work/eighty4/music-lesson-studio/web/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/node/chunks/dep-mCdpKltl.js:51548:22)
at async viteTransformMiddleware (file:///Users/adam/work/eighty4/music-lesson-studio/web/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/node/chunks/dep-mCdpKltl.js:61557:24) (x4)
The import of $env/dynamic/private could be in a source file imported by a source imported by a source that's eventually referenced in src/hooks.ts. The knowledge of src/hooks.ts being the cause of the client-side code infringing dependency and the eventual source infringing would be even more helpful:
5:27:33 PM [vite] Internal server error: Cannot import $env/dynamic/private into client-side code which is done by src/lib/data.ts and imported by src/hooks.ts which is a client-side only extension point
at LoadPluginContext.load (file:///Users/adam/work/eighty4/music-lesson-studio/web/node_modules/.pnpm/@[email protected]_@[email protected][email protected][email protected]_@typ_nktueinoag7c77ghffmdk4wvdm/node_modules/@sveltejs/kit/src/exports/vite/index.js:400:12)
at PluginContainer.load (file:///Users/adam/work/eighty4/music-lesson-studio/web/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/node/chunks/dep-mCdpKltl.js:48754:17)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async loadAndTransform (file:///Users/adam/work/eighty4/music-lesson-studio/web/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/node/chunks/dep-mCdpKltl.js:51548:22)
at async viteTransformMiddleware (file:///Users/adam/work/eighty4/music-lesson-studio/web/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/node/chunks/dep-mCdpKltl.js:61557:24) (x4)
Alternatives considered
No response
Importance
nice to have
Additional Information
No response
To trace it, open the Context.load function from the stack trace in your editor...
const plugin_virtual_modules = {
name: 'vite-plugin-sveltekit-virtual-modules',
resolveId(id, importer) {
// If importing from a service-worker, only allow $service-worker & $env/static/public, but none of the other virtual modules.
// This check won't catch transitive imports, but it will warn when the import comes from a service-worker directly.
// Transitive imports will be caught during the build.
if (importer) {
const parsed_importer = path.parse(importer);
const importer_is_service_worker =
parsed_importer.dir === parsed_service_worker.dir &&
parsed_importer.name === parsed_service_worker.name;
if (importer_is_service_worker && id !== '$service-worker' && id !== '$env/static/public') {
throw new Error(
`Cannot import ${id} into service-worker code. Only the modules $service-worker and $env/static/public are available in service workers.`
);
}
}
// treat $env/static/[public|private] as virtual
if (id.startsWith('$env/') || id.startsWith('__sveltekit/') || id === '$service-worker') {
+ console.log({importer})
return `\0virtual:${id}`;
}
},
Reproduction. Apparently, we already have report the files in another part of the codebase but it's not used in this instance. I'm not too sure yet how we'd replicate this functionality. https://github.com/sveltejs/kit/blob/1616669387388aaef02159ad7dc316c605bfdaec/packages/kit/src/exports/vite/graph_analysis/index.js#L61-L65
In comparison, here's the LOC for the current error: https://github.com/sveltejs/kit/blob/1616669387388aaef02159ad7dc316c605bfdaec/packages/kit/src/exports/vite/index.js#L401