question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

jest-resolve can't handle "exports"

See original GitHub issue

🐛 Bug Report

“exports” field seems to be ignored by jest-resolve, resulting with unexpected “Cannot find module …” error thrown even though Node is capable of resolving the paths.

https://nodejs.org/api/esm.html#esm_package_entry_points

To Reproduce

Steps to reproduce the behavior:

I have a simple utils package in my workspace, that’s shaped like this:

/dist
  array.js
  … 
/src
  array.js
  …
package.json

package.json:

{
  "name": "@my-org/utils",
  "version": "1.0.0",
  "exports": {
    "./array": "./dist/array.js",
    "./boolean": "./dist/boolean.js",
    "./math": "./dist/math.js",
    "./number": "./dist/number.js",
    "./object": "./dist/object.js",
    "./promise": "./dist/promise.js",
    "./string": "./dist/string.js"
  }
}

This allows me to use it in my main project by importing utils like this:

import { min } from '@my-org/utils/math'

This works both in native ES modules environment in Node 14, and in code transpiled by Babel. But this code crashes in Jest tests.

Expected behavior

“exports” to be respected and understood by jest-resolve

Link to repl or repo (highly encouraged)

https://github.com/wojtekmaj/resolve-exports-issue

envinfo

  System:
    OS: macOS 11.0
    CPU: (8) x64 Intel(R) Core(TM) i5-8259U CPU @ 2.30GHz
  Binaries:
    Node: 14.5.0 - /usr/local/bin/node
    Yarn: 2.1.1 - /usr/local/bin/yarn
    npm: 6.14.5 - /usr/local/bin/npm
  npmPackages:
    jest: ^26.4.0 => 26.4.0 

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

1reaction
guilleflmpeyacommented, Apr 30, 2021

I found this post after the same happened to me. I´m using node 12.22.1 Client project dependencies

"devDependencies": {
    "@babel/cli": "^7.13.14",
    "@babel/core": "^7.13.15",
    "@babel/node": "^7.13.13",
    "@babel/preset-env": "^7.13.15",
    "@types/jest": "^26.0.22",
    "babel-jest": "^26.6.3",
    "babel-loader": "^8.2.2",
    "circular-dependency-plugin": "^5.2.2",
    "enhanced-resolve": "^5.8.0",
    "jest": "^26.6.3",
    "jest-sonar-reporter": "^2.0.0",
    "npm-run-all": "^4.1.5",
    "sonarqube-scanner": "^2.8.0",
    "webpack": "^5.33.1",
    "webpack-bundle-analyzer": "^4.4.1",
    "webpack-cli": "^4.6.0"
  }

exporter lib config

"main": "./lib/main.js",
  "exports": {
    ".": "./lib/main.js",
    "./*": "./lib/*.js"
  },
  "files": [
    "lib"
  ],

I found an easy way to implemente a workaround. Hope it can help someone until Jest add support for this case. In my case, inside my package, I had globalHeaders.js with some named exports. So I was importing like this.

import {
  GLOBAL_HEADERS,
} from "@domain/package-name/globalHeaders";

but I got, can’t find module error. Since Jest does not supports “exports”: { “.”: “./lib/main.js”, “./": "./lib/.js” },

I try change my import to

import {
  GLOBAL_HEADERS,
} from "@domain/package-name/lib/globalHeaders";

It worked, but still far from elegant, so I also added this to my Jest config file

moduleNameMapper: {
    "^@domain/package-name/(.*)$": "@appscore/package-name/lib/$1",
  },

And now I can do this as it supposed to be from start

import {
  GLOBAL_HEADERS,
} from "@domain/package-name/globalHeaders";
0reactions
github-actions[bot]commented, May 31, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest gives an error: "SyntaxError: Unexpected token export"
This works for me, in my case Jest can't deal with imports from lodash-es . Whitelisting the package like this resolves the issue....
Read more >
Using with webpack - Jest
Let's start with a common sort of webpack config file and translate it to a Jest setup. webpack.config.js. module.exports = {
Read more >
Nx 14 with Jest 28: Jest failed to parse a file - Zero to production
This is caused by Jest 28 that does not handle all type of export properly, (see this issue for more details)[https://github.com/facebook/jest/ ...
Read more >
jest-node-exports-resolver - npm
Jest resolver to handle node's 'exports' package entry points.. Latest version: 1.1.6, last published: 9 months ago.
Read more >
TypeScript Jest: Unexpected Token Export - Reddit
However, despite following dozens of how-tos and tutorials, I am not able to fix the issue below. Jest cannot seem to parse the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found