Rayrun
← Back to Discord Forum

Undefined environment variables

german.jabloposted in #help-playwright
Open in Discord
german.jablo
german.jablo
  1. env. vars explicitly defined in .env file work fine (see ex: TEST)
  2. env. vars "native" are undefined (ex: NODE_ENV)
  3. env. vars in webserver are undefined (see ex: 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`?"

0 replies

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

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 luc@ray.run.