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

[Bug]: jest-resolve doesn't read "exports" field #12723

Closed
guimard opened this issue Apr 23, 2022 · 2 comments
Closed

[Bug]: jest-resolve doesn't read "exports" field #12723

guimard opened this issue Apr 23, 2022 · 2 comments

Comments

@guimard
Copy link
Contributor

guimard commented Apr 23, 2022

Version

27.5.1

Steps to reproduce

In packages/jest-resolve/src/defaultResolver.ts, jest search only for "main" field to resolve package without checking if a "exports" field defines which file to read. This is different than what Node.js does to resolve package. This cause problems when a module is "type:module" and has a "exports->require" to be able to use it in a CommonJS file.

Here is the relevant part of packages/jest-resolve/src/defaultResolver.ts

  return function packageFilter(pkg, packageDir) {
    let filteredPkg = pkg;

    if (userFilter) {
      filteredPkg = userFilter(filteredPkg, packageDir);
    }

    if (filteredPkg.main != null) {
      return filteredPkg;
    }

    const indexInRoot = resolve(packageDir, './index.js');

Expected behavior

Follow Node.js behavior

Actual behavior

Module foo is a commonjs module. It uses require('bar'). Module bar is a ES module:

{ "name": "bar", "type":"module",
   "main": "index.js",
   "exports": {
     "import": "index.js",
     "require": "index.cjs"
   }
}

Jest reads bar/index.js instead of bar/index.cjs

Additional context

No response

Environment

Linux Debian/sid
@SimenB
Copy link
Member

SimenB commented Apr 24, 2022

Duplicate of #9771.

Fixed in Jest 28

@SimenB SimenB closed this as completed Apr 24, 2022
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 25, 2022
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