title: Unable to run jest with typescript - "Cannot find module 'typescript'"
body: <p>I'm trying to run jest with typescript, but I'm getting following error. The project runs fine in webpack and with ts-node. For some reason I can't get it to work with jest.</p>
  <pre><code>FAIL  src/__tests__/classes/Utils.spec.ts
  ● Test suite failed to run
  
  Cannot find module 'typescript'
  
  Require stack:
  - /Users/myuser/repos/project/node_modules/ts-jest/dist/config/config-set.js
  - /Users/myuser/repos/project/node_modules/ts-jest/dist/ts-jest-transformer.js
  - /Users/myuser/repos/project/node_modules/ts-jest/dist/index.js
  - /Users/myuser/repos/project/node_modules/@jest/transform/build/ScriptTransformer.js
  - /Users/myuser/repos/project/node_modules/@jest/transform/build/index.js
  - /Users/myuser/repos/project/node_modules/jest-runtime/build/index.js
  - /Users/myuser/repos/project/node_modules/@jest/core/build/cli/index.js
  - /Users/myuser/repos/project/node_modules/@jest/core/build/jest.js
  - /Users/myuser/repos/project/node_modules/jest/node_modules/jest-cli/build/cli/index.js
  - /Users/myuser/repos/project/node_modules/jest/node_modules/jest-cli/bin/jest.js
  - /Users/myuser/repos/project/node_modules/jest/bin/jest.js
  - /usr/local/lib/node_modules/jest/node_modules/import-local/index.js
  - /usr/local/lib/node_modules/jest/bin/jest.js
  </code></pre>
  <h1>jest.config.js</h1>
  <p>This is the configuration. I've tried many variations on the roots property and moduleNameMapper, but the error message is exactly the same no matter what I change in the config.</p>
  <pre class="lang-js prettyprint-override"><code>module.exports = {
    preset: 'ts-jest',
    testEnvironment: 'node',
    roots: ['&lt;rootDir&gt;/src'],
    transform: {
      '^.+\\.tsx?$': 'ts-jest',
    },
    testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
    moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
    moduleDirectories: ['node_modules', 'src'],
    moduleNameMapper: {
      '^src/(.*)$': '&lt;rootDir&gt;/src/$1',
    }
  };
  </code></pre>
  <h1>tsconfig.json</h1>
  <pre class="lang-js prettyprint-override"><code>{
      &quot;compilerOptions&quot;: {
          &quot;baseUrl&quot;: &quot;./&quot;,
          &quot;paths&quot;: {
              &quot;src/*&quot;: [&quot;src/*&quot;],
              &quot;tests/*&quot;: [&quot;__tests__/*&quot;]
          },
          &quot;target&quot;: &quot;es6&quot;,
          &quot;module&quot;: &quot;commonjs&quot;,
          &quot;esModuleInterop&quot;: true,
          &quot;moduleResolution&quot;: &quot;node&quot;,
          &quot;emitDecoratorMetadata&quot;: true,
          &quot;experimentalDecorators&quot;: true,
          &quot;sourceMap&quot;: true,
          &quot;inlineSourceMap&quot;: false,
          &quot;outDir&quot;: &quot;dist&quot;,
          &quot;lib&quot;: [&quot;es6&quot;, &quot;dom&quot;, &quot;esnext&quot;, &quot;esnext.asynciterable&quot;],
          &quot;typeRoots&quot;: [&quot;node_modules/@types&quot;, &quot;src/typings&quot;]
      },
      &quot;awesomeTypescriptLoaderOptions&quot;: {
          &quot;useBabel&quot;: true,
          &quot;useCache&quot;: true,
          &quot;useTranspileModule&quot;: true
      },
      &quot;include&quot;: [&quot;src/**/*.ts&quot;],
      &quot;exclude&quot;: [&quot;./node_modules/*&quot;]
  }
  </code></pre>
  <h1>Utils.spec.js</h1>
  <pre class="lang-js prettyprint-override"><code>import { Utils } from 'src/utils/classes/Utils';
  
  test('Generates a 6 digit number'), () =&gt; {
      expect(Utils.randomNumberGenerator(6).toHaveLength(6))
  });
  </code></pre>
  <h1>Utils.ts</h1>
  <pre class="lang-js prettyprint-override"><code>export class Utils {
      public static randomNumberGenerator(length: number): number {
          const baseNumber: number = Number(1 + '0'.repeat(length - 1));
          const randomMultiplier: number = Math.floor(Math.random() * Number('9' + '0'.repeat(length - 1)));
          const randomToken: number = baseNumber + randomMultiplier;
          return randomToken;
      }
  }
  </code></pre>