eslint-import-resolver-typescript icon indicating copy to clipboard operation
eslint-import-resolver-typescript copied to clipboard

import/no-unresolved: wildcard exports causing import resolution error in a monorepo

Open davbrito opened this issue 2 years ago • 4 comments

In my monorepo, I have a package.json file with the following configuration:

{
  "name": "example",
  "private": "true",
  "type": "module",
  "version": "1.0.0",
  "description": "",
  "devDependencies": {},
  "scripts": {},
  "exports": {
    "./*.ts": "./src/*.ts"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

However, when I try to import a module from this package in my code with the statement import { example } from "example/index.ts", I encounter the following error:

error: Unable to resolve path to module 'example/index.ts' (import/no-unresolved) at src/index.ts:1:25:
> 1 | import { example } from "example/index.ts";
    |                         ^
  2 | 
  3 | example();
  4 | 

This is my .eslintrc

{
  "extends": ["plugin:import/recommended", "plugin:import/typescript"],
  "parser": "@typescript-eslint/parser",
  "plugins": ["import"],
  "rules": {
    "import/no-unresolved": "error"
  },
  "settings": {
    "import/resolver": {
      "node": true,
      "typescript": true
    }
  }
}

Here is a demo CodeSandbox that reproduces the error: https://codesandbox.io/p/sandbox/amazing-booth-i2ugum?welcome=true

davbrito avatar Mar 24 '23 18:03 davbrito

Hi. This fixed a similar issue for me. I didn't need to add a exports entry to my package.json files though. I'm also not using the .ts file extension in my import lines in my TypeScript code

I fixed my issue by adding this to my eslint.cjs file in the root of my monorepo:

    parserOptions: {
        project: ['tsconfig.json', 'packages/*/tsconfig.json'],
        node: true,
    },
    settings: {
        'import/parsers': {
            '@typescript-eslint/parser': ['.ts'],
        },
        'import/resolver': {
            typescript: {
                // See https://github.com/import-js/eslint-import-resolver-typescript for documentation of these settings
                // This `project` entry is needed, in addition to the `project` entry above under `parserOptions`,
                // otherwise eslint-plugin-import's `import/no-unused-modules` rule will not work correctly
                project: ['tsconfig.json', 'packages/*/tsconfig.json'],
            },
        },
    },

Equivalent syntax for a .eslintrc file:

{
    "parserOptions": {
        "project": [
            "tsconfig.json",
            "packages /*/tsconfig.json"
        ],
        "node": true
    },
    "settings": {
        "import/parsers": {
            "@typescript-eslint/parser": [
                ".ts"
            ]
        },
        "import/resolver": {
            "typescript": {
                "project": [
                    "tsconfig.json",
                    "packages/*/tsconfig.json"
                ]
            }
        }
    }
}

simondean avatar Apr 14 '23 17:04 simondean

Any progress on this? I also want to use the exports field in a monorepo, and made typescript work, except I couldn't make eslint to know about imports.

jim-y avatar Nov 08 '23 14:11 jim-y

Any issue should be reported with an online runnable reproduction, otherwise I can help to debug at all. Mostly this should belong to enhanced-resolve.

JounQin avatar Dec 06 '23 15:12 JounQin

This worked for me: https://www.npmjs.com/package/eslint-import-resolver-exports

"import/resolver": {
      // https://www.npmjs.com/package/eslint-import-resolver-typescript
      typescript: {
        project: ["tsconfig.json", "packages/*/tsconfig.json", "applications/*/tsconfig.json"]
      },
      // https://www.npmjs.com/package/eslint-import-resolver-exports
      exports: {}
    }

Canuckaholic avatar Dec 13 '23 23:12 Canuckaholic

Closing due to lack of reproduction.

@Canuckaholic eslint-import-resolver-exports is unnecessary when eslint-import-resolver-typescript is already used.

Feel free to provide a reproduction if it doesn't work for you.

JounQin avatar Mar 17 '25 14:03 JounQin