Maaz Zindani

Loading the index…

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

TypeScript rejects an import that ends in .ts

ErrorAn import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.

Short answer

TypeScript refuses a .ts import because it never rewrites the specifier, so the emitted JavaScript would point at a file that does not exist. Pick one way out: allow it with allowImportingTsExtensions plus noEmit when a bundler or runtime handles the code, let tsc rewrite it with rewriteRelativeImportExtensions, or drop the extension.

  • Applies to TypeScript 5.7, Node.js 22, webpack 5
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find out whether tsc writes JavaScript for this project and which TypeScript version it is. The answer decides which fix below is allowed.

    npx tsc --version
    grep -nE "noEmit|emitDeclarationOnly|allowImportingTsExtensions|rewriteRelativeImportExtensions|moduleResolution" tsconfig.json
  2. 2Match what you see, then jump to its fix

    1. 1tsc is only used for type checking (noEmit), and webpack, Vite, tsx or Deno runs the code.A bundler or TypeScript runtime consumes the files, so no JavaScript needs to come from tscFix
    2. 2You build with tsc into dist and want to write imports like ./math.ts in the source.tsc emits the JavaScript that you ship, so the .ts path would reach runtimeFix
    3. 3The project is bundled with webpack, Vite or similar and nothing requires the extension.The extension was never needed under a bundler-style resolutionFix

What does the error look like?

  • An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.

Cause 1 of 3

A bundler or TypeScript runtime consumes the files, so no JavaScript needs to come from tsc

1Check

If your build does not use tsc output, the rule that protects emitted JavaScript does not matter for you.

grep -nE "noEmit|emitDeclarationOnly" tsconfig.json

2Fix

Turn on allowImportingTsExtensions together with noEmit. The flag is only allowed when noEmit or emitDeclarationOnly is set.

{
  "compilerOptions": {
    "allowImportingTsExtensions": true,
    "noEmit": true
  }
}

3Verify

Type checking passes and the import keeps its .ts extension.

npx tsc -p tsconfig.json

Confirmed by typescriptlang.org

Did this fix it?

Cause 2 of 3

tsc emits the JavaScript that you ship, so the .ts path would reach runtime

1Check

allowImportingTsExtensions would be refused here because the output would contain .ts paths. TypeScript 5.7 added an option that rewrites them.

npx tsc --version

2Fix

Enable rewriteRelativeImportExtensions. Relative specifiers ending in .ts, .tsx, .mts or .cts are rewritten to .js, .jsx, .mjs or .cjs in the output. Bare package paths, aliased paths and dynamic imports with computed paths are not rewritten.

{
  "compilerOptions": {
    "rewriteRelativeImportExtensions": true
  }
}

3Verify

Build, then open the emitted file and check that the import now ends in .js.

npx tsc -p tsconfig.json
grep -rn "from \"./" dist | head

Confirmed by typescriptlang.org

Did this fix it?

Cause 3 of 3

The extension was never needed under a bundler-style resolution

1Check

With moduleResolution set to bundler, extensionless relative imports work the way they do in CommonJS.

grep -n moduleResolution tsconfig.json

2Fix

Use the bundler resolution and remove the extension from the import. Webpack finds the file through resolve.extensions.

// tsconfig.json: "moduleResolution": "bundler"
// before
import { add } from "./math.ts";
// after
import { add } from "./math";

3Verify

tsc passes without any new compiler flag.

npx tsc --noEmit -p tsconfig.json

Confirmed by typescriptlang.org

Did this fix it?

How do I stop it coming back?

  • Decide once whether tsc emits code or only checks types, and set noEmit in the same commit.
  • Use moduleResolution bundler for apps and node16 or nodenext for libraries that Node.js loads directly.
  • Run tsc --noEmit in CI so a stray .ts import fails before a build.

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.