Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when importing an ESM package via a CJS package #4332

Closed
6 tasks done
betabandido opened this issue Oct 18, 2023 · 1 comment
Closed
6 tasks done

Error when importing an ESM package via a CJS package #4332

betabandido opened this issue Oct 18, 2023 · 1 comment

Comments

@betabandido
Copy link

Describe the bug

I have created an ESM package (parent) that imports a CJS package (child), which in turn imports an ESM-only package (es-cookie).

When running vitest on the parent package I get the following error:

│ Error: require() of ES Module /home/runner/vitest-cjs/node_modules/.pnpm/es-cookie@1.4.0/node_modules/es-cookie/src/es-cookie.js from /home/runner/vite…
│ Instead change the require of es-cookie.js in /home/runner/vitest-cjs/packages/child/dist/cjs/index.js to a dynamic import() which is available in all …
│  ❯ async /home/runner/vitest-cjs/packages/parent/src/index.ts:1:31
│  ❯ async /home/runner/vitest-cjs/packages/parent/src/index.test.ts:2:31

When the child package is ESM instead of CJS, tests run as expected, though. Yet, we do not have control over this child package, so we cannot convert it to ESM. I have tried inlining es-cookie, but the issue did not resolve. This issue is preventing us from moving from jest to vitest.

Reproduction

I have created an environment that reproduces the error here: https://replit.com/@betabandido/vitest-cjs (I just realized a StackBlitz environment was requested, but hopefully the Replit one is good too; if that's not the case, please let me know, and I'll move it to StackBlitz).

To build and test both packages, run:

pnpm -r build
pnpm -r test

The second command should fail with the error described above.

System Info

System:
    OS: Linux 6.2 Ubuntu 20.04.2 LTS (Focal Fossa)
    CPU: (8) x64 Intel(R) Xeon(R) CPU @ 2.20GHz
    Memory: 42.94 GB / 62.79 GB
    Container: Yes
    Shell: 5.0.17 - /bin/bash
  Binaries:
    Node: 18.12.1 - /nix/store/dj805sw07vvpbxx39c8g67x8qddg0ikw-nodejs-18.12.1/bin/node
    Yarn: 1.22.19 - /nix/store/zdcnqq55qi214j46zgw2qa1jp4dpdf2m-yarn-1.22.19/bin/yarn
    npm: 8.19.2 - /nix/store/dj805sw07vvpbxx39c8g67x8qddg0ikw-nodejs-18.12.1/bin/npm
    pnpm: 8.9.2 - ~/vitest-cjs/.config/npm/node_global/bin/pnpm
  npmPackages:
    vitest: ^0.34.6 => 0.34.6

Used Package Manager

pnpm

Validations

@sheremet-va
Copy link
Member

sheremet-va commented Oct 18, 2023

This is expected behavior. You cannot use require (whenever it is) to import ES module.

You can try downgrading es-cookie to the version that ships CJS in package.json instead:

NPM/PNPM:

{
  "overrides": {
    "es-cookie": "1.3.2"
  }
}

Yarn:

{
  "resolutions": {
    "es-cookie": "1.3.2"
  }
}

@sheremet-va sheremet-va closed this as not planned Won't fix, can't repro, duplicate, stale Oct 18, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Nov 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants