title: Cannot use import statement outside a module in /node_modules
body: <p>I'm in the process of migrating some node packages which has become quite a headache.</p>
  <p>Currently this is what my package.json looks like:</p>
  <pre><code>{
    //These are the updated dependencies and Jest configurations
    &quot;dependencies&quot;: {
      &quot;@google-cloud/logging-winston&quot;: &quot;^4.0.2&quot;,
      &quot;@google-cloud/secret-manager&quot;: &quot;^3.2.3&quot;,
      &quot;@google-cloud/storage&quot;: &quot;^5.7.0&quot;,
      &quot;@nestjs/common&quot;: &quot;^8.2.3&quot;,
      &quot;@nestjs/config&quot;: &quot;^1.1.5&quot;,
      &quot;@nestjs/core&quot;: &quot;^8.2.3&quot;,
      &quot;@nestjs/jwt&quot;: &quot;^8.0.0&quot;,
      &quot;@nestjs/mongoose&quot;: &quot;^9.0.1&quot;,
      &quot;@nestjs/passport&quot;: &quot;^8.0.1&quot;,
      &quot;@nestjs/platform-express&quot;: &quot;^8.2.3&quot;,
      &quot;bcrypt&quot;: &quot;^5.0.1&quot;,
      &quot;class-transformer&quot;: &quot;^0.5.1&quot;,
      &quot;class-validator&quot;: &quot;^0.13.2&quot;,
      &quot;dayjs&quot;: &quot;^1.9.7&quot;,
      &quot;helmet&quot;: &quot;^4.2.0&quot;,
      &quot;mongoose&quot;: &quot;^6.0.14&quot;,
      &quot;nanoid&quot;: &quot;^3.1.20&quot;,
      &quot;nest-winston&quot;: &quot;^1.4.0&quot;,
      &quot;p-all&quot;: &quot;^3.0.0&quot;,
      &quot;passport&quot;: &quot;^0.4.1&quot;,
      &quot;passport-jwt&quot;: &quot;^4.0.0&quot;,
      &quot;passport-local&quot;: &quot;^1.0.0&quot;,
      &quot;reflect-metadata&quot;: &quot;^0.1.13&quot;,
      &quot;rxjs&quot;: &quot;^7.4.0&quot;,
      &quot;winston&quot;: &quot;^3.3.3&quot;
    },
    &quot;devDependencies&quot;: {
      &quot;@nestjs/cli&quot;: &quot;^8.1.5&quot;,
      &quot;@nestjs/schematics&quot;: &quot;^8.0.5&quot;,
      &quot;@nestjs/testing&quot;: &quot;^8.2.3&quot;,
      &quot;@types/bcrypt&quot;: &quot;^5.0.0&quot;,
      &quot;@types/express&quot;: &quot;^4.17.13&quot;,
      &quot;@types/jest&quot;: &quot;^27.0.3&quot;,
      &quot;@types/multer&quot;: &quot;^1.4.7&quot;,
      &quot;@types/node&quot;: &quot;^16.11.11&quot;,
      &quot;@types/passport-jwt&quot;: &quot;^3.0.3&quot;,
      &quot;@types/passport-local&quot;: &quot;^1.0.33&quot;,
      &quot;@types/supertest&quot;: &quot;^2.0.10&quot;,
      &quot;@typescript-eslint/eslint-plugin&quot;: &quot;^4.6.1&quot;,
      &quot;@typescript-eslint/parser&quot;: &quot;^4.6.1&quot;,
      &quot;eslint&quot;: &quot;^7.12.1&quot;,
      &quot;eslint-config-prettier&quot;: &quot;^8.3.0&quot;,
      &quot;eslint-plugin-prettier&quot;: &quot;^3.4.0&quot;,
      &quot;jest&quot;: &quot;^27.4.3&quot;,
      &quot;jest-mock&quot;: &quot;^27.4.2&quot;,
      &quot;prettier&quot;: &quot;^2.2.1&quot;,
      &quot;supertest&quot;: &quot;^6.1.3&quot;,
      &quot;ts-jest&quot;: &quot;^27.1.0&quot;,
      &quot;ts-loader&quot;: &quot;^9.2.6&quot;,
      &quot;ts-node&quot;: &quot;^10.4.0&quot;,
      &quot;tsconfig-paths&quot;: &quot;^3.9.0&quot;,
      &quot;typescript&quot;: &quot;^4.5.2&quot;
    },
    &quot;jest&quot;: {
      &quot;moduleFileExtensions&quot;: [
        &quot;js&quot;,
        &quot;json&quot;,
        &quot;ts&quot;
      ],
      &quot;rootDir&quot;: &quot;src&quot;,
      &quot;testRegex&quot;: &quot;.*\\.spec\\.ts$&quot;,
      &quot;transform&quot;: {
        &quot;^.+\\.(t|j)s$&quot;: &quot;ts-jest&quot;
      },
      &quot;moduleDirectories&quot;: [
        &quot;node_modules&quot;,
        &quot;src&quot;
      ],
      &quot;collectCoverageFrom&quot;: [
        &quot;**/*.(t|j)s&quot;
      ],
      &quot;coveragePathIgnorePatterns&quot;: [
        &quot;.module.ts&quot;,
        &quot;main.ts&quot;,
        &quot;env-var-names.ts&quot;,
        &quot;.d.ts&quot;,
        &quot;.types.ts&quot;,
        &quot;.e2e-spec.ts&quot;
      ],
      &quot;coverageDirectory&quot;: &quot;../coverage&quot;,
      &quot;testEnvironment&quot;: &quot;node&quot;,
      &quot;resetMocks&quot;: true,
      &quot;resetModules&quot;: true
    }
  }
  </code></pre>
  <p>When I try to run Jest, I get the following error relating to <code>/node_modules</code></p>
  <pre><code>● Test suite failed to run
  
      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:
  
      /home/{username}/{project_directory}/node_modules/mongodb/src/bson.ts:1
      ({&quot;Object.&lt;anonymous&gt;&quot;:function(module,exports,require,__dirname,__filename,jest){import type {
                                                                                        ^^^^^^
  
      SyntaxError: Cannot use import statement outside a module
  
        at Runtime.createScriptFromCode (../node_modules/jest-runtime/build/index.js:1728:14)
        at Object.&lt;anonymous&gt; (../node_modules/mongodb/src/bson.ts:8:12)
  </code></pre>
  <p>So within our team, we've been able to fix it by changing a part of the Jest configuration from:</p>
  <pre><code>&quot;moduleDirectories&quot;: [
    &quot;node_modules&quot;,
    &quot;src&quot;
  ],
  </code></pre>
  <p>To so:</p>
  <pre><code>&quot;moduleDirectories&quot;: [
    &quot;node_modules&quot;,
  ],
  </code></pre>
  <p>We also had to change our import paths back to relative. This gets rid of the error and is working fine, however we are a bit stumped as to why this works? Does anyone have any insight they could share?</p>
  <p>Thank you!</p>