Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

webpack does not find your .ts and .tsx files

ErrorModule not found

Short answer

Webpack tries only .js, .json and .wasm unless told otherwise, and it needs a loader before it can read TypeScript. Add .ts and .tsx to resolve.extensions, add a rule that sends them to ts-loader, and use resolve.extensionAlias if your imports end in .js while the real files are .ts.

  • Applies to webpack 5.105, ts-loader, TypeScript 5.7, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Run a development build and read the first error: it names the import and the folder webpack searched. Then open the config and look at resolve.extensions and module.rules.

    npx webpack --mode development
    grep -n -A3 "extensions\|ts-loader" webpack.config.*
  2. 2Match what you see, then jump to its fix

    1. 1Import written without extension, such as import App from './App', and the file is App.tsx.resolve.extensions does not contain .ts and .tsxFix
    2. 2The build fails inside a .ts or .tsx file instead of at the import line.No rule sends .ts and .tsx files to a loaderFix
    3. 3Source such as import { f } from './helpers.js' where only helpers.ts exists.Imports end in .js but the real files are .tsFix

What does the error look like?

  • Module not found
  • Can't resolve

Cause 1 of 3

resolve.extensions does not contain .ts and .tsx

1Check

The default list is .js, .json and .wasm, and setting the option replaces that list.

grep -n -A3 "extensions" webpack.config.*

2Fix

List the TypeScript extensions first. The token three dots keeps the defaults.

resolve: {
  extensions: ['.tsx', '.ts', '...'],
},

3Verify

The import resolves; the order matters if two files share a name.

npx webpack --mode development

Confirmed by webpack.js.org

Did this fix it?

Cause 2 of 3

No rule sends .ts and .tsx files to a loader

1Check

The TypeScript guide sets up both the extension list and a module rule that sends .ts and .tsx files to ts-loader. Check that the rule exists and its test pattern matches both extensions.

grep -n -B1 -A4 "ts-loader" webpack.config.*

2Fix

Install typescript and ts-loader and add the rule from the guide.

npm install --save-dev typescript ts-loader

// webpack.config.js
module: {
  rules: [
    { test: /\.tsx?$/, use: 'ts-loader', exclude: /node_modules/ },
  ],
},

3Verify

The build compiles the file and the bundle is created.

npx webpack --mode development

Confirmed by webpack.js.org

Did this fix it?

Cause 3 of 3

Imports end in .js but the real files are .ts

1Check

ESM-style TypeScript often writes the .js extension. Webpack looks for helpers.js on disk and does not find it.

ls src | grep helpers

2Fix

Map the .js extension to the TypeScript extensions with resolve.extensionAlias.

resolve: {
  extensionAlias: {
    '.js': ['.ts', '.js'],
    '.mjs': ['.mts', '.mjs'],
  },
},

3Verify

The import now resolves to the .ts file.

npx webpack --mode development

Confirmed by webpack.js.org

Did this fix it?

How do I stop it coming back?

  • Copy the resolve and rule blocks from the official TypeScript guide when you start a project.
  • Keep extensions and extensionAlias in one place so editor and bundler agree.
  • Run the production build in CI as well as the dev server.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

tsconfig rejects allowImportingTsExtensions

An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly

allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.