You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Use <ImportMetaprop="env"/> to access environment variables.
46
+
Use <ImportMetaprop="env.SOME_ENV"/> to access environment variables.
47
47
48
48
```ts
49
49
// /pages/movies/+data.ts
50
+
// Environment: server
50
51
51
52
importtype { PageContextServer } from'vike/types'
52
53
@@ -62,14 +63,13 @@ export async function data(pageContext: PageContextServer) {
62
63
}
63
64
```
64
65
65
-
> For server only code, you can access environment variables using [`process.env`](https://nodejs.org/en/learn/command-line/how-to-read-environment-variables-from-nodejs), but we generally recommend against this because it won't be <Linkhref="#static-replacement">statically replaced</Link>.
66
+
`import.meta.env.PUBLIC_ENV__*`variables are <Linkhref="#define">also available on the client-side</Link>:
66
67
67
68
```jsx
68
69
// components/ContactUs.jsx
70
+
// Environment: client
69
71
70
-
// The UI component <ContactUs> is loaded on the client-side
71
-
72
-
// This environment variable is available on the client-side: it's prefixed with PUBLIC_ENV__
72
+
// This environment variable is available on the client because it's prefixed with PUBLIC_ENV__
> For server-only code, you can access environment variables using [`process.env`](https://nodejs.org/en/learn/command-line/how-to-read-environment-variables-from-nodejs), but we generally recommend `import.meta.env.SOME_ENV` instead because <Link href="#static-replacement">it's statically replaced for minimal bundle sizes</Link>.
91
+
> ```ts
92
+
>// ✅ Works, eveywhere and statically replaced (minimizing bundle sizes)
93
+
>import.meta.env.SOME_ENV
94
+
>// ⚠️ Works, but only on server-side and no static replacement
95
+
>process.env.SOME_ENV
96
+
>// ⚠️ Works, but only on server-side and no static replacement
97
+
>const { SOME_ENV } =process.env
98
+
>```
99
+
90
100
91
101
## TypeScript
92
102
@@ -99,7 +109,9 @@ For improved DX, consider using [`zod`](https://github.com/colinhacks/zod), for
99
109
100
110
## Static replacement
101
111
102
-
For better [tree shaking](https://rollupjs.org/introduction/#tree-shaking) (aka dead code elimination), `import.meta.env.SOME_ENV` is statically replaced with its value. For example:
112
+
`import.meta.env.SOME_ENV` is statically replaced with its value at build time, enabling [tree shaking](https://rollupjs.org/introduction/#tree-shaking) (dead code elimination).
113
+
114
+
For example:
103
115
104
116
```js
105
117
// src/someFile.js [source code in your Git repository]
@@ -119,7 +131,7 @@ if (import.meta.env.DISABLE_TRACKING === 'true') {
119
131
DISABLE_TRACKING='true'
120
132
```
121
133
122
-
The file`src/someFile.js`is built to:
134
+
After build,`src/someFile.js`becomes:
123
135
124
136
```js
125
137
// dist/server/chunks/chunk-DdwTql6x.js [built code deployed to production]
Note how `import()` is completely removed, resulting in a more lightweight production bundle.
143
+
The `import()` call is completely removed, resulting in a more lightweight production bundle.
132
144
133
145
134
146
## Config files
135
147
136
148
In config files, [<ImportMetaprop="env"/> isn't available](https://github.com/vikejs/vike/issues/1726#issuecomment-2208626928) (neither `vite.config.js` nor `+config.js`).
137
149
138
-
Use [`process.env`](https://nodejs.org/en/learn/command-line/how-to-read-environment-variables-from-nodejs) instead of <ImportMetaprop="env"/>.
150
+
Use [`process.env`](https://nodejs.org/en/learn/command-line/how-to-read-environment-variables-from-nodejs) instead.
0 commit comments