Skip to content

[Vite 8][Regression] import.meta.url incorrectly resolved when formats: ['cjs'] #21974

@axel7083

Description

@axel7083

Describe the bug

When have a vite 7 repository, and the dependabot upgrade is failing, after investigating we were getting the following error

Stack trace:
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string or an instance of URL. Received undefined
    at fileURLToPath (node:internal/url:1606:11)

The problem is coming from inside the ps-list we are using;

Checking the compile code with vite 8.0.1 we have

var __dirname$1 = node_path.default.dirname((0, node_url.fileURLToPath)({}.url));

Looking at the corresponding source code we have

const __dirname = path.dirname(fileURLToPath(import.meta.url));

When adding platform: "node" in the rollupOptions the compile code change to

var __dirname$1 = node_path.default.dirname((0, node_url.fileURLToPath)(require("url").pathToFileURL(__filename).href));

However, according to the documentation, this should not be necessary as we use the format cjs, and in the documentation it is written

Default

  • 'node' if the format is 'cjs'
  • 'browser' for other formats1

Reproduction

https://github.com/axel7083/vite-cjs-node-reproducer

Steps to reproduce

  1. Checkout https://github.com/axel7083/vite-cjs-node-reproducer
  2. Run npm install
  3. Run npm run demo
  4. Assert
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string or an instance of URL. Received undefined

System Info

System:
    OS: Windows 11 10.0.22000
    CPU: (16) x64 AMD Ryzen 7 5800XT 8-Core Processor            
    Memory: 9.06 GB / 30.94 GB
  Binaries:
    Node: 24.11.1 - C:\Program Files\nodejs\node.EXE
    npm: 11.6.2 - C:\Program Files\nodejs\npm.CMD
    pnpm: 10.28.0 - C:\Program Files\nodejs\pnpm.CMD
  Browsers:
    Edge: Spartan (44.22000.120.0), Chromium (143.0.3650.139)
    Internet Explorer: 11.0.22000.120
  npmPackages:
    vite: ^8.0.1 => 8.0.1

Used Package Manager

pnpm

Logs

No response

Validations

Footnotes

  1. https://rolldown.rs/reference/InputOptions.platform#platform

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions