Solution according to docs
From my experience with the "babel-plugin-root-import" plugin, you should change your configuration to:
"plugins": [[
"babel-plugin-root-import",
{
"rootPathPrefix": "~"
"rootPathSuffix": "src",
}
]]
The extra slashes (/
) and dots (.
) seem to confuse the plugin.
But ...
Unfortunately, the eslint packages for "babel-plugin-root-import" are not well maintained at this time. Based on your eslintrc, it appears you are using the eslint-import-resolver-babel-root-import
package so you "Don't let ESLint be Confused". If you have not installed that package yet, you may want to try that first.
I am currently trying the "eslint-import-resolver-babel-root-import-fixed" fork, which was updated more recently. It is configured slightly differently due to NPM naming collisions; in your eslintrc file, you will need to replace babel-plugin-root-import
with eslint-import-resolver-babel-root-import-fixed
.
Solution that works
With all that said, I have not been able to get any of these to work. When compiling with babel, there are no problems, but eslint throws the errors as you have described.
My solution for the past couple of years has been to add the following eslint rule:
"import/no-unresolved": [
"error",
{
"ignore": [
"^[~]",
]
}
]
Another Approach
I use the babel-plugin-root-import
because we have multiple root prefixes. If you only need the one, I suggest looking into babel-plugin-module-resolver
, though that may have its own set of problems. I've also used "Webpack Module Aliases" in the past, though that may not be available to you.