Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RollupError: "AUTH_SECRET" is not exported by "virtual:$env/static/private", imported by "src/hooks.server.ts" #11425

Closed
524c opened this issue Dec 21, 2023 · 17 comments · Fixed by #11436

Comments

@524c
Copy link
Contributor

524c commented Dec 21, 2023

Describe the bug

9: import {
10: AUTH_SECRET,
^
11: KEYCLOAK_ID,
12: KEYCLOAK_SECRET,
error during build:
RollupError: "AUTH_SECRET" is not exported by "virtual:$env/static/private", imported by "src/hooks.server.ts".
at error (file:///drone/src/node_modules/vite/node_modules/rollup/dist/es/shared/parseAst.js:337:30)
at Module.error (file:///drone/src/node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:12738:16)
at Module.traceVariable (file:///drone/src/node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:13175:29)
at ModuleScope.findVariable (file:///drone/src/node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:11592:39)
at Identifier.bind (file:///drone/src/node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:7179:40)
at Property.bind (file:///drone/src/node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:4625:23)
at ObjectExpression.bind (file:///drone/src/node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:4621:28)
at CallExpression.bind (file:///drone/src/node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:4621:28)
at CallExpression.bind (file:///drone/src/node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:8813:15)
at VariableDeclarator.bind (file:///drone/src/node_modules/vite/node_modules/rollup/dist/es/shared/node-entry.js:4625:23)

Reproduction

No response

Logs

No response

System Info

Need to install the following packages:
[email protected]
Ok to proceed? (y) y

  System:
    OS: macOS 14.2
    CPU: (10) arm64 Apple M2 Pro
    Memory: 120.92 MB / 32.00 GB
    Shell: 5.9 - /usr/local/bin/zsh
  Binaries:
    Node: 18.19.0 - ~/n/bin/node
    Yarn: 4.0.2 - ~/n/bin/yarn
    npm: 10.2.3 - ~/n/bin/npm
    bun: 1.0.18 - ~/.bun/bin/bun
  Browsers:
    Chrome: 120.0.6099.129
    Edge: 120.0.2210.89
    Safari: 17.2
  npmPackages:
    @sveltejs/adapter-auto: ^3.0.1 => 3.0.1
    @sveltejs/adapter-node: ^2.0.1 => 2.0.1
    @sveltejs/kit: ^2.0.4 => 2.0.4
    @sveltejs/vite-plugin-svelte: ^3.0.1 => 3.0.1
    svelte: ^4.2.8 => 4.2.8
    vite: ^5.0.10 => 5.0.10

Severity

blocking all usage of SvelteKit

Additional Information

"Dynamic environment variables can no longer be read during prerendering in SvelteKit 2 — you should use the static modules instead."

Sveltekit 2 changed the way we use dynamic private environments like import { env as privateEnv } from $env/dynamic/private, so the recommendation is to use $env/static/private.

What should we do now in a CI to be able to build?

My Drone CI build does not have any environment variables or static .env in the repository. The environment variables on my system are passed in the k8s deployment only during execution.

@524c
Copy link
Contributor Author

524c commented Dec 21, 2023

The only way now to be able to build will be to include all environment variables in Drone CI so that sveltekit 2 can import via $env/static/private during the build.

Was this change in Sveltekit 2 really necessary? Would there be any other solution to work with private dynamic variables?

@dummdidumm
Copy link
Member

Are you using prerendering? (Trying to understand if this would be fixable by relaxing the constraint to only error when env variables are accessed during prerendering)

@524c
Copy link
Contributor Author

524c commented Dec 21, 2023

Hi @dummdidumm,

This is my src/routes/+layout.ts

export const ssr = true;
export const prerender = false;

and still have problems with importing $env/dynamic/private

Well, I'll have to downgrade to Sveltekit 1 until this is resolved. It will be a hassle to have to include all the environment variables to be able to build the application.

Previously, if I needed to change the value of a variable, I would simply redeploy the application to my Kubernetes, which took seconds. Now I would have to do a new build to include a static variable, and it would take seconds to at least 10 minutes to do the entire process of building, creating the docker image and deploying the application to argo-cd.

@dummdidumm
Copy link
Member

Ok so you're not using prerendering - in that case, it should be possible to keep using $env/dynamic/.. and we need to fix it

@524c
Copy link
Contributor Author

524c commented Dec 21, 2023

Ok so you're not using prerendering - in that case, it should be possible to keep using $env/dynamic/.. and we need to fix it

Thank you very much, @dummdidumm!!

@524c
Copy link
Contributor Author

524c commented Dec 21, 2023

thanks!
Screenshot 2023-12-21 at 20 49 39

@PlkMarudny
Copy link

It seems to be still not working for me, using the same prerender = false I am getting during vite build:

"DISK_ROOT" is not exported by "virtual:$env/dynamic/private", imported by "src/routes/trim/+server.js".
file: C:/ ... /src/routes/trim/+server.js:1:9
1: import { DISK_ROOT, OUT_DIR, SERVER_PATH } from '$env/dynamic/private';
            ^
2: 
3: import { json } from '@sveltejs/kit';
error during build:
RollupError: "DISK_ROOT" is not exported by "virtual:$env/dynamic/private", imported by "src/routes/trim/+server.js".
    at error (file:///C:/ ... /node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/parseAst.js:337:30)
    at Module.error (file:///C:/ ... /node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:12759:16)  

During vite dev no problem.

"@sveltejs/kit": "^2.0.0"

@524c
Copy link
Contributor Author

524c commented Jan 15, 2024

It seems to be still not working for me, using the same prerender = false I am getting during vite build:

"DISK_ROOT" is not exported by "virtual:$env/dynamic/private", imported by "src/routes/trim/+server.js".
file: C:/ ... /src/routes/trim/+server.js:1:9
1: import { DISK_ROOT, OUT_DIR, SERVER_PATH } from '$env/dynamic/private';
            ^
2: 
3: import { json } from '@sveltejs/kit';
error during build:
RollupError: "DISK_ROOT" is not exported by "virtual:$env/dynamic/private", imported by "src/routes/trim/+server.js".
    at error (file:///C:/ ... /node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/parseAst.js:337:30)
    at Module.error (file:///C:/ ... /node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:12759:16)  

During vite dev no problem.

"@sveltejs/kit": "^2.0.0"

Update to the latest version of 2.0 which should resolve the issue. It was fixed in the version 2.0.6 https://github.com/sveltejs/kit/releases/tag/%40sveltejs%2Fkit%402.0.6

@PlkMarudny
Copy link

I have just tried, "@sveltejs/kit": "^2.3.2", still the same, unfortunately.

@PlkMarudny
Copy link

Actually, if this helps, I am trying to import .env variables in API routes.

@524c
Copy link
Contributor Author

524c commented Jan 15, 2024

Try run svelte-kit sync or remove .svelte-kit completely.

@PlkMarudny
Copy link

I am afraid this does not work either. OK, what I did is I created the fresh 'skeleton' project, JS + JSDoc:

pnpm create svelte@latest my-app
cd my-app
pnpm install
pnpm run dev

pnpm install output:

Packages are hard linked from the content-addressable store to the virtual store.    
  Content-addressable store is at: C:\Users\...\AppData\Local\pnpm\store\v3
  Virtual store is at:             node_modules/.pnpm
Progress: resolved 285, reused 250, downloaded 0, added 250, done

devDependencies:
+ @sveltejs/adapter-auto 3.1.0
+ @sveltejs/kit 2.3.2
+ @sveltejs/vite-plugin-svelte 3.0.1
+ @types/eslint 8.56.0 (8.56.2 is available)
+ eslint 8.56.0
+ eslint-config-prettier 9.1.0
+ eslint-plugin-svelte 2.35.1
+ prettier 3.2.2
+ prettier-plugin-svelte 3.1.2
+ svelte 4.2.8
+ svelte-check 3.6.3
+ typescript 5.3.3
+ vite 5.0.11
+ vitest 1.2.0

Done in 8.6s

I added a /src/routes/+layout.js with:

export const prerender = false;

I added /src/routes/+server.js with:

import { EXAMPLE } from "$env/dynamic/private"

Created also a .env file with:

EXAMPLE=test

Next, issued pnpm run build and the result was:

> [email protected] build C:\Users\...\Documents\js\my-app
> vite build

vite v5.0.11 building SSR bundle for production...
src/routes/+server.js (1:9) "EXAMPLE" is not exported by "virtual:$env/dynamic/private", imported by "src/routes/+server.js".
✓ 88 modules transformed.
vite v5.0.11 building for production...
✓ 60 modules transformed.
.svelte-kit/output/client/_app/version.json                             0.03 kB │ gzip:  0.05 kB
.svelte-kit/output/client/.vite/manifest.json                           2.26 kB │ gzip:  0.46 kB
.svelte-kit/output/client/_app/immutable/entry/start.IRGdKFW6.js        0.07 kB │ gzip:  0.08 kB
.svelte-kit/output/client/_app/immutable/nodes/0._8CBVaDm.js            0.60 kB │ gzip:  0.39 kB
.svelte-kit/output/client/_app/immutable/nodes/2.HmgQHYm4.js            0.69 kB │ gzip:  0.45 kB
.svelte-kit/output/client/_app/immutable/nodes/1.DWgraoQ-.js            0.97 kB │ gzip:  0.57 kB
.svelte-kit/output/client/_app/immutable/chunks/scheduler.zMJaRgub.js   2.16 kB │ gzip:  1.02 kB
.svelte-kit/output/client/_app/immutable/chunks/index.yy3khgz-.js       5.25 kB │ gzip:  2.21 kB
.svelte-kit/output/client/_app/immutable/entry/app.H6g-Nt4r.js          5.96 kB │ gzip:  2.41 kB
.svelte-kit/output/client/_app/immutable/chunks/entry.yQqEGdCS.js      25.76 kB │ gzip: 10.35 kB
✓ built in 749ms
.svelte-kit/output/server/.vite/manifest.json                   2.12 kB
.svelte-kit/output/server/entries/endpoints/_server.js          0.07 kB
.svelte-kit/output/server/entries/fallbacks/layout.svelte.js    0.24 kB
.svelte-kit/output/server/internal.js                           0.25 kB
.svelte-kit/output/server/entries/pages/_page.svelte.js         0.37 kB
.svelte-kit/output/server/entries/fallbacks/error.svelte.js     1.16 kB
.svelte-kit/output/server/chunks/ssr.js                         3.35 kB
.svelte-kit/output/server/chunks/internal.js                    5.90 kB
.svelte-kit/output/server/chunks/exports.js                     5.96 kB
.svelte-kit/output/server/index.js                            113.99 kB

Run npm run preview to preview your production build locally.

> Using @sveltejs/adapter-auto
  Could not detect a supported production environment. See https://kit.svelte.dev/docs/adapters to learn how to configure your app to run on the platform of your choosing  
  ✔ done
✓ built in 2.42s
 *  Terminal will be reused by tasks, press any key to close it. 
 

@janguardian
Copy link

Doesn't work for me — neither with the new skeleton project nor with the old one updated to 2.4.3.

error during build: RollupError: "MONGO_URL" is not exported by "virtual:$env/dynamic/private", imported by "src/db/connection.js".

Please, help. Am I missing something?

@WiseNat
Copy link

WiseNat commented Mar 28, 2024

Having the exact same issue - having to resort to using the VITE_ prefix with a process.env call

	"devDependencies": {
		"@sveltejs/adapter-auto": "^3.0.0",
		"@sveltejs/kit": "^2.0.0",
		"@sveltejs/vite-plugin-svelte": "^3.0.0",
		"svelte": "^4.0.5",
		"vite": "^5.0.0"
	},

@WiseNat
Copy link

WiseNat commented Apr 2, 2024

Found the solution and expectedly feel stupid, I guess I didn't read the documentation properly. Based on the error messages, it looks like it's the same issue both @janguardian and @PlkMarudny ran into.

In Sveltekit, dynamic and static environment variables are accessed differently.

Static

import { API_KEY } from '$env/static/private';

console.log(API_KEY)

Dynamic

import { env } from '$env/dynamic/private';

console.log(env.API_KEY)

@leejiulong
Copy link

Doesn't work for me — neither with the new skeleton project nor with the old one updated to 2.4.3.

error during build: RollupError: "MONGO_URL" is not exported by "virtual:$env/dynamic/private", imported by "src/db/connection.js".

Please, help. Am I missing something?

Check your .gitignore file,maybe .env in the ignore list

@leejiulong
Copy link

I am afraid this does not work either. OK, what I did is I created the fresh 'skeleton' project, JS + JSDoc:

pnpm create svelte@latest my-app
cd my-app
pnpm install
pnpm run dev

pnpm install output:

Packages are hard linked from the content-addressable store to the virtual store.    
  Content-addressable store is at: C:\Users\...\AppData\Local\pnpm\store\v3
  Virtual store is at:             node_modules/.pnpm
Progress: resolved 285, reused 250, downloaded 0, added 250, done

devDependencies:
+ @sveltejs/adapter-auto 3.1.0
+ @sveltejs/kit 2.3.2
+ @sveltejs/vite-plugin-svelte 3.0.1
+ @types/eslint 8.56.0 (8.56.2 is available)
+ eslint 8.56.0
+ eslint-config-prettier 9.1.0
+ eslint-plugin-svelte 2.35.1
+ prettier 3.2.2
+ prettier-plugin-svelte 3.1.2
+ svelte 4.2.8
+ svelte-check 3.6.3
+ typescript 5.3.3
+ vite 5.0.11
+ vitest 1.2.0

Done in 8.6s

I added a /src/routes/+layout.js with:

export const prerender = false;

I added /src/routes/+server.js with:

import { EXAMPLE } from "$env/dynamic/private"

Created also a .env file with:

EXAMPLE=test

Next, issued pnpm run build and the result was:

> [email protected] build C:\Users\...\Documents\js\my-app
> vite build

vite v5.0.11 building SSR bundle for production...
src/routes/+server.js (1:9) "EXAMPLE" is not exported by "virtual:$env/dynamic/private", imported by "src/routes/+server.js".
✓ 88 modules transformed.
vite v5.0.11 building for production...
✓ 60 modules transformed.
.svelte-kit/output/client/_app/version.json                             0.03 kB │ gzip:  0.05 kB
.svelte-kit/output/client/.vite/manifest.json                           2.26 kB │ gzip:  0.46 kB
.svelte-kit/output/client/_app/immutable/entry/start.IRGdKFW6.js        0.07 kB │ gzip:  0.08 kB
.svelte-kit/output/client/_app/immutable/nodes/0._8CBVaDm.js            0.60 kB │ gzip:  0.39 kB
.svelte-kit/output/client/_app/immutable/nodes/2.HmgQHYm4.js            0.69 kB │ gzip:  0.45 kB
.svelte-kit/output/client/_app/immutable/nodes/1.DWgraoQ-.js            0.97 kB │ gzip:  0.57 kB
.svelte-kit/output/client/_app/immutable/chunks/scheduler.zMJaRgub.js   2.16 kB │ gzip:  1.02 kB
.svelte-kit/output/client/_app/immutable/chunks/index.yy3khgz-.js       5.25 kB │ gzip:  2.21 kB
.svelte-kit/output/client/_app/immutable/entry/app.H6g-Nt4r.js          5.96 kB │ gzip:  2.41 kB
.svelte-kit/output/client/_app/immutable/chunks/entry.yQqEGdCS.js      25.76 kB │ gzip: 10.35 kB
✓ built in 749ms
.svelte-kit/output/server/.vite/manifest.json                   2.12 kB
.svelte-kit/output/server/entries/endpoints/_server.js          0.07 kB
.svelte-kit/output/server/entries/fallbacks/layout.svelte.js    0.24 kB
.svelte-kit/output/server/internal.js                           0.25 kB
.svelte-kit/output/server/entries/pages/_page.svelte.js         0.37 kB
.svelte-kit/output/server/entries/fallbacks/error.svelte.js     1.16 kB
.svelte-kit/output/server/chunks/ssr.js                         3.35 kB
.svelte-kit/output/server/chunks/internal.js                    5.90 kB
.svelte-kit/output/server/chunks/exports.js                     5.96 kB
.svelte-kit/output/server/index.js                            113.99 kB

Run npm run preview to preview your production build locally.

> Using @sveltejs/adapter-auto
  Could not detect a supported production environment. See https://kit.svelte.dev/docs/adapters to learn how to configure your app to run on the platform of your choosing  
  ✔ done
✓ built in 2.42s
 *  Terminal will be reused by tasks, press any key to close it. 
 

Check your .gitignore file,maybe .env in the ignore list

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants