Eslint-plugin-import: no-internal-modules is not working with CRA+Typescript setup

Created on 9 Aug 2020  路  5Comments  路  Source: benmosher/eslint-plugin-import

I have a directory structure that goes like this:

|- src/
    | components/
        |- MyComponent/
            | -- MyComponent.tsx
            | -- index.ts

And a component that's doing the following:

import MyComponent from 'components/MyComponent/MyComponent'

This is my .eslintrc.json

{
  "extends": ["react-app"],
  "rules": {
    "import/no-internal-modules": "error",
  }
}

However when I run Eslint, it is not throwing an error.

Steps to reproduce

  1. yarn create react-app --template typescript
  2. Add EXTEND_ESLINT=true to .env
  3. Create .eslintrc.json with the contents above
  4. Create the file structure above
  5. Add "baseUrl": "src" to tsconfig.json (to support absolute imports)
  6. Run linter.

Expected result

Unless I misunderstood the plugin, it seems to me it should have thrown an error because I attempted to import the file, rather than the directory (since it has an index file)

I'm using Create React App with Template Typescript.

question resolver typescript

Most helpful comment

Fixed by doing the following:

  1. installed the plugin above by running yarn add eslint-import-resolver-typescript
  2. Added this to my .eslintrc.json
{
  // ......

  "settings": {
    "import/resolver": {
      "typescript": {
        "project": "."
      }
    }
  }
}

All 5 comments

Adding src to tsconfig doesn't change anything unless you're using https://www.npmjs.com/package/eslint-import-resolver-typescript in your eslint settings.

@ljharb Thanks for replying.

I'm not sure I follow, CRA already handles typescript files and resolves absolute imports, by adding src to tsconfig.json

Or perthaps you meant to say that this plugin won't touch TS files unless eslint-import-resolver-typescript is installed?

CRA does - but this eslint plugin indeed can't know about that unless you configure that eslint resolver.

@ljharb Oh ok, that makes perfect sense. I'm not familiar with that plugin, so I'll start reading the docs right away.

Fixed by doing the following:

  1. installed the plugin above by running yarn add eslint-import-resolver-typescript
  2. Added this to my .eslintrc.json
{
  // ......

  "settings": {
    "import/resolver": {
      "typescript": {
        "project": "."
      }
    }
  }
}
Was this page helpful?
0 / 5 - 0 ratings