|
1
|
--- |
|
2
|
sidebar_label: "admin-env-vars-import-meta" |
|
3
|
--- |
|
4
|
|
|
5
|
export const metadata = { |
|
6
|
title: `admin-env-vars-import-meta - ESLint plugin rules`, |
|
7
|
} |
|
8
|
|
|
9
|
# {metadata.title} |
|
10
|
|
|
11
|
This rule requires admin code to use `import.meta.env` instead of `process.env` for environment variables, since admin code is bundled for the browser by Vite. |
|
12
|
|
|
13
|
## Severity |
|
14
|
|
|
15
|
`warn`. This rule is enabled in the [recommended](../../page.mdx#recommended) preset. |
|
16
|
|
|
17
|
## What it Targets |
|
18
|
|
|
19
|
This rule targets files in your project's `src/admin` directory. It reports access to `process.env`. |
|
20
|
|
|
21
|
The following code is reported by the rule: |
|
22
|
|
|
23
|
```tsx title="src/admin/widgets/product-widget.tsx" |
|
24
|
const url = process.env.VITE_BACKEND_URL |
|
25
|
``` |
|
26
|
|
|
27
|
Instead, use `import.meta.env`: |
|
28
|
|
|
29
|
```tsx title="src/admin/widgets/product-widget.tsx" |
|
30
|
const url = import.meta.env.VITE_BACKEND_URL |
|
31
|
``` |
|
32
|
|
|
33
|
## Why it's Important |
|
34
|
|
|
35
|
Admin code runs in the browser via Vite, where `process.env` is undefined. Using `import.meta.env` ensures your environment variables resolve correctly at runtime. |
|
36
|
|
|
37
|
Learn more in the [Environment Variables in Admin Development documentation](!docs!/learn/fundamentals/admin/environment-variables). |
|
38
|
|
|
39
|
## Fixable |
|
40
|
|
|
41
|
This rule is auto-fixable. Run the linter with the `--fix` option to apply the fix automatically. |
|
42
|
|
|
43
|
## Turn it Off |
|
44
|
|
|
45
|
To turn off this rule, set it to `off` in your ESLint configuration: |
|
46
|
|
|
47
|
```ts title="eslint.config.ts" |
|
48
|
import { defineConfig } from "eslint/config" |
|
49
|
import medusa from "@medusajs/eslint-plugin" |
|
50
|
|
|
51
|
export default defineConfig([ |
|
52
|
...medusa.configs.recommended, |
|
53
|
{ |
|
54
|
rules: { |
|
55
|
"@medusajs/admin-env-vars-import-meta": "off", |
|
56
|
}, |
|
57
|
}, |
|
58
|
]) |
|
59
|
``` |
|
60
|
|
|
61
|
Or disable it for a single line using an inline comment: |
|
62
|
|
|
63
|
```ts |
|
64
|
// eslint-disable-next-line @medusajs/admin-env-vars-import-meta |
|
65
|
console.log(process.env.MEDUSA_BACKEND_URL) |
|
66
|
``` |