Using eslint with typescript - Unable to resolve path to module

225,694

Solution 1

You can set the ESLint module import resolution by adding this snippet to your .eslintrc.json configuration file:

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

More informations about resolvers: https://github.com/benmosher/eslint-plugin-import#resolvers.

Solution 2

I had the same problem and I was only able to fix it by adding the typescript plugin to .eslintrc, using the extends option in .eslintrc

  extends: [
    "plugin:import/errors",
    "plugin:import/warnings",
    "plugin:import/typescript",
  ],

Solution 3

This does it for me:

.eslintrc.js

{
    ...
    settings: {
        ...
        'import/resolver': {
            node: {
                extensions: ['.js', '.jsx', '.ts', '.tsx'],
                moduleDirectory: ['node_modules', 'src/'],
            },
        },
    }
}

Solution 4

This was the only solution that worked for me.

First, you need to install this package:

yarn add -D eslint-import-resolver-typescript

Then add this to your .eslintrc file so it can load the alias settings from your tsconfig.json into ESLint:

{
  "settings": {
    "import/resolver": {
      "typescript": {}
    },
  },
}

Solution 5

When using "eslint": "6.8.0" with "typescript": "3.8.3" besides adding the following to .eslintrc:

"settings": {
  "import/resolver": {
    "node": {
      "paths": ["src"],
      "extensions": [".js", ".jsx", ".ts", ".tsx"],
    }
  },
}

You also need to add this line to tsconfig.json under compilerOptions:

"compilerOptions": {
  ...
  // Base directory to resolve non-relative module names.
  "baseUrl": "src",
  ...
}
Share:
225,694

Related videos on Youtube

Maxime Dupré
Author by

Maxime Dupré

Updated on April 22, 2022

