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:
- Created 3 years ago
- Reactions:1
- Comments:5
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I found this post after the same happened to me. I´m using
node 12.22.1
Client project dependenciesexporter lib config
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.
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
It worked, but still far from elegant, so I also added this to my Jest config file
And now I can do this as it supposed to be from start
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.