207

I have a simple Node/Express app made with Typescript. And eslint give me the error

Missing file extension "ts" for "./lib/env" import/extensions

Here is my .eslintrc file

    {
  "extends": [
    "airbnb",
    "plugin:@typescript-eslint/recommended",
    "prettier",
    "prettier/react",
    "plugin:import/errors",
    "plugin:import/warnings",
    "plugin:import/typescript"
  ],
  "parser": "@typescript-eslint/parser",
  "plugins": ["@typescript-eslint", "prettier", "import"],
  "settings": {
    "import/extensions": [".js", ".jsx", ".ts", ".tsx"],
    "import/parsers": {
      "@typescript-eslint/parser": [".ts", ".tsx"]
    },
    "import/resolver": {
      "typescript": {
        "directory": "./tsconfig.json"
      },
      "node": {
        "extensions": [".js", ".jsx", ".ts", ".tsx"]
      }
    }
  },
  "rules": {
    "@typescript-eslint/indent": [2, 2],
    "no-console": "off",
    "import/no-unresolved": [2, { "commonjs": true, "amd": true }],
    "import/named": 2,
    "import/namespace": 2,
    "import/default": 2,
    "import/export": 2
  }
}

I have installed eslint-plugin-import & eslint-import-resolver-typescript. And I cannot figure out why, I got that error.

2
  • 3
    github.com/benmosher/eslint-plugin-import/issues/1558 Commented Dec 10, 2019 at 11:41
  • 5
    a note for myself: I solve this before. This is problem about airbnb, and can be oneliner solved using "import/extensions": [ "error", "ignorePackages", { "": "never" } ]. Commented Mar 26, 2022 at 14:51

13 Answers 13

379

Add the following code to rules:

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

airbnb ESLint config leads the problem.

Sign up to request clarification or add additional context in comments.

8 Comments

Worked! Thanks. (other readers: make sure you put it under "rules" not "settings"...)
but but what does this do?
@JulianSoto For whom using 'eslint-config-airbnb-base' in there typescript project, there is a rule to 'Ensure consistent use of file extension within the import path', which leads the problem. So we can just overwrite it to fit typescript project.
replace "error" with 2.
This just disables import/extensions entirely. Pointless answer.
|
183

I know this is late, but if you're extending the Airbnb rules you can use eslint-config-airbnb-typescript. Please refer to the project page for the latest instructions. Here's the gist of it as of March 2022:

Install

npm install -D eslint-config-airbnb-typescript

ESLint config file

using React - add 'airbnb-typescript' after 'airbnb'

extends: [
  'airbnb',
+ 'airbnb-typescript'
]

without React - add 'airbnb-typescript/base' after 'airbnb-base'

extends: [
  'airbnb-base',
+ 'airbnb-typescript/base'
]

set parserOptions.project to path of your tsconfig.json

{
  extends: ['airbnb', 'airbnb-typescript'],
+ parserOptions: {
+   project: './tsconfig.json'
+ }
}

NOTE: Prior to August 2021, you would need to also do the following tasks. These instructions are remaining here for historical reference only. You should not need to do this any more.

First uninstall the old one and add the new one:

# uninstall whichever one you're using
npm uninstall eslint-config-airbnb
npm uninstall eslint-config-airbnb-base

# install the typescript one
npm install -D eslint-config-airbnb-typescript

Then update your ESlint config, remove the old Airbnb from the "extends" section and add the new one:

extends: ["airbnb-typescript"]

5 Comments

Thanks! Running npm install -D eslint-config-airbnb-typescript and adding 'airbnb-typescript' fixed for me.
This should be the accepted answer. Thanks for taking the time to update your original answer
npm install eslint-config-airbnb-typescript \ @typescript-eslint/eslint-plugin@^5.13.0 \ @typescript-eslint/parser@^5.0.0 \ --save-dev I can't install the package, got the error: imgur.com/rG8Zkzx
What if I have one eslintrc.js file in a monorepo, where some packages have react and some do not?
@cyrf - monorepos are a whole other game. I generally keep a single eslint file for the entire repo - you should be able to create a single config that works for react and non-react projects. The other approach is to have a one or more "base" configs in your root project and you import/extend those in individual packages in your monorepo.
32

