日本語

Fixing TS5097 for .ts Extension Imports in TypeScript

​​TypeScript​Node.js,Troubleshooting

error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled. is the error tsc reports when an import path ends with .ts, such as import { add } from "./util.ts". The error goes away if you change the path to .js, or add allowImportingTsExtensions or rewriteRelativeImportExtensions to tsconfig.json. Which one you should choose depends on whether you run the JavaScript that tsc emits, or run the .ts files directly.

In Node.js 22, the feature that strips types and runs the code has been enabled by default since 22.18, so you can run a .ts file with node main.ts. In that case the import must end with .ts, and if you type-check the same file with tsc, you get TS5097. Using that situation as an example, I tried three fixes.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, npm 10.9.9, and TypeScript 5.9.3. I also checked the same error with TypeScript 7.0.2, which was the latest version on npm on the day I tested. I only checked the output of tsc and node. I did not test TypeScript 6.x or versions before 5.7, editor displays such as VS Code, bundlers such as Vite, Deno or Bun, or imports from .tsx files.

Choosing a fix based on how you run the code

Which fix you should use depends on how you run the code. The three fixes are summarized below.

How you run the code Fix
Run the .js output of tsc Change the import path to .js
Run the .ts files directly, and use tsc only for type checking Add allowImportingTsExtensions and noEmit
Run the .ts files directly, and also emit .js with tsc Add rewriteRelativeImportExtensions (TypeScript 5.7 or later)

Reproducing the error with a .ts import path

I created an empty folder, added a package.json with "type": "module", and installed TypeScript 5.9.3. The tsconfig.json was as follows.

json
{
  "compilerOptions": {
    "target": "es2022",
    "module": "nodenext",
    "strict": true,
    "rootDir": "src",
    "outDir": "dist"
  },
  "include": ["src"]
}

I wrote one function in src/util.ts, and imported it from src/main.ts using a path with .ts.

ts
// src/util.ts
export function add(a: number, b: number): number {
  return a + b;
}
ts
// src/main.ts
import { add } from "./util.ts";

console.log(add(1, 2));

When I ran npx tsc, it printed the following error and exited with code 2.

console
$ npx tsc
src/main.ts(1,21): error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.

The message means that an import path may end with .ts only when allowImportingTsExtensions is enabled. With --pretty, a ~ mark appeared under the path string "./util.ts" (colors omitted).

console
$ npx tsc --pretty
src/main.ts:1:21 - error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.

1 import { add } from "./util.ts";
                      ~~~~~~~~~~~


Found 1 error in src/main.ts:1

Even with this error, tsc still output dist/main.js and dist/util.js. However, the import in dist/main.js remained "./util.ts". When I ran node dist/main.js, it looked for util.ts, which does not exist in dist, and stopped with exit code 1. (I replaced the path with /path/to/app and omitted the stack trace.)

console
$ cat dist/main.js
import { add } from "./util.ts";
console.log(add(1, 2));
$ node dist/main.js
...
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/path/to/app/dist/util.ts' imported from /path/to/app/dist/main.js

TS5097 warns that the emitted JavaScript will not work this way. The TypeScript 5.0 release notes also state that a path ending in .ts cannot be resolved when the emitted JavaScript files are run.

If you run tsc output, change the path to .js

I changed the import in src/main.ts to "./util.js". There is no util.js file in src, but TypeScript looks for a .ts file with the same name first when it sees a .js path (the file extension substitution section in the TypeScript modules reference).

ts
import { add } from "./util.js";

console.log(add(1, 2));

npx tsc printed nothing and exited with code 0, and node dist/main.js printed 3.

If I wrote "./util" without an extension, this configuration with "module": "nodenext" gave a different error.

console
$ npx tsc
src/main.ts(1,21): error TS2835: Relative import paths need explicit file extensions in ECMAScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean './util.js'?

