Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ESLint - Only Allow Relative Import Paths Not Absolute

On a similar note as the question ESLint - Only Allow Absolute Import Paths Not Relative

How can eslint made to error on absolute imports? Specifically interested in the context of TypeScript.

like image 464
Luke Avatar asked Sep 17 '25 00:09

Luke


2 Answers

set 'relative path only' in IDE and remove baseUrl from tsconfig.json

like image 154
glushkina1 Avatar answered Sep 18 '25 19:09

glushkina1


You could try to use the rule @typescript-eslint/no-restricted-imports to disallow absolute imports (anything that does not start with ./ or ../).

{
  rules: {
    "no-restricted-imports": "off",
    "@typescript-eslint/no-restricted-imports": [
      "error",
      {
        "patterns": ["!./*", "!../*"]
      }
    ]
  }
}

@typescript-eslint/no-restricted-imports extends eslint/no-restricted-imports.

The reason why we disable eslint/no-restricted-imports is because it can report incorrect errors.

More information here.

like image 26
Akasha Avatar answered Sep 18 '25 17:09

Akasha