Comments

  • Maxime Dupré
    Maxime Dupré 8 months

    I have this import in my file app.spec.ts:

    import app from './app';
    

    Which causes this Typescript error

    2:17  error  Unable to resolve path to module './app'  import/no-unresolved
    

    ./app.ts does exist, but I have not compiled the .ts file into a .js file. As soon as I compile the .ts file to a .js, the error goes away.

    However, since eslint is supposed to work with typescript, it should resolve modules with the .ts and not the .js.

    I've also added the typescript information in my eslint config file:

    "parser": "@typescript-eslint/parser",
    "parserOptions": {
        "project": "./tsconfig.json"
    }
    

    How can I config eslint in such a way that it tries to resolve modules with the .ts and not the .js?

    Cheers!

    EDIT #1

    Content of app.ts:

    import bodyParser from 'body-parser';
    import express from 'express';
    import graphqlHTTP from 'express-graphql';
    import { buildSchema } from 'graphql';
    const app = express();
    const schema = buildSchema(`
        type Query {
            hello: String
        }
    `);
    const root = { hello: () => 'Hello world!' };
    app.use(bodyParser());
    app.use('/graphql', graphqlHTTP({
        schema,
        rootValue: root,
        graphiql: true,
    }));
    export default app;
    
    • Jasmonate almost 4 years
      Have you added "plugins": ["@typescript-eslint"]? Docs github.com/typescript-eslint/typescript-eslint/tree/master/…
    • Maxime Dupré
      Maxime Dupré almost 4 years
      Hey @Jasmonate, thanks for the answer! So I just added this config, and while I can now do some Typescript-specific linting, it does not solve my problem. I still get Unable to resolve path to module './app'
    • Abulafia almost 4 years
      Have you tried adding this into your .eslintrc? settings: { 'import/resolver': 'webpack' } (I'm assuming the code builds OK. If not, you will need to tell webpack to resolve .ts/.tsx files as well, which means adding something like: ``` module.exports = { resolve: { extensions: ['.js', '.ts'] } }; ``` or whatever file extensions you want to be able to import! )
    • Abulafia almost 4 years
      The above comment is incomplete, I'm afraid! I ran out of editing time having pressed enter too soon! The module.exports = { resolve: { extensions: ['.js', '.ts'] } }; bit should be in your webpack config!
    • Eastrall almost 4 years
      Can you add the content of your app.ts file ?
    • Maxime Dupré
      Maxime Dupré almost 4 years
      Hey @Abulafia! Unfortunately, I am not using webpack...
    • Maxime Dupré
      Maxime Dupré almost 4 years
      @Eastrall I edited the question with the content of app.ts :)
    • KyleMit
      KyleMit over 1 year
      If all else fails, you can try deleting node_modules, reinstalling, and restarting TS server
  • Izhaki
    Izhaki over 3 years
    I believe the other answer is the right solution for this.
  • Epoch almost 3 years
    This is what worked for me; even including the "plugin:import/*" extensions didn't do anything. The moduleDirectory line pointing to 'src/' was the key to get this working on my end.
  • Tevon Strand-Brown
    Tevon Strand-Brown almost 3 years
    This worked for me as well. Note that HAD to add the "moduleDirectory" key.
  • Robin Zimmermann
    Robin Zimmermann over 2 years
    I needed both the "settings" and the "rules", as described in this answer. Thanks!
  • Yogi
    Yogi over 2 years
    barking!! Hats off👏🏻
  • Yan Yankowski over 2 years
    Adding the moduleDirectory: ['node_modules', 'myModules/'] indeed helped. I've seen a partial version of this solution without 'moduleDirectory' property and it didn't work. This one did the trick.
  • underscore
    underscore over 2 years
    works after adding moduleDirectory: ['node_modules', 'src/']
  • Tomas Vancoillie
    Tomas Vancoillie over 2 years
    Greatly appreciated to add the baseUrl options, saved my day
  • charlax
    charlax over 2 years
    Thanks! Adding "plugin:import/typescript" is what fixed it for me.
  • httpete about 2 years
    In my case I had to add .d.ts to the list- the package csstypes only contains an index.d.ts: "settings": { "import/resolver": { "node": { "paths": [ "src" ], "extensions": [ ".js", ".jsx", ".d.ts", ".ts", ".tsx" ] } } }
  • Ryan
    Ryan almost 2 years
    This seemed to solve the Unable to resolve path to module error, but now I'm still getting a bunch of Missing file extension "tsx" for [...]
  • Ryan
    Ryan almost 2 years
    Ahh, 'import/extensions': 0, helped: stackoverflow.com/questions/56064609/…
  • brianyang
    brianyang almost 2 years
    this is the only one that worked for me, just add this line and save. the error went right away!
  • MasterWil
    MasterWil almost 2 years
    Note that you need to 1. place "plugin:import/typescript" after "airbnb-base". 2. Remember to add "import" in plugins section.
  • Evgeniya Manolova
    Evgeniya Manolova almost 2 years
    This can also solve the case when the eslint configuration is shared between several projects and therefore located in a top-level folder. Listing all projects' paths in the paths property solves the eslint error.
  • semkeijsper over 1 year
    This is the only one that worked for me when using a monorepo and a path alias to a different package.
  • Jalal
    Jalal over 1 year
    @Semx11, Same here!
  • Volodymyr Bobyr over 1 year
    now i get Missing file extension "ts" for <file>
  • Volodymyr Bobyr over 1 year
  • c0dezer019
    c0dezer019 over 1 year
    It's one of those days! I had this problem, used this solution, and it didn't work. Tried it again several hours later and then it works. Weird!
  • Diego Alberto Zapata Häntsch over 1 year
    this doesn't solve the problem only it turn off the rule
  • Sandeep vashisth
    Sandeep vashisth about 1 year
    This worked for me using the same config in .eslintrc.json in the typescript react project.
  • Kristóf Dombi about 1 year
    For some reason by just having "plugin:import/typescript", did not solved this problem for me. The solution was to use this: github.com/alexgorbatchev/eslint-import-resolver-typescript
  • June about 1 year
    it's only solution for me thx ;)
  • FR073N
    FR073N about 1 year
    ` "typescript": {} ` did the trick for me.
  • ofundefined
    ofundefined 12 months
    Indeed. My "babel-module" had only {}
  • Nicolas
    Nicolas 11 months
    That worked for me!! Thank you :)
  • julianYaman 11 months
    Thanks for this solution, the other ones didn't really help in my use case
  • Paul P
    Paul P 10 months
    This worked for me with Quasar v1 (quasar create <project> --branch v1) and airbnb-base ESLint config.
  • SalahAdDin
    SalahAdDin 9 months
    It didn't work for me.
  • Kristi Jorgji
    Kristi Jorgji 9 months
    this works with nextjs, thanks
  • VimNing
    VimNing 9 months
    Hey, hey you, 2022 readers! Take a look at my one-line solution: stackoverflow.com/a/71629047/5290519
  • shamaseen
    shamaseen 9 months
    this worked for me, one thing to note is if you are using an IDE and the error is shown by the IDE, make sure that you check it with the command line, cause your IDE may be misconfigured.
  • Oleksandr Danylchenko
    Oleksandr Danylchenko 8 months
    Thanks! Missing eslint-import-resolver-typescript was the issue!
  • Nivethan
    Nivethan 7 months
    this solved the path alias issue in a react-native cli project
  • German Attanasio
    German Attanasio 7 months
    @pedro_A you are the real MVP!
  • Hazy
    Hazy 7 months
    Add my voice to also say this is the only solution that also worked for me after trying pretty much everything else that I came across to solve this problem
  • Chunky Chunk
    Chunky Chunk 6 months
    ignoring a problem and fixing a problem are very different solutions.
  • VimNing
    VimNing 6 months
    @ChunkyChunk: But the accepted one didn't work during my writing.
  • Juanma
    Juanma 6 months
    this is what did the trick for me. I was missing this step. Thanks