HaveSpacesuit
HaveSpacesuit

Reputation: 4004

Setting up eslint-import-resolver-typescript in monorepo

I'm trying to add path-mapping to a monorepo. I already have eslint-plugin-import rules in place, and I'm getting an error "Unable to resolve path to module" on all mapped imports.

app/
├─ package/
│  ├─ src/
│  ├─ tsconfig.json
.eslintrc
tsconfig.json

.eslintrc looks like

{
  "parser": "@typescript-eslint/parser",
  "plugins": [
    "@typescript-eslint",
    "import",
  ],
  "extends": [
    "eslint:recommended",
    "plugin:import/recommended",
    "plugin:import/typescript"
  ],
  "settings": {
    "import/resolver": {
      "typescript": {}
    }
  }
}

The inner tsconfig.json looks like:

{
  "extends": "../tsconfig",
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "actions/*": ["src/actions/*"],
      "context/*": ["src/context/*"]
    }
  }
}

The paths map just fine, but I have the lint error "Unable to resolve path to module 'context' wherever I try to use the mapped path.

Researching this shows I need to use the eslint-import-resolver-typescript package, but it hasn't made the errors go away.

Upvotes: 12

Views: 14211

Answers (1)

HaveSpacesuit
HaveSpacesuit

Reputation: 4004

This is the lint settings that ended up working:

"settings": {
  "import/parsers": {
    "@typescript-eslint/parser": [".ts", ".tsx"]
  },
  "import/resolver": {
    "typescript": {
      "project": ["tsconfig.json", "package/tsconfig.json"]
    },
    "node": {
      "project": ["tsconfig.json", "package/tsconfig.json"]
    }
  }
}

I'm not positive the node settings are needed. There is more conversation on the import-js github page.

Upvotes: 13

Related Questions