Cannot use compiled package with jest due to ESM errors
See original GitHub issueDescription Running into ESM related error trying to use the package with jest
Expected Package should work with jest on node v14. My suspicion is something isn’t transpiled correctly?
Reproduction Example repo that reproduces the error: https://github.com/neeschit/alpaca-jest-issue
Logs
Details:
/Users/nranganath/playground/alpaca-jest-test/node_modules/@master-chief/alpaca/dist/mjs/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export { AlpacaClient } from './client.js';
^^^^^^
SyntaxError: Unexpected token 'export'
> 1 | import { AlpacaClient } from "@master-chief/alpaca";
| ^
2 |
3 | export const client = new AlpacaClient({
4 | credentials: {
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)
at Object.<anonymous> (index.ts:1:1)
Additional
I’ve tried using transformIgnorePatterns
as specified here, but no luck
FYI, I can get it to run just fine with node directly, just jest that’s a problem
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:16 (2 by maintainers)
Top Results From Across the Web
Cannot use compiled package with jest due to ESM errors #69
Description Running into ESM related error trying to use the package with jest. Expected Package should work with jest on node v14.
Read more >How to use ESM tests with jest? - Stack Overflow
Here are the steps I took to run Jest with a test using ESM. The source files under test were also written using...
Read more >Configuring Jest
Jest will run .mjs and .js files with nearest package.json 's type field set to module as ECMAScript Modules.
Read more >TypeScript Jest: Cannot use import statement outside module
The TypeScript jest error "Cannot use import statement outside module" occurs when we misconfigure jest in a TypeScript project and run test files...
Read more >module-not-found - Next.js
A module not found error can occur for many different reasons: The module you're trying to ... The swr module has to be...
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 think it has to do with the CommonJS and ES6 module issues.
Jest is trying to transpile the mjs files but not able to do it correctly. We were able to fix this by directing Jest to use the cjs files:
try this in your jest.config.js:
@117 hey nico much thanks. It’s very nice of you to credit. If I may could you help to credit @wailinkyaww as well? We found the solution together.
P.S However the underlying issue is not really resolved, but I guess we can go with this. I met similar problems with my repos. I just ditched the whole cjs and mjs altogether, and output a commonJS distro that is compatible with both require and import interfaces:
https://github.com/calvintwr/not/commit/6ce195d09b224f2211b4c6f62c8851abfae3930d