-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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 ESM (ts-jest) can't find import from CJS module #12120
Comments
The same problem. Any updates? |
I've also run into this on a minimal project (not TypeScript), after following a suggestion that I should install |
we had a similar issue. here is described, what we did to keep hopefully everything in esm-mode and not having any cjs produced on the way. : svelteness/svelte-jester#72 (comment) |
Hi, I had the same issue, spent a lot time on it but eventually fixed by changing jest config like described here |
I am facing the same issue. Does anyone have a solution? If I modify manually a package.json from my node_modules adding type: "module" and modifying main to point to the esm dist, it will work. If I do it through the resolver.js, it is not working. |
@themaskedavenger I'm able to get it working on your repro (here is my fork of your repo: https://github.com/cyberuni/jest-esm-apollo-client) In my repo I'm using @repobuddy/jest which I'm working on, trying to simplify all these jest config issues. |
I'm having a similar issue, I think it fits in here. My problem is that, even though the packages from Of course I could try to apply some of the transformations referred in this thread, but what confuses me is why is it even trying to load the ESM files when we have CJS ones available (and they are properly declared in the respective |
Better open new issue with minimal reproduction. It is impossible to guess what is going on from your description. |
I'm also experiencing this issue, did you create an issue or managed to resolve it? |
|
@NiklasPor I didn't had time to prepare a minimum reproducible example for this problem and to write a detailed issue yet. Will do. |
Why do we need to compile the ESM to CJS when there is already a CJS compiled version in the node_modules directory? For example, package.json contains:
And the node_module itself contains both ESM and CJS:
Despite this layour, Jest complains about exports coming from the dist directory in |
If you run Jest in ESM mode, then it will use the As for the OP - you cannot import // file.mjs
import {
ApolloClient,
InMemoryCache,
ApolloProvider,
useQuery,
gql
} from '@apollo/client'; $ node file.mjs
file:///Users/simen/wat/file.mjs:2
ApolloClient,
^^^^^^^^^^^^
SyntaxError: Named export 'ApolloClient' not found. The requested module '@apollo/client' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:
import pkg from '@apollo/client';
at ModuleJob._instantiate (node:internal/modules/esm/module_job:131:21)
at async ModuleJob.run (node:internal/modules/esm/module_job:213:5)
at async ModuleLoader.import (node:internal/modules/esm/loader:316:24)
at async loadESM (node:internal/process/esm_loader:34:7)
at async handleMainPromise (node:internal/modules/run_main:66:12)
Node.js v20.9.0 {
"devDependencies": {
"@apollo/client": "^3.8.7",
"graphql": "^16.8.1"
}
} With that I'll close this issue. If people are having other issues, feel free to open a new one with a minimal reproduction |
Possibly related issue that also mentions a temporary workaround as well (that worked for me): #11563 |
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. |
Version
27.4.3
Steps to reproduce
Minimal repro: https://github.com/themaskedavenger/jest-esm-apollo-client
run
yarn
and thenyarn test:jest
also to see the original issue replace the imports (lines 4-10) with:
Expected behavior
Would expect to have Jest be able to properly import those components and see the tests run (and hopefully pass!).
Actual behavior
When using these imports:
Each of the imports gives an error like
SyntaxError: The requested module '@apollo/client' does not provide an export named 'ApolloProvider'
. Looking at the@apollo/client
's package.json, the"main"
key is set to"./main.cjs"
, which is re-exporting things in cjs style:For some reason I guess this cjs is not recognized with the recommended jest config. To get around it, can directly import the specific exports directly from esm files in the
@apollo/client
package like this:Then there's a new error for a dependency:
Adding
transformIgnorePatterns
to jest config doesn't seem to help:Any thoughts on how to fix?
Full jest config:
and tsconfig:
Additional context
Also tried using this jest resolver suggested in the docs:
I notice that the
@apollo/client
package has both"module"
and"main"
keys. But using this resolver it errors for other modules:Is it possible to utilize the esm
"module"
field in package.json with jest ESM? Seemed like the obvious choice for ESM jest but it creates all kinds of errors like this. Is it because cjs modules in node_modules are trying to load the ESM exports?Environment
The text was updated successfully, but these errors were encountered: