alpaca-ts: Cannot use compiled package with jest due to ESM errors

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

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 16 (2 by maintainers)

Most upvoted 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:

    moduleNameMapper: {
        '@master-chief/alpaca': '<rootDir>/node_modules/@master-chief/alpaca/dist/cjs/index.cjs',
    },

@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

Apologies, still haven’t had time to circle back to this.

@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:

calvintwr/not@6ce195d

Added. This issue has been an enigma to me for months! Thank you both, I’m glad we have at least a client-side solution to direct people to now. 😃