This is a strange solution, but I've added "": "never" and it worked for me (https://github.com/import-js/eslint-plugin-import/issues/1573).

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

6 Comments

Strangely enough this was the only solution that worked for me. Does anyone know why the empty quote marks are needed?
@Hazy I can assume that this is a bug in the webpack of one of the packages/extensions, but I don't know the exact answer. github.com/import-js/eslint-plugin-import/issues/… – here is the guy who found this solution. Seems, he has no answer as well, but he described how he came to it.
in my case, it also helped
If you have this problem in VS Code: "eslint.workingDirectories": [{ "mode": "auto" }] to my settings.json fixed the issue for me, without changing the eslint rules
Why is it working like that?
|
13

Important to add import extension and parser to .eslintrc

    "settings": {
    "import/extensions": [".js", ".jsx", ".ts", ".tsx"],
    "import/parsers": {
      "@typescript-eslint/parser": [".ts", ".tsx"]
    },
    "import/resolver": {
      "node": {
        "extensions": [".js", ".jsx", ".ts", ".tsx"]
      }
    }
  }
},

Further add to rules:

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



enter code here

Comments

5

got this working by bundling all answers I found on internet:

this is the end result for me:

{
  "settings": {
    "import/extensions": [".js", ".jsx", ".ts", ".tsx"],
    "import/parsers": {
      "@typescript-eslint/parser": [".ts", ".tsx"]
    },
    "import/resolver": {
      "node": {
        "extensions": [".js", ".jsx", ".ts", ".tsx"]
      }
    }
  },

  "env": {
    "browser": true,
    "es2021": true
  },

  "extends": ["plugin:react/recommended", "airbnb"],
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "ecmaFeatures": {
      "jsx": true
    },
    "ecmaVersion": 12,
    "sourceType": "module"
  },
  "plugins": ["import", "react", "@typescript-eslint"],
  "rules": {
    "no-console": 1,
    "import/extensions": [
      "error",
      "ignorePackages",
      {
        "js": "never",
        "jsx": "never",
        "ts": "never",
        "tsx": "never",
        "mjs": "never"
      }
    ]
  }
}

im using react native btw. if u're using something different just removing react related should be enough

Comments

3

If you're like me trying to configure babel, legacy Javascript, and new typescript files and attempting to enable extension free imports on *.ts then I was running into this ESLint issue (where I also found below solution):

In addition to the rules config:

"rules": {
   "import/extensions": [
      "error",
      "ignorePackages",
      {
        "js": "never",
        "mjs": "never",
        "jsx": "never",
        "ts": "never",
        "tsx": "never"
      }
   ]
}

You also need an additional settings entry in your .eslintrc.js:

  "settings": {
    "import/extensions": [".js", ".mjs", ".jsx", ".ts", ".tsx"]
  },

Good luck!

Comments

3

Following is a similar error that can be thrown due to '~' in imports:

enter image description here

Missing file extension for "~/path/" eslintimport/extensions\

Solution 1:

This issue can be resolved by configuring tsconfig.json and .eslintrc.js as follows.

tsconfig.json

