Skip to content

[Bug]: rspack 2 beta version: import.meta.dirname is undefined in CJS context #13420

Description

@xfsnowind

System Info

System:
OS: macOS 15.7.4
CPU: (10) arm64 Apple M1 Pro
Memory: 113.05 MB / 16.00 GB
Shell: 4.4.0 - /opt/homebrew/bin/fish
Binaries:
Node: 25.8.1 - /opt/homebrew/bin/node
npm: 11.11.0 - /opt/homebrew/bin/npm
pnpm: 10.30.3 - /opt/homebrew/bin/pnpm
Deno: 2.6.8 - /opt/homebrew/bin/deno
Browsers:
Brave Browser: 142.1.84.141
Chrome: 146.0.7680.153
Chrome Canary: 142.0.7444.3
Edge: 126.0.2592.81
Firefox: 137.0.2
Safari: 26.3
npmPackages:
@rspack/cli: 2.0.0-beta.7 => 2.0.0-beta.7
@rspack/core: 2.0.0-beta.7 => 2.0.0-beta.7
@rspack/dev-server: 2.0.0-beta.7 => 2.0.0-beta.7
@rspack/plugin-react-refresh: 1.6.0 => 1.6.0

Details

I tried rspack 2.0.0-beta.7 with cypress component test, but it reports the error

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined
    at join (node:path:1339:7)
    at compiler (/Users/josephfeng/Projects/Work/testrspack/node_modules/.pnpm/@[email protected]/node_modules/@rspack/core/dist/index.js:2492:167)
    at Object.<anonymous> (/Users/josephfeng/Projects/Work/testrspack/node_modules/.pnpm/@[email protected]/node_modules/@rspack/core/dist/index.js:13523:16)
    at Module._compile (node:internal/modules/cjs/loader:1829:14)
    at Object.transformer (/Users/josephfeng/Library/Caches/Cypress/15.12.0/Cypress.app/Contents/Resources/app/node_modules/tsx/dist/register-D46fvsV_.cjs:3:1104)
    at Module.load (node:internal/modules/cjs/loader:1552:32)
    at Module._load (node:internal/modules/cjs/loader:1354:12)
    at wrapModuleLoad (node:internal/modules/cjs/loader:255:19)
    at Module.require (node:internal/modules/cjs/loader:1575:12)
    at require (node:internal/modules/helpers:191:16)
    at Object.<anonymous> (/Users/josephfeng/Projects/Work/testrspack/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected]/node_modules/cypress-rspack-dev-server/dist/makeDefaultRspackConfig.js:7:16)
    at Module._compile (node:internal/modules/cjs/loader:1829:14)
    at Object.transformer (/Users/josephfeng/Library/Caches/Cypress/15.12.0/Cypress.app/Contents/Resources/app/node_modules/tsx/dist/register-D46fvsV_.cjs:3:1104)
    at Module.load (node:internal/modules/cjs/loader:1552:32)
    at Module._load (node:internal/modules/cjs/loader:1354:12)
    at wrapModuleLoad (node:internal/modules/cjs/loader:255:19)
    at Module.require (node:internal/modules/cjs/loader:1575:12)
    at require (node:internal/modules/helpers:191:16)
    at Object.<anonymous> (/Users/josephfeng/Projects/Work/testrspack/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected]/node_modules/cypress-rspack-dev-server/dist/makeRspackConfig.js:10:35)
    at Module._compile (node:internal/modules/cjs/loader:1829:14)
    at Object.transformer (/Users/josephfeng/Library/Caches/Cypress/15.12.0/Cypress.app/Contents/Resources/app/node_modules/tsx/dist/register-D46fvsV_.cjs:3:1104)
    at Module.load (node:internal/modules/cjs/loader:1552:32)

Which I checked is caused by the value import.meta.dirname in here.

The problem would be solved if I replace the code with:

typeof __dirname !== "undefined" ? __dirname : import.meta.dirname

I think it's because cypress is using CJS context while rspack2 has turned to ESM module totally.

To reproduce this, you can try this repo,
run pnpm cypress:run, it would show the error.

Reproduce link

https://github.com/xfsnowind/test-rspack2-beta

Reproduce Steps

  1. Run pnpm install
  2. Run pnpm cypress:run
  3. It would show the error

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions