TEST
)NODE_ENV
)WEB_SERVER
)I have already tried using import { loadEnvConfig } from "@next/env"; loadEnvConfig(process.cwd());
which is supposed to load env. vars in custom servers in nextjs, but it doesn't change anything.
Repro: npx create-next-app@latest --example with-playwright with-playwright-app
example.spec.ts
import { test, expect } from '@playwright/test'
import { loadEnvConfig } from "@next/env";
loadEnvConfig(process.cwd());
test('', async () => {
const nodeEnv = process.env.NODE_ENV // undefined, should be "development"
const WEB_SERVER = process.env.WEB_SERVER // undefined, should be "true"
const test = process.env.TEST // true, works fine
})
.env
TEST="true"
playwright.config.ts
//...
webServer: {
command: 'npm run dev',
url: baseURL,
timeout: 120 * 1000,
reuseExistingServer: !process.env.CI,
env: {
WEB_SERVER: 'true',
}
},
//...
This thread is trying to answer question "Why are certain environment variables undefined in a Next.js application despite using the `loadEnvConfig` function from `@next/env`?"
Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].