Open flayks opened 2 years ago
Tracking what functions call what - or what functions use what data - is a whole lot more work than just tracing the import
module graph, with very little worthwhile gain. I'm marking this as a documentation issue, but if there's not really a good way to convey this in the documentation without confusing more people than it helps, we shouldn't force it.
Perhaps the error could explain something to this effect? Something like:
"Ensure that no other exports in "path/to/file.ts" are imported into client code."
Just in case someone gets the same issue as me…. There’s something odd in the auto import functionality in VSCode. Not sure how I triggered it but after receiving the same error and scratching my head for a while I found a new import at the top of my +page.svelte like this:
import { _ } from ‘$env/static/private’;
Like I said, no idea how I triggered it but I’ve had similar problems with VSCode, or some add-in creating unwanted imports over the last few months.
Writing my observations here if anyone else is baffled by this issue. Naming matters, so don't name your function "serverFoo" in src/lib/serverFoo.ts if it's used also in client code.
Just in case someone gets the same issue as me…. There’s something odd in the auto import functionality in VSCode. Not sure how I triggered it but after receiving the same error and scratching my head for a while I found a new import at the top of my +page.svelte like this:
import { _ } from ‘$env/static/private’;
Like I said, no idea how I triggered it but I’ve had similar problems with VSCode, or some add-in creating unwanted imports over the last few months.
You saved my day, VSC is really annoying for that :D
Got this today and the error seemed quite cryptic. My issue was related to how I was importing something.
/* Album.svelte */
// Crashes app with "cannot import $env/static/private into client-side code" error
import { AlbumEntity } from '$db/album-collection'
// Works perfectly fine, no errors
import type { AlbumEntity } from '$db/album-collection'
This error is so annoying - it's just popped up out of nowhere, and I do not use any private variables.
Why cant there be a clear stack trace that points one to the actual source of the error?
This is such as bad DX.
3:56:59 AM [vite] Internal server error: Cannot import $env/static/private into client-side code
at LoadPluginContext.load (file:///Users/thebeachmaster/git/<REDACTED>/portal/node_modules/@sveltejs/kit/src/exports/vite/index.js:401:12)
at PluginContainer.load (file:///Users/thebeachmaster/git/<REDACTED>/portal/node_modules/vite/dist/node/chunks/dep-CDnG8rE7.js:49002:17)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async loadAndTransform (file:///Users/thebeachmaster/git/<REDACTED>/portal/node_modules/vite/dist/node/chunks/dep-CDnG8rE7.js:51807:22)
at async viteTransformMiddleware (file:///Users/thebeachmaster/git/<REDACTED>/portal/node_modules/vite/dist/node/chunks/dep-CDnG8rE7.js:61824:24)
What exactly am I supposed to get from this?
VS Code (or a Svelte plugin?) will automatically add import { _ } from '$env/static/private';
to your file if you type an underscore and then miss the autocomplete prompt and close the parentheses slug.set(_)
- even if you fix it with your intended code slug.set(_slug)
. Deleting the import line fixes this issue but this "feature" is a little greedy if you ask me.
Describe the bug
This seems like a bug even though it could be the desired behaviour, but here I go:
Having a file
functions.js
including two functions:that supposedly fetches data from an API and requires access to
API_TOKEN` (a private and sensitive environment variable)multiply
that just takes 2 parameters and returns a number, without access to the env variableIt then fire this error:
If you comment the lines 2 and 8 of
src/routes/+pages.svelte
, then you have access to the information.This sort of makes sense but since
multiply
does not useAPI_TOKEN
, I don't see why we would have to separate functions into different files?A solution could be to isolate the functions that require access to private environment variables. Or am I missing something here?
Reproduction
https://stackblitz.com/edit/sveltejs-kit-template-default-s8msqa?file=src/routes/+page.svelte
Logs
System Info
Severity
serious, but I can work around it
Additional Information
Discord thread if that helps https://discord.com/channels/457912077277855764/1009108558194950174