Skip to content

Vitest shows misleading Dynamic access of "import.meta.env" is not supported error for static access #7888

@alexmnv

Description

@alexmnv

When running Vitest on a project using Vite 6 and Vike (>=0.4.221), Vitest throws an error:

Error: [module runner] Dynamic access of "import.meta.env" is not supported. Please, use "import.meta.env.PUBLIC_ENV__FOO" instead.

This error occurs when Vitest processes a file (in my case, a Vike config file like +bodyHtmlBegin.ts) that contains static access to an environment variable, like:

export const bodyHtmlBegin = import.meta.env.PUBLIC_ENV__FOO;

While the trigger involves Vike, the inaccurate error message might be an issue on Vitest side.

Additional Context:

This issue was initially reported and discussed in the Vike project here: vikejs/vike#2412

cc @brillout

Reproduction

  1. Clone the repo:
git clone git@github.com:alexmnv/vike-env-issue-test.git
cd vike-env-issue-test
npm install
  1. Run Vitest:
npm run test

Expected Behavior:

Vitest should not throw a "Dynamic access" error when the code uses static access (import.meta.env.VAR).

Actual Behavior:

Vitest throws the following misleading error:

Error: [module runner] Dynamic access of "import.meta.env" is not supported. Please, use "import.meta.env.PUBLIC_ENV__FOO" instead.

System Info

System:
    OS: Linux 6.8 Ubuntu 22.04.5 LTS 22.04.5 LTS (Jammy Jellyfish)
    CPU: (16) x64 AMD Ryzen 7 5800U with Radeon Graphics
    Memory: 6.54 GB / 30.67 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 22.3.0 - ~/.nvm/versions/node/v22.3.0/bin/node
    npm: 10.8.1 - ~/.nvm/versions/node/v22.3.0/bin/npm
  Browsers:
    Chrome: 135.0.7049.95

Used Package Manager

npm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    p3-minor-bugAn edge case that only affects very specific usage (priority)

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions