title: How to set up Jest w/ESM to recognize non-cjs modules in node_modules
body: <p>Have got a successful jest/esm setup, however occasionally a module is released that specifies both a <code>main</code> key (for commonjs) and a <code>module</code> key (for ESM) in its <code>package.json</code>. This leads to jest errors, for example with the <a href="https://www.npmjs.com/package/uuid" rel="noreferrer">uuid module</a>:</p>
  <pre><code>/repo/path/node_modules/uuid/dist/esm-browser/index.js:1
  ({&quot;Object.&lt;anonymous&gt;&quot;:function(module,exports,require,__dirname,__filename,jest){export { default as v1 } from './v1.js';
                                                                                        ^^^^^^
  SyntaxError: Unexpected token 'export'
  
  </code></pre>
  <p>I can see that <code>dist/esm-browser/index.js</code> is the file specified by the <code>module</code> key in <code>package.json</code>.</p>
  <p>How can Jest w/ESM be configured to handle these cases, where stuff in node_modules is ESM?</p>
  <p>Jest config:</p>
  <pre><code>{
      &quot;resetMocks&quot;: true,
      &quot;testEnvironment&quot;: &quot;jsdom&quot;,
      &quot;testMatch&quot;: [
        &quot;**/src/**/*.(spec|test).[tj]s?(x)&quot;
      ],
      &quot;preset&quot;: &quot;ts-jest/presets/default-esm&quot;,
      &quot;extensionsToTreatAsEsm&quot;: [
        &quot;.ts&quot;,
        &quot;.tsx&quot;
      ],
      &quot;globals&quot;: {
        &quot;ts-jest&quot;: {
          &quot;useESM&quot;: true
        }
      },
      &quot;globalSetup&quot;: &quot;&lt;rootDir&gt;/jest/setup.cjs&quot;,
      &quot;globalTeardown&quot;: &quot;&lt;rootDir&gt;/jest/teardown.cjs&quot;,
      &quot;watchPathIgnorePatterns&quot;: [
        &quot;&lt;rootDir&gt;/.tmp&quot;
      ],
      &quot;moduleNameMapper&quot;: {
        &quot;^~/(.*)$&quot;: &quot;&lt;rootDir&gt;/src/$1&quot;,
        &quot;^~components/(.*)$&quot;: &quot;&lt;rootDir&gt;/src/components/$1&quot;,
        &quot;^~util/(.*)$&quot;: &quot;&lt;rootDir&gt;/src/util/$1&quot;,
        &quot;^~types/(.*)$&quot;: &quot;&lt;rootDir&gt;/src/types/$1&quot;
      }
    }
  </code></pre>