eslint should be listed in the project's dependencies, not devDependencies
Asked Answered
R

8

168

Either I don't understand dependencies vs. devDependencies in node 100% yet or eslint is just wrong here (not capable of analyzing this correctly):

   3:1   error  'chai' should be listed in the project's dependencies, not devDependencies              import/no-extraneous-dependencies
   4:1   error  'chai-enzyme' should be listed in the project's dependencies, not devDependencies       import/no-extraneous-dependencies
   5:1   error  'enzyme' should be listed in the project's dependencies, not devDependencies            import/no-extraneous-dependencies
   7:1   error  'sinon' should be listed in the project's dependencies, not devDependencies             import/no-extraneous-dependencies
   9:1   error  'redux-mock-store' should be listed in the project's dependencies, not devDependencies  import/no-extraneous-dependencies

These are test dependencies, so why is it saying that they should be listed in dependencies?

Additional note: We're using Travis as our CI so I don't know if it makes a difference for that at all either.

Redfield answered 6/7, 2017 at 3:28 Comment(0)
R
198

Solved it with adding this to my .eslintrc:

"import/no-extraneous-dependencies": ["error", {"devDependencies": true}]

[no-extraneous-dependencies] Add exceptions? #422

Based on this user's reply:

you could set the option devDependencies: true in an .eslintrc in your test folder:

rules: import/no-extraneous-dependencies: [error, { devDependencies: true }] Then you'll get reports of any packages referenced that are not included dependencies or devDependencies. Then you get the goodness of the rule, with no noise from the disable comments.

I think that might work for you? This is how I would use the rule, in your case, since you have your test code separated into a test directory.

Also this post was helpful to confirm I wasn't insane to not want some of these in my dependencies list: Sharable ESLint Config

Redfield answered 6/7, 2017 at 4:1 Comment(4)
Make sure to restart your node.js server with CTRL-C and a npm start it for this to catch onSelfjustifying
Have a look at my answer if you want to disable the lint only for certain files (i.e. tests).Relativize
This could lead to missing dependencies when running in non-development mode. I think @Relativize answer is a better alternative.Catnip
the answer of @Relativize is better as it keeps the eslint config in a single place - easier to comprehend and maintain.Buber
R
132

If you want to allow imports of devDependencies in test files only you can use an array of globs, as the documentation of no-extraneous-dependencies states:

When using an array of globs, the setting will be set to true (no errors reported) if the name of the file being linted matches a single glob in the array, and false otherwise.

The following setting will disable the lint for test files only.

"import/no-extraneous-dependencies":[
  "error",
  {
     "devDependencies":[
        "**/*.test.ts",
        "**/*.test.tsx"
     ]
  }
]

That way imports from devDependencies are still reported as errors.

Relativize answered 26/4, 2019 at 8:40 Comment(3)
Anyone have a clue why adding that yields, Unexpected top-level property "import/no-extraneous-dependencies" in VScode... While the reported error is 'yaml' should be listed in the project's dependencies, not devDependencies. eslint(import/no-extraneous-dependencies)Hynda
@Hynda do you have that as a property of the rules object in your eslint file?Sanorasans
@DavidGilbertson Hmm. I thought I had, but trying to add it right now, it works fine. Thanks.Hynda
A
7

(2023 Answer)

I fixed it by restarting my code editor.

Similar to the suggestion to restart ESLint, I restarted my code editor and the error went away.

Accrete answered 29/12, 2023 at 16:48 Comment(1)
Gosh I should have done this first...Ingratiate
B
6

I fixed it by using

 'import/no-extraneous-dependencies': [
      'error',
      {
        projectDependencies: false,
      },
    ],
Beamy answered 12/8, 2022 at 5:11 Comment(2)
Gives me error: Value {"projectDependencies":false} should NOT have additional properties.Hedelman
@Hedelman Maybe it's just because of the trailing comma inside the object (after "false" here: projectDependencies: false,)Inheritor
K
4

I was able to solve it by adding the missing packages (in my case, Typescript and Storybook) to my plugins directory in .eslintrc.

I give the specifics in this post: ESLint error: '@storybook/react' should be listed in the project's dependencies, not devDependencies

Kenny answered 4/1, 2022 at 22:43 Comment(1)
In another case, I had 'react' in node_modules but it wasn't finding it for some reason so I deleted node_modules and reinstalled of them with yarn and that solved it.Kenny
S
3

By me It was fixed by adding this:

"import/no-extraneous-dependencies": "off"
Seligmann answered 1/3, 2023 at 18:16 Comment(1)
This is not an answer, it just turns the rule off!!!Tabb
V
2

finally found solution. to .eslintrc pass it like this:

"import/no-extraneous-dependencies":[
  "error",
  {
     "devDependencies":[
        "**/*.test.ts",
        "**/*.test.tsx"
     ]
  }
]

thanks to @magic_al answer and than restart your ESLint server. (for those running on VSCode press F1 and than search for ESLint: Restart ESLint Server)

Vashtivashtia answered 16/9, 2023 at 19:42 Comment(0)
E
1

In my case, with latest eslint + import plugin, I needed to DELETE the rule customizations from my eslintrc.

Elmiraelmo answered 19/12, 2023 at 22:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.