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

no-extraneous-dependencies not checking dependencies #1177

Open
volosovich opened this issue Oct 1, 2018 · 12 comments
Open

no-extraneous-dependencies not checking dependencies #1177

volosovich opened this issue Oct 1, 2018 · 12 comments

Comments

@volosovich
Copy link

The same question on stackoverflow https://stackoverflow.com/questions/52554918/eslint-no-extraneous-dependencies-issue

I have a problem with eslint rule import/no-extraneous-dependencies

What needs to do. If js file has import with a package which not present in closest parent package.json - show error.

Rule description : https://github.com/benmosher/eslint-plugin-import/blob/HEAD/docs/rules/no-extraneous-dependencies.md

Project very simple:
Structure of folders:

    ./
    ├── eslintrc.js
    ├── index.html
    ├── index.js
    └── package.json
    
    0 directories, 4 files

Package.json:

    {
      "name": "test",
      "version": "1.0.0",
      "main": "index.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "license": "ISC",
      "dependencies": {
        "jquery": "^3.3.1"
      },
      "devDependencies": {
        "eslint": "^5.6.0",
        "eslint-config-airbnb": "^17.1.0",
        "eslint-plugin-import": "^2.14.0",
        "eslint-plugin-jsx-a11y": "^6.1.1",
        "eslint-plugin-react": "^7.11.1"
      }
    }

My eslintrc.js

    module.exports = {
      parserOptions: {
      	ecmaVersion: 6
      },
      extends: 'airbnb',
      plugins: ['import'],
      // custom rules
      'rules': {
        'import/no-unresolved': 0,
        'import/extensions': 0,
        "import/no-extraneous-dependencies": ["error",
          {
            "devDependencies": false, 
            "optionalDependencies": false, 
            "peerDependencies": false,
          }
         ]
      }
    };

my index.js

    import moment from 'moment';
    
    moment();

moment is not present in package.json, but when I run eslint with my config, it's not showing errors:

./node_modules/eslint/bin/eslint.js -c ./eslintrc.js ./index.js

Result - nothing, but when I change eslintrc.js line "devDependencies": true,, then add to index.js import 'eslint'; and rerun CLI command, all works as expected, and error is showing.

What I'm doing wrong?

@volosovich volosovich changed the title no-extraneous-dependencies not check dependencies no-extraneous-dependencies not checking dependencies Oct 1, 2018
@ljharb
Copy link
Member

ljharb commented Oct 1, 2018

Add "root": true to your eslintrc, just in case?

@volosovich
Copy link
Author

@ljharb it does not help for me

@hornta
Copy link

hornta commented Oct 9, 2018

Does not work for me neither.

@ljqx
Copy link
Contributor

ljqx commented Nov 18, 2018

In the doc, it says

Modules have to be installed for this rule to work.

Here in your example, moment doesn't present in node_modules.

Related code:
https://github.com/benmosher/eslint-plugin-import/blob/798eed7e559adab2eac07bf1b3e3518d4b7a4296/src/rules/no-extraneous-dependencies.js#L114-L115

@volosovich
Copy link
Author

volosovich commented Dec 11, 2018

@ljqx it makes sense

@armano2
Copy link

armano2 commented Feb 8, 2020

i'm facing similar issue with lerna monorepo

@bengao01
Copy link

For this issue, would you accept a PR to add a new option to the no-extraneous-dependencies rule that will report a lint warning when the module isn't installed? Even if the module isn't installed, we'd still like to have it flagged as being an extraneous dependency if a file is trying to import it. The project I'm working on also ran into this issue, and it would be helpful to have this as an option that we can specify. The old TSLint rule no-implicit-dependencies would report modules that weren't installed as warnings in its default behavior. I think it wouldn't be too much effort to add since we already have a case checking for this (https://github.com/import-js/eslint-plugin-import/blob/master/src/rules/no-extraneous-dependencies.js#L175).

This rule also currently checks for aliases for package names. I think having an option to disable this check could make sense for some projects that don't need this extra functionality.

@ljharb
Copy link
Member

ljharb commented Aug 16, 2021

There's already import/no-unresolved that should be reporting not-installed things.

@bengao01
Copy link

Ah yes, I noticed that rule exists, but I think for our purposes and others as well, it would be helpful if we had this as an option on this rule. The way my project is set up, I use symlinks for some of my custom packages, and imports for these packages wouldn't be able to be resolved, since they point to the compiled index.js file which doesn't exist until I compile my typescript. That's why I'm not able to use the import/no-unresolved rule in conjunction with this, and why I think being able to flag dependencies that aren't listed in the package.json, regardless of if they are installed or not, would be super helpful. If you're open to a PR for this, I can work on implementing this option and it shouldn't be too much additional work

@ljharb
Copy link
Member

ljharb commented Aug 16, 2021

You should be compiling your typescript before running the linter, so I'm not sure why that's an issue.

The problem is that we can't know if it's a package.json dependency until it's actually resolved, because it could be coming from a symlink, or a TS "path", or a babel alias, etc - that's what the custom resolver answers for us.

@bengao01
Copy link

Yeah, I think you're right that in the cases where people run eslint during CI, they'll have compiled the typescript and then run the linter. In our setup and I think in a good number of others, we run eslint in our editor, so typescript might not be compiled for some of the other packages in our monorepo. Typescript actually explicitly supports this feature (https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#build-free-editing-with-project-references) so we can run eslint without building the other packages. We still would like to have the eslint errors show up in the editor, not just after CI runs.

We're actually working on a project to migrate off of TSLint onto ESLint, which is why we're trying to use rules that have parity with the old TSLint versions, since it would be frustrating for developers used to having these lint errors caught in the editor suddenly have their builds failing during CI.

@ljharb
Copy link
Member

ljharb commented Aug 17, 2021

Unfortunately I think it's just not technically possible to do this, because eslint-plugin-import supports far more variations on specifiers than TS (and thus TSLint) does.

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

No branches or pull requests

6 participants