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?
Add "root": true to your eslintrc, just in case?
@ljharb it does not help for me
Does not work for me neither.
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
@ljqx it makes sense
i'm facing similar issue with lerna monorepo