...
“paths”: {
 “~*”: [“./src/*”],
},
...

.eslintrc.js

...
settings: {
  'import/resolver': {
     alias: {
       map: [['~', './src/']],
       extensions: ['.ts', '.js', '.tsx'],
     },
   },
},
...

Solution 2:

If the error still persists, then probably you must have opened the project in another directory other than the root directory (the directory where tsconfig.json and .eslintrc.js are located). Therefore opening the directory other than the root directory can confuse the eslint in identifying the path correctly, and throw the above error.

Comments

2

I bumped into this because I was doing:

import Ham from "./Ham.tsx"       // bad!

I needed to do:

import Ham from "./Ham"           // good!

Comments

1

The following eslint.config.mjs worked for me:

import eslintPluginReact from 'eslint-plugin-react';
import eslintPluginReactHooks from 'eslint-plugin-react-hooks';
import typescriptEslintPlugin from '@typescript-eslint/eslint-plugin';
import typescriptEslintParser from '@typescript-eslint/parser';
import eslintPluginJsxA11y from 'eslint-plugin-jsx-a11y';
import eslintPluginImport from 'eslint-plugin-import';

export default [
  {
    files: ["src/**/*.{ts,tsx,js,jsx}"],
    ignores: [
      "node_modules",
      "build",
      "dist"
    ],
    languageOptions: {
      parser: typescriptEslintParser,
      ecmaVersion: 2021,
      sourceType: "module",
      globals: {
        React: "readonly",
      },
    },
    plugins: {
      react: eslintPluginReact,
      "react-hooks": eslintPluginReactHooks,
      "@typescript-eslint": typescriptEslintPlugin,
      "jsx-a11y": eslintPluginJsxA11y,
      "import": eslintPluginImport,
    },
    rules: {
      "react/react-in-jsx-scope": "off",  // React 17+ doesn't require React in scope
      "@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_" }],
      "react/jsx-filename-extension": [1, { "extensions": [".tsx"] }],
      // Correct import/extensions rule setup
      "import/extensions": [
        "error",
        "ignorePackages",
        {
          js: "never",
          jsx: "never",
          ts: "never",
          tsx: "never"
        }
      ],
    },
    // Add the resolver to support TypeScript
    settings: {
      "import/resolver": {
        // Use Node resolver for JavaScript files
        node: {
          extensions: [".js", ".jsx", ".ts", ".tsx"]
        },
        // Use TypeScript resolver for TypeScript files
        typescript: {}
      }
    }
  },
];

2 Comments

Best to provide an explanation of why your solution works. Just providing the solution with no explanation doesn't allow others to learn form the question.
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

In my case, I was extending monorepo ESLint configs, and had airbnb-base loading after the package in which I was disabling that rule. So I just had to load it last.

Comments

0

My issue persisted even with all the typescript properties in my .eslintrc.

.eslintrc

"extends": [
    "airbnb-base",
    "airbnb-typescript/base"
  ],
  "parser": "@typescript-eslint/parser",
  "plugins": ["@typescript-eslint"],

I was able to fix the issue by adding this to my webpack config:

webpack.config.json

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

Source: https://smartdevpreneur.com/the-three-easiest-ways-to-fix-the-missing-file-extension-tsx-error/

Comments

0

for eslint@^8, I just put *.js into the overrides.files, such as:

    ...
    overrides: [
        {
            // files: ['*.ts', '*.mts', '*.cts'],
            files: ['*.ts', '*.mts', '*.cts', '*.js'],
            plugins: [
                //
                'eslint-plugin-tsdoc',
                '@typescript-eslint',
            ],
    ...

the lint error massage around the line which import a js file, Missing file extension "ts" for ".... gone.

Comments

0

You could use eslint-import-resolver-typescript plugin to add TypeScript support to an existing eslint-plugin-import plugin in your project by using the following steps:

  1. Install the plugin: npm i -D eslint-import-resolver-typescript

NOTE: Please install eslint-plugin-import and @typescript-eslint/parser if you have not already. These are required for adding .tsx file import support with ESLint and Typescript-ESLint. 2. Add the following value at the "root" of your .eslintrc.json file:

"settings": {
    "import/resolver": {
        "typescript": {
            "alwaysTryTypes": true
        }
    }
}
  1. Turn off the rule for specific extensions:
"rules": {
    "import/extensions": [
        "error",
        "always",
        {
            "pattern": {
                "js": "never",
                "ts": "never",
                "jsx": "never",
                "tsx": "never"
            }
        }
    ]
}

Hopefully that should do the trick.

For further clarification, you can also check out the links below:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.