title: Next.js: Jest fails to process a file inside `node_modules`, giving error `Cannot use import statement outside a module`
body: <p>I'm using Next.js, and I setup Jest using the <a href="https://nextjs.org/docs/testing" rel="nofollow noreferrer">official instructions</a>. However, when I run a test for a component that uses the <a href="https://www.npmjs.com/package/remark" rel="nofollow noreferrer"><code>remark</code></a> module, I get the following error:</p>
  <pre class="lang-js prettyprint-override"><code>Jest encountered an unexpected token
  
  Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
  
  Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
  
  By default &quot;node_modules&quot; folder is ignored by transformers.
  
  Here's what you can do:
    • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
    • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
    • To have some of your &quot;node_modules&quot; files transformed, you can specify a custom &quot;transformIgnorePatterns&quot; in your config.
    • If you need a custom transformation specify a &quot;transform&quot; option in your config.
    • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the &quot;moduleNameMapper&quot; config option.
  
  You'll find more details and examples of these config options in the docs:
  https://jestjs.io/docs/configuration
  For information about custom transformations, see:
  https://jestjs.io/docs/code-transformation
  
  Details:
  
  /Users/mk/Code/github/hollowverse/hollowverse/node_modules/remark/index.js:1
  ({&quot;Object.&lt;anonymous&gt;&quot;:function(module,exports,require,__dirname,__filename,jest){import {unified} from 'unified'
                                                                                    ^^^^^^
  
  SyntaxError: Cannot use import statement outside a module
  
    1 | import matter from 'gray-matter';
  &gt; 2 | import { remark } from 'remark';
      |                                ^
    3 | import remarkHtml from 'remark-html';
    4 | import { sanityClient } from '~/lib/components/sanityio';
    5 | import { groqRelatedPeople } from '~/lib/[celeb]/getStaticProps/groqRelatedPeople';
  
    at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
    at Object.&lt;anonymous&gt; (lib/[celeb]/getStaticProps/getParsedOldContent.ts:2:32)
  </code></pre>
  <p>My <code>jest.config.js</code> is the following</p>
  <pre class="lang-js prettyprint-override"><code>// jest.config.js
  const tsconfig = require('./tsconfig.json');
  const moduleNameMapper = require('tsconfig-paths-jest')(tsconfig);
  const nextJest = require('next/jest');
  
  const createJestConfig = nextJest({
    // Provide the path to your Next.js app to load next.config.js and .env files in your test environment
    dir: './',
  });
  
  // Add any custom config to be passed to Jest
  const customJestConfig = {
    // Add more setup options before each test is run
    // setupFilesAfterEnv: ['&lt;rootDir&gt;/jest.setup.js'],
    // if using TypeScript with a baseUrl set to the root directory then you need the below for alias' to work
    moduleDirectories: ['node_modules', '&lt;rootDir&gt;/'],
    testEnvironment: 'jest-environment-jsdom',
    moduleNameMapper: {
      ...moduleNameMapper,
      '^lodash-es$': 'lodash',
    },
  };
  
  // createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
  module.exports = createJestConfig(customJestConfig);
  </code></pre>
  <p>Anyone know how to solve this problem?</p>