-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 doesn't support nested package.json #458
Comments
@sindresorhus Any chance you have already written a tool to list all package.json files (like @nkt Out of curiosity, what is the use of those |
@benmosher I propose to have an option (name TBD) to use all |
@jfmengels no, I'm not. I use
I'm too. It's not monorepo. It's react component library. react-starter-kit uses the same approach. |
I haven't. Go ahead. |
@jfmengels I'm thinking maybe this just shouldn't be supported. Autofix would be impossible in this case, plus I occasionally run into problems with the resolvers because they are descendants of the folder with the root Up to you, though, if it's easy and sane to do it behind an option. |
I'm having my doubts too, but I'm not sure what the viable option would be for people who want to have such as project setup, except turn this rule off either entirely or in that folder. Well, either that or stop using I do think it's strange to use a
Easy: Meh, have to create a new package and have to add a new dependency here. Any comments from others to help us decide? Especially about the following concern:
|
You should know, {
"name": "GoogleMap",
"main": "./ServerGoogleMap.js",
"browser": "./GoogleMap.js"
} What if rule will check |
Didn't know that, thanks!
You would then necessarily need to use ESLint from the root of the project, which is not always the case. I'm not sure your editor does that for instance when you open your workspace folder. |
@jfmengels maybe eslint has API which gives root config filename? I mean if config |
I couldn't find one, but that wouldn't work either for some projects.
2 - This still wouldn't address the concern @benmosher mentionned about the sub-packages not declaring all their dependencies, or is that what you meant with the |
This would be great for me. I work on a monorepo (here’s why) with a root package.json and sub-directories with their own package.json. As it stands, I just have to disable this rule, which is a bummer, because it’s so useful. As I understand it, in order to resolve the installed dependencies accurately (in a way that matches node’s module system), the I understand the concerns about introducing this, but I would still like to put a strong favorable vote towards adding this as an option. Monorepos are fairly common and probably worth supporting. See Lerna for evidence of this, which can also serve as a way to make a quick example repo test case. And in terms of the more general question of sub-packages not declaring all their dependencies and therefore having to do multiple |
@acusti So am I understanding it right that #458 (comment) should resolve your problem?
@benmosher Would you be fine with adding this as an option? For the autofix, we could disable the autofix altogether if that option is enabled (imagining we get to it one day). |
Yup, an option to check all package.json for dependencies would work for me. |
@jfmengels Yeah, that should be fine. I like having it behind an option. and it's fair to ignore autofix until it actually exists 😎 |
The option to check all package.json sounds good! Could I suggest the option to specify an array of package.json paths or a blob based on the eslintrc file that is applying the rule? |
@migueloller That sounds to me like a good idea if you have dependencies declared in /foo/package.json that you do not want to be accessed in /foo/bar/baz.js (meaning that you'd have to re-declare all wanted dependencies in /foo/bar/package.json). Can someone confirm that they have a need for something like this? |
I guess I'll confirm my own reasons 😉 . If I can specify that the package.json to check is the one at the root then it would ignore other nested package.json. The array would look something like Potentially this could also include negation patterns, and other blob features (see .eslintignore). |
Expanding on my previous comment, here's what my ESLint settings look like: 'import/resolver': {
node: {
moduleDirectory: [
'node_modules',
'./src',
],
},
}, Inside {
"name": "services"
} Elsewhere in my project I can now import a component (or a service or a scene) by simply typing: import Button from 'components/Button' This is much more manageable than typing something like: import Button from '../../../../../../../../components/Button'; Currently, I get If the rule allowed me to do the following, it would solve this issue: 'import/no-extraneous-dependencies': {
moduleDirectories: ['./'] // or any other configuration that's most convenient for all
}, |
Jumping here, monorepo user, maybe we could:
Or just a way to express some different packages.json to read. Dunno |
This rule doesn't work with nested package.json files: import-js/eslint-plugin-import#458
This rule doesn't work with nested package.json files: import-js/eslint-plugin-import#458
In some specific situations, enabling this rule to go up the hierarchy of Let's say you have the following project structure:
with the following contents: // package.json
{
"name": "my-app",
"dependencies": {
"lodash": "3.10.1"
}
} // file1.js
const _ = require('lodash');
// Expected output: 3.10.1
console.log(_.VERSION); // nested/package.json
{
"name": "nested",
"dependencies": {
"babel-types": "6.16.0"
}
} // nested/file2.js
const _ = require('lodash');
// Expected output: 3.10.1
console.log(_.VERSION); Then we have the following outputs: node file1.js
> 3.10.1 // All good!
node nested/file2.js
> 4.16.4 // Wrong! Expected to also be 3.10.1: the version we depend on in the root package.json Even though we declared the dependency on However, if Thus, the only sane situations in which it would be good to support nested In the end, the use-cases of @nkt and @migueloller will work fine, but it's not generally safe - at least version wise - to support nested |
@tibdex, you make a fair point. Usually nested package.json are used in a monorepo or very large projects. In the first case, all dependencies will be declared in the nested package.json for the relevant package (meaning that for your example The issue you raise is a valid but I would argue that it's more of a Node.js issue that the developer should be aware of if they opt in to use nested package.json. In this case it would still make sense for ESLint to warn nested package.json by default. If the developer wants to opt in though, they shouldn't have to disable the entire rule to do this. Being able to modify it to support the nested package.json (much like Node.js module resolution algorithm does) seems OK to do. Would you agree? |
@lotap I am using the version 2.8.0 and this didn't work for me. Why? |
I am also using 2.8.0 with the same code that @lotap provided and getting the same error :/. |
@kopax @kilpatty this is the code I currently use in my boilerplate:
That being said, I haven't worked on a project that uses multiple package.json files in a long time, so your mileage may vary |
I just hit this today too. I think the only real way to fix is to enable an array of 'import/no-extraneous-dependencies': [
'error',
{
devDependencies: true,
packageDirs: [
'./', // this package
'../../', // mono-repo root
],
},
], |
Just throwing a +1 for the solution that @tizmagik proposed. I'm hitting this problem in a monorepo architecture as well. I see that this issue is closed, should I created a new issue for this? |
@rjhilgefort this solved it for me: "import/no-extraneous-dependencies": ["error", {"devDependencies": true, "packageDir": "./"}] |
Here's a version that works in all contexts (lerna root + packages / vscode) with a root level 'import/no-extraneous-dependencies': context => [
'error',
{
devDependencies: true,
packageDir: [context.getFilename(), __dirname]
}
] The edit: doesn't cover sibling package dependencies though, since the require are declared using the fqpn |
@medfreeman how do you define |
You don’t; eslint provides it. |
When trying to pass a function to the rule with both eslint 5.16.0 and 6.0.0-alpha I get
|
Any solution for @jacobrask 's issue? |
@medfreeman what version of eslint are you using? you didnt face any issue like @jacobrask's issue? |
@mudin eslint 5.13.0 |
@medfreeman Thanks! |
Is there any news on @jacobrask issue? |
Also wondering if this has a solution other than "off" |
so we are on 2020 no further improvements on this apart just to switch the rule off? |
Example of my project structure:
Root
package.json
contains all dependencies, nestedpackage.json
looks like this:That's why I got wrong warnings:
Is this fixable?
The text was updated successfully, but these errors were encountered: