Eslint-plugin-import: import/no-unresolved fails to find missing dependencies within a workspace

Created on 17 Mar 2020  Â·  4Comments  Â·  Source: benmosher/eslint-plugin-import

I have a Yarn workspace with the following structure:

- package.json
- packages/
   - myPackage/
      - package.json
      - index.js

In ./package.json, I have

"name": "@my/workspace",
"devDependencies": {
  "@storybook/addon-knobs": "^5.0.10"
}

That adds react-transition-group through the following dependency tree:

@my/[email protected]
└─┬ @storybook/[email protected]
  └─┬ [email protected]
    └── [email protected]

In ./packages/myPackage/index.js, I have

import { CSSTransition } from 'react-transition-group'

but I don't declare a direct dependency from @my/package to react-transition-group.

import/no-unresolved finds no problems. But if I publish @my/package, it (naturally) doesn't have access to react-transition-group.

Most helpful comment

I can not do like @jamesarosen, I have this error:

Error: .eslintrc » @openagenda/eslint-config:
        Configuration for rule "import/no-extraneous-dependencies" is invalid:
        Severity should be one of the following: 0 = off, 1 = warn, 2 = error (you passed '[Function: import/no-extraneous-dependencies]').

All 4 comments

This also happens with other packages within the same workspace. So given

// packages/one/index.js
import Two from "@my/two"
export default function One() {
  return <span class='one'><Two /></span>
}


// packages/two/index.js

export default function Two() {
  return <span class='two'></span>
}

import/no-unresolved reports no problems even if packages/one/package.json declares no dependency on "@my/two".

no-unresolved only checks what's in node_modules; not what will be there when you publish it.

no-extraneous-dependencies is the rule you want to be using, which will ensure that everything you import/require is actually in your package.json.

Thanks for that, @ljharb!

I have

'import/no-extraneous-dependencies': context => [
  'error',
  {
    devDependencies: false,
    packageDir: [context.getFilename(), __dirname],
  },
],

in my root package.json so I can use _dev_ dependencies from the root workspace in each package to work around #458. Maybe I need to narrow down the list of files that matches.

I can not do like @jamesarosen, I have this error:

Error: .eslintrc » @openagenda/eslint-config:
        Configuration for rule "import/no-extraneous-dependencies" is invalid:
        Severity should be one of the following: 0 = off, 1 = warn, 2 = error (you passed '[Function: import/no-extraneous-dependencies]').
Was this page helpful?
0 / 5 - 0 ratings