Skip to content

Prefixless env var with import.meta.env.* via define config not available #9191

@leepowelldev

Description

@leepowelldev

Describe the bug

I have a config with several defines in it:

{
  define: {
    'import.meta.env.FOO': JSON.stringify(process.env.FOO),
    'import.meta.env.BAR': JSON.stringify(process.env.BAR),
  }
}

This follows the recommendation from https://vite.dev/config/shared-options#envprefix. This works as expected in Vite - however in Vitest Browser Mode they are undefined. I have set my config to extend the main config, and other values work such as:

{
  define: {
    '__FOO__': JSON.stringify(process.env.FOO),
    '__BAR__': JSON.stringify(process.env.BAR),
  }
}

However the usage with import.meta.env does not seem to work as expected.

Reproduction

Can produce a skeleton repo if required

System Info

System:
    OS: macOS 15.7.2
    CPU: (10) arm64 Apple M1 Pro
    Memory: 191.08 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.14.0 - /Users/me/.nvm/versions/node/v22.14.0/bin/node
    npm: 11.6.2 - /Users/me/.nvm/versions/node/v22.14.0/bin/npm
    pnpm: 9.15.6 - /Users/me/.nvm/versions/node/v22.14.0/bin/pnpm
    Watchman: 2025.05.26.00 - /opt/homebrew/bin/watchman
  Browsers:
    Chrome: 143.0.7499.41
    Firefox: 145.0.2
    Safari: 26.0.1
  npmPackages:
    @vitest/coverage-istanbul: ^4.0.15 => 4.0.15 
    @vitest/ui: ^4.0.15 => 4.0.15 
    vite: ^7.2.6 => 7.2.6 
    vitest: ^4.0.15 => 4.0.15 
    vitest-browser-react: ^2.0.2 => 2.0.2

Used Package Manager

pnpm

Validations

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions