import-js/eslint-plugin-import

`paths` in tsconfig.json breaks `import/no-extraneous-dependencies`

Open

#2,617 opened on Dec 16, 2022

View on GitHub
 (5 comments) (3 reactions) (0 assignees)JavaScript (1,540 forks)batch import
enhancementhelp wantedtypescript

Repository metrics

Stars
 (4,946 stars)
PR merge metrics
 (Avg merge 138d 22h) (3 merged PRs in 30d)

Description

Our project is a mono repo managed by Nx. All dependencies are installed at the root directory and the package.json containing all dependencies is also there. When I import logger in another repo, the rule import/no-extraneous-dependencies fails.

The directory structure would be like:

.
├── node_modules
├── libs
│   ├── logger
│   │   ├── package.json
│   │   ├── src
│   │   ├── tsconfig.json
│   │ 
│   └── another-lib
│       ├── package.json
│       ├── src
│       ├── tsconfig.json
├── package.json
├── .eslintrc.js
├── tsconfig.base.json

.eslintrc.js:

module. Exports = {
...
settings: {
    'import/resolver': {
      typescript: {
        project: 'tsconfig.base.json',
      },
      node: {
        extensions: ['.js', '.jsx', '.ts', '.tsx'],
      },
    },
},
rules: {
...
        'import/no-extraneous-dependencies': [
          'error',
          {
            devDependencies: [
              '**/__tests__/**', // jest pattern
            ],
            optionalDependencies: false,
            peerDependencies: false,
            packageDir: './',
          },
        ],
...
}
}

tsconfig.base.json:

"paths": {
      "@my/logger": [
        "libs/logger/src/index.ts"
      ],
      "@my/another-lib": [
        "libs/another-lib/src/index.ts"
      ]
}

package.json in root directory is a normal one:

{
  "name": "my",
  "version": "0.0.0",
  "private": true,
  "scripts": {
...
  },
"dependencies": {
...
  },
  "devDependencies": {
...
  },
...
}

package.json in libs is really simple:

{
  "name": "@my/another-libr",
  "version": "0.0.1",
  "type": "commonjs"
}

tsconfig.json in libs will extends the base one.

And when I import logger from "@my/logger" in another-lib, this rule will fail

  4:1  error  '@my/logger' should be listed in the project's dependencies. Run 'npm i -S @olm/logger' to add it  import/no-extraneous-dependencies

Contributor guide