Skip to content

Commit

Permalink
Docs: Add section for @next/env package (vercel#64908)
Browse files Browse the repository at this point in the history
Related to: vercel/front#31685

---------

Co-authored-by: Lee Robinson <me@leerob.io>
  • Loading branch information
delbaoliveira and leerob committed Apr 23, 2024
1 parent 06db6ca commit 13a3e94
Showing 1 changed file with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,54 @@ export async function GET() {
</AppOnly>

### Loading Environment Variables with `@next/env`

If you need to load environment variables outside of the Next.js runtime, such as in a root config file for an ORM or test runner, you can use the `@next/env` package.

This package is used internally by Next.js to load environment variables from `.env*` files.

To use it, install the package and use the `loadEnvConfig` function to load the environment variables:

```bash
npm install @next/env
```

```tsx filename="envConfig.ts" switcher
import { loadEnvConfig } from '@next/env'

const projectDir = process.cwd()
loadEnvConfig(projectDir)
```

```jsx filename="envConfig.js" switcher
import { loadEnvConfig } from '@next/env'

const projectDir = process.cwd()
loadEnvConfig(projectDir)
```

Then, you can import the configuration where needed. For example:

```tsx filename="orm.config.ts" switcher
import './envConfig.ts'

export default defineConfig({
dbCredentials: {
connectionString: process.env.DATABASE_URL!,
},
})
```

```jsx filename="orm.config.js" switcher
import './envConfig.js'

export default defineConfig({
dbCredentials: {
connectionString: process.env.DATABASE_URL,
},
})
```

### Referencing Other Variables

Next.js will automatically expand variables that use `$` to reference other variables e.g. `$VARIABLE` inside of your `.env*` files. This allows you to reference other secrets. For example:
Expand Down

0 comments on commit 13a3e94

Please sign in to comment.