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.

Cannot use compiled package with jest due to ESM errors

See original GitHub issue

Description 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)
      
Screen Shot 2021-05-09 at 1 08 18 AM

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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:16 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
calvintwrcommented, Mar 21, 2022

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:

    moduleNameMapper: {
        '@master-chief/alpaca': '<rootDir>/node_modules/@master-chief/alpaca/dist/cjs/index.cjs',
    },
2reactions
calvintwrcommented, Mar 22, 2022

@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

Read more comments on GitHub >

github_iconTop 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 >

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