Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

ESLint asks for a .ts extension on imports that work fine

ErrorMissing file extension "ts"

Short answer

The import/extensions rule demands or forbids file extensions per file type. When ts and tsx are not in its list, a config such as always makes it ask for them on every import. Add ts and tsx with never for plain bundler or ts-node style code, and keep always only if you run native ESM.

  • Applies to eslint-plugin-import, ESLint 9, TypeScript 5.x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find the import/extensions setting that is active, including the one inherited from a shared config.

    npx eslint --print-config src/index.ts | grep -n "import/extensions" -A8
  2. 2Match what you see, then jump to its fix

    1. 1The rule is set to always or ignorePackages with no entry for tsts and tsx are missing from the per-extension mapFix
    2. 2tsconfig uses module nodenext or node16, and the runtime import fails without an extensionThe project runs as native ESM, where extensions are requiredFix

What does the error look like?

  • Missing file extension "ts"
  • import/extensions

Cause 1 of 2

ts and tsx are missing from the per-extension map

1Check

The rule accepts a string as the default plus an object that overrides single extensions. Without a ts entry, the default applies to ts files.

grep -rn "import/extensions" eslint.config.* .eslintrc* package.json 2>/dev/null

2Fix

Keep your default, and declare ts and tsx as never so extensionless imports pass.

"import/extensions": [
  "error",
  "ignorePackages",
  { "ts": "never", "tsx": "never" }
]

3Verify

Lint the file that failed.

npx eslint src/index.ts

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 2

The project runs as native ESM, where extensions are required

1Check

Node.js does not support extensionless relative paths in import statements. TypeScript expects the written extension to be .js and maps it to the .ts file.

grep -n "\"module\"\|moduleResolution" tsconfig.json

2Fix

Do not turn the rule off. Write imports with the .js extension and set the rule to always for js.

import { slugify } from './utils.js';

"import/extensions": ["error", "ignorePackages", { "js": "always", "ts": "never" }]

3Verify

Lint and run the compiled output.

npx eslint src
npx tsc && node dist/index.js

Confirmed by typescriptlang.org

Did this fix it?

How do I stop it coming back?

  • Decide once whether the project uses extensionless or .js imports, and set the rule to match.
  • Check shared configs with --print-config after upgrading them.
  • Let tsc, not ESLint, be the authority on whether an import path resolves.

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

Back end

cdk synth says --app is required

--app is required either in command-line, in cdk.json or in ~/.cdk.json

The CDK CLI needs a command that runs your app and finds it in three places: the --app option, cdk.json in the project folder, or cdk.json in your home folder.

3 causes, each with a fix

Back end

Mocha or Node cannot find ts-node/register

Error: Cannot find module 'ts-node/register'

Node resolves ts-node/register from your project's node_modules, so the package must be installed there, next to typescript.

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.