When I switched the tsconfig.json to "module": "esnext", "moduleResolution": "bundler", and "noEmit": true, npx tsc exited with code 0 with the extensionless "./util". However, when I changed the import back to "./util.ts" in this configuration, TS5097 appeared.

If you run .ts files directly, add allowImportingTsExtensions

I kept the import as "./util.ts" and added only "allowImportingTsExtensions": true to the first tsconfig.json. This time, the error appeared in tsconfig.json.

console
$ npx tsc
tsconfig.json(8,35): error TS5096: Option 'allowImportingTsExtensions' can only be used when either 'noEmit' or 'emitDeclarationOnly' is set.

The allowImportingTsExtensions entry in the TypeScript tsconfig reference says that this option can be used only together with noEmit or emitDeclarationOnly. The TS5096 message in 5.9.3 says the same thing. The option assumes that JavaScript is not emitted, and that resolving .ts imports is left to a bundler or a runtime such as Node.js.

So I removed rootDir and outDir and added "noEmit": true.

json
{
  "compilerOptions": {
    "target": "es2022",
    "module": "nodenext",
    "strict": true,
    "allowImportingTsExtensions": true,
    "noEmit": true
  },
  "include": ["src"]
}

npx tsc exited with code 0, and no dist folder was created. The .ts files can be run directly with Node.js.

console
$ node src/main.ts
3

With this approach, changing the import to "./util.js" does not work. node src/main.ts looked for util.js, which does not exist in src, and stopped with ERR_MODULE_NOT_FOUND. The Node.js documentation on TypeScript support also says that when running .ts files, imports must include the extension, such as ./file.ts, and that allowImportingTsExtensions lets tsc type-check that style.

If you also want emitted output, add rewriteRelativeImportExtensions

If you want to run the .ts files directly but also emit .js files, for example for distribution, use rewriteRelativeImportExtensions, which was added in TypeScript 5.7. I added only this option to the first tsconfig.json.

json
{
  "compilerOptions": {
    "target": "es2022",
    "module": "nodenext",
    "strict": true,
    "rootDir": "src",
    "outDir": "dist",
    "rewriteRelativeImportExtensions": true
  },
  "include": ["src"]
}

With "./util.ts" unchanged, npx tsc exited with code 0, and the import in the emitted dist/main.js had been rewritten to .js.

console
$ cat dist/main.js
import { add } from "./util.js";
console.log(add(1, 2));

Both node dist/main.js and node src/main.ts printed 3. Adding allowImportingTsExtensions as well still gave exit code 0. The TS5096 message in 5.9.3 and the tsconfig reference do not mention this combination. However, with rewriteRelativeImportExtensions, allowImportingTsExtensions worked even without noEmit.

The TypeScript 5.7 release notes say that only relative paths starting with ./ or ../ are rewritten, and only when they end with .ts, .tsx, .mts, or .cts. Paths that start with a package name, and paths that use paths in tsconfig.json, are not rewritten.

TypeScript 7.0.2 shows the same message

TypeScript 7.0.2 also reported TS5097 for the same src/main.ts. The message was word-for-word identical to 5.9.3, and the exit code was also 2.

console
$ npx tsc
src/main.ts(1,21): error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.

When I added allowImportingTsExtensions without noEmit, the TS5096 message changed. The list of valid combinations now included rewriteRelativeImportExtensions.

console
$ npx tsc
tsconfig.json(8,35): error TS5096: Option 'allowImportingTsExtensions' can only be used when one of 'noEmit', 'emitDeclarationOnly', or 'rewriteRelativeImportExtensions' is set.

With 7.0.2, both the configuration with allowImportingTsExtensions and noEmit, and the configuration with rewriteRelativeImportExtensions, exited with code 0. The import in dist/main.js emitted with rewriteRelativeImportExtensions was .js, as it was with 5.9.3. The tsconfig.json in this article includes "rootDir": "src" because, with 7.0.2, without rootDir the build stopped with TS5011, which asks for rootDir to be specified, instead of showing TS5097.

​Posted by 86