eslint-plugin-import: [no-extraneous-dependencies] Add exceptions?

For a specific package, I’m importing babel-core in my test files, but babel-core is set as a devDep because I’m compiling the files before publishing my package. Therefore, there’s no reason to put the module as a direct dependency.

~/dev/babel-plugin-module-alias/test/index.js
  4:1  error  'babel-core' should be listed in the project's dependencies, not devDependencies  import/no-extraneous-dependencies

Would it be possible to not run no-extraneous-dependencies in specific directories/files?? Like test.js, test/**/*.js, __tests__/**/*.js, **/*.test.js or **/*.spec.js

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 21 (11 by maintainers)

Commits related to this issue

Most upvoted comments

Simple to do:

"import/no-extraneous-dependencies": 0,

add this to rules

@tleunen 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.

actually, scratch that: you may be able to specify "import/core-modules": [ "ember" ] in your .eslintrc.js.

refer to the import/core-modules docs for more info.

Would it be possible to not run no-extraneous-dependencies in specific directories/files?

You can, in some cases, but this is a limitation of ESLint. If you want to disable the rule in a folder, you can add a .eslintrc file at the root of that folder where you disable the rule. Or the other way around: disable it by default in your root config, and activate it using a .eslintrc file where appropriate (a src folder for instance).

The other options are to:

  • disable the rule in each file you want to disable it for using /* eslint-disable import/no-extraneous-dependencies */ at the top of your file
  • enable the rule in each file you want to enable it for using /* eslint import/no-extraneous-dependencies: ["error", {options: ...}] */ at the top of you file
  • (probably the safest option, as it’s the only one that does not disable the rule for a whole file altogether) disable the line you want to ignore using `require(‘babel-core’); // eslint-disable-line import/no-extraneous-dependencies
  • or a combination of all the previous possibilities

Or my favorite, use glob based configurations https://github.com/eslint/eslint/issues/3611. Unfortunately, that is not yet implemented 😕 (should you be using XO, then it is)

Of course, we could disable the rule in the code if we notice that the file in question is in some directory, but this could be applied to so many rules that it’s better to invest time to make it work in https://github.com/eslint/eslint/issues/3611 IMO.

Let me know if this answers your needs. If so, I’ll close the issue 😃

Sure, but then you’re disabling this very useful rule.

FWIW: v1.15.0 shipped with #527, which allows globs in addition to booleans for the various dependency flavors. Docs here: https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-extraneous-dependencies.md#options

(apparently we changed our minds 😅)

@Willibaur: if ember is a magic dependency (i.e. not listed in package.json), then you won’t be able to use no-extraneous-dependencies, IIRC. It assumes a pretty standard npm/node view of the world.

Hi all, I’ve been through many of the Issues on this repo, and could not find what I’m looking for so I’m going to leave my question here, I have this rule in my .eslintrc.js

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

But is still giving my a bunch of errors, all of them like this one

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

Any help would be appreciated Thanks in advance

@tleunen I had the same question. The Internet is small sometimes 😄

Yep I was using the comment to disable it, I’m ok 😃

I thought it was potentially an error in the rule but everything is ok if you suggest using the comment anyway 😃 Thanks!