Hi there,
When using import/order in conjunction with the import/no-unresolved rule, it doesn't appear possible to configure a resolver to distinguish between external (i.e. node_modules) and internal/absolute imports (i.e. src). In every case, either the resolver groups the external and absolute imports together, or, the resolver is unable to resolve one of the groups.
The following configuration is able to automatically add a newline between the external and absolute imports, but is unable to resolve the external imports:
"eslintConfig": {
"extends": [
"eslint:recommended",
"react-app",
"plugin:import/recommended",
"plugin:import/react"
],
"rules": {
"import/order": [
"error",
{
"groups": [
"builtin",
"external",
"internal",
"parent",
"sibling",
"index"
],
"newlines-between": "always"
}
]
},
"settings": {
"import/external-module-folders": [
"node_modules"
],
"import/resolver": {
"node": {
"moduleDirectory": [
"src"
],
"extensions": [
".jsx",
".js"
]
}
}
}
}
In order to get the external imports to resolve, adding node_modules to the module_directory works, but then external and absolute internal imports are grouped together.
I've tried additional configurations using the webpack resolver as well, but am running into the same problem. Any help would be greatly appreciated!
Happens to me when I upgraded to the latest version. Was not the case before. Moved from 2.18.0 to 2.20.1
@constgen Thanks for your reply!
After a bit of bisecting it appears that in this particular case, the bug only affects Windows on v2.20.1. I tested both MacOS and Ubuntu and both were working properly on v2.20.1. So for now, I have rolled back to v2.20.0 for Windows compatibility.
Here is the latest eslint configuration I'm using that works with Windows on v2.20.0, but not v2.20.1:
"eslintConfig": {
"extends": [
"eslint:recommended",
"react-app",
"plugin:import/recommended",
"plugin:import/react"
],
"rules": {
"no-console": "warn",
"import/order": [
"error",
{
"groups": [
"builtin",
"external",
"internal",
"parent",
"sibling",
"index"
],
"newlines-between": "always"
}
]
},
"settings": {
"import/resolver": {
"webpack": {
"config": {
"resolve": {
"extensions": [
".js",
".jsx"
],
"modules": [
"src",
"node_modules"
]
}
}
}
}
}
}
@warnellw can you perhaps try on windows on latest master on this plugin?
@ljharb Yes, #1651 seems to have fixed it - thanks!
Running into this same issue on Windows, any timeline on when this fix will be released?
hi
First the PR is fixed but still not published on NPM. Is it possible to publish it to npm ?
Secondly, in case of monorepo, the external resolving is not working at all, as modules are symlinked and are not under node_modules folder.
In this case, it doens't work at all.
I don't understand how your trying to detect if a module is external withot knowing what is the path of the module we work on.
in case we know it, a path.relative(from, to) is enough to know if we are in or out of our module.
But we need the package.json path extra info to be in all your importType management, that we don't have at all.
I can work on it but it will generate lot of changes
Yes, it will be published to npm with the next release. That will happen when I have time to do it, and not before.
For the other thing, can you file a new issue to discuss what you need and why?
Sure
I've create a PR here to fix all these problems #1696
Do I need to create a new defect or we can talk over the PR itself ?
Globally I replace the external detection part thanks to the resolved path, because before the resolving, hte import name has no real value.
Everything works correctly now, window / unix, monorepo, aliases, ...
Tell me if you can look at it
The PR is fine, I'll take a look at it today.
Most helpful comment
Running into this same issue on Windows, any timeline on when this fix will be released?