Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
davbrito opened this issue Mar 24, 2023 · 4 comments

Comments

@davbrito
Copy link

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

@simondean
Copy link

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"
                ]
            }
        }
    }
}

@jim-y
Copy link

jim-y commented Nov 8, 2023

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.

@JounQin
Copy link
Collaborator

JounQin commented Dec 6, 2023

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.

@Canuckaholic
Copy link

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: {}
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

5 participants