-
-
Notifications
You must be signed in to change notification settings - Fork 379
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
Cannot find module '@middy/core' when running jest with typescript and esm #1148
Comments
**I am experiencing the same issue! ⬆️ 😞 ** When running:
Where:
Dependencies:
My
My
Notes:
|
This is blocking us from upgrading to v5. We don't have a wildly exotic
|
I've spent days fighting this thinking it was my setup. This is really critical |
The ESM part of the middy package hasn't changed. Based on the configs above, CJS isn't supposed to be used. A quick search on StackOverflow shows this kind of error is very common for jest. I found this pinned on the jest project: jestjs/jest#9430 (Native support for ES Modules). |
Thanks @willfarrell I will look into the link you provided. However, the issue I had was specific to @middy.. In other words, all other dependencies using ESM are working. Can you validate if, following one of the setups we described above (with the provided dependencies versions), it works? If it does, can you share with us what’s missing or not working properly. As you can see, this issue seems to happen to many people who have tried and followed the middy documentation already. (I will still investigate the link you provided next week. Hoping and find an information that could be missing to allow Jest to find the @middy module… and share my finding here) |
I personally don't use jest or typescript. Any documentation on these has been mostly community provided. Sorry I can't be more help here.
Appreciated, a PR is most welcome. |
@willfarrell -- apologies for piling on, that was not my intent. I think the takeaway is that though ESM should be universal, packages like jest are throwing a wrench in their adoption. I've tried switching over jest to ESM only, and I'm past the middy error but now fighting other packages that aren't compatible with that mode. Example:
|
It's all good, apologies not necessary. We anticipated non-runtime packages (build, test, etc) might have issues. The struggle to maintain CJS with all the edge cases they create was one of the reason we chose to drop support. We know they will natively support ESM one day soon, the whole industry is moving towards it. Please let them know it's important to natively support ESM. In the meantime v4.x is super stable to use while you wait for jest and others to upgrade. |
Please try this, it works for my case. In the
|
I've followed all the suggestions listed here and am also still having the same issue after upgrading from Middy 4 to 5. |
For reference, I downgraded the version to 4. I'm also using yarn PNP, so that made things more difficult |
yup doesn't work for me either. maybe you can give the whole setup? |
@minhhuynh-smg thanks that worked for me. Here is my whole config:
|
Would someone like to update the docs to reflect what's missing from the docs? |
The docs as described in the middy files didn't quite help me. The biggest impact was NODE_OPTIONS=--experimental-vm-modules, although this causes another set of issues. The simplest middy + jest setup I could create https://github.com/jrobens/middyskel. Not very 'deterministic' as there are a lot of moving parts. |
Simply ignoring the middy modules and adding module mapping config solved the problem. Using ESM with Jest caused a lot of problems, so I removed the
|
Downgrading the version from 5 to "@middy/core": "^4.2.7" solved the issue for me. |
If someone still has the same issue, I have found a solution. This is a common issue when working with ESM (ECMAScript Modules) in Jest. The error occurs because @middy/core is using ES modules (import syntax), but Jest is configured to handle CommonJS modules by default. { after that, you will need two packages in devdep:
then you will need a babel.config.js file with this content: let me know if this works for others as well |
Describe the bug
hello I am running typescript with esm and jest and I have a problem with middy:
I had this test:
which worked fine with the configurations I supplied in the "To Reproduce section" and
npm run test
command.howerver when I changed it to look like this:
I get this error
To Reproduce
How to reproduce the behaviour:
tsconfig.json
:jest.config.cjs
package.json
:handler:
Expected behaviour
I would expect the test to pass when I run
npm run test
Environment (please complete the following information):
Additional context
what I have tried so far step by step:
changing
test
script inpackge.json
to this"NODE_OPTIONS=--experimental-vm-modules jest -c jest.config.cjs"
as mentioned here
result was the same
changing
jest.config.cjs
to this:as mentioned here
result was the same
jest.config.cjs
to this:as mentioned here
result was the same
tsconfig.json
to this:as mentioned here
got this error:
The text was updated successfully, but these errors were encountered: