I've seen a few similar questions to this, but I think this issue is a little different.
I have shared ESLint config coming from a dependency, let's call it my-shared-config. The dependency contains a few plugins including @babel like so:
{
env: { ... },
plugins: [
'@babel',
'import',
'inclusive-language',
'react',
'react-hooks',
],
rules: { ... },
}
The package.json contains these dependencies, among others:
"dependencies": {
"@babel/core": "7.17.0",
"@babel/eslint-parser": "7.17.0",
"@babel/eslint-plugin": "7.16.5",
"@babel/preset-react": "7.16.7",
"eslint": "8.8.0",
"eslint-plugin-import": "2.25.4",
"eslint-plugin-inclusive-language": "2.2.0",
"eslint-plugin-react": "7.28.0",
"eslint-plugin-react-hooks": "4.3.0"
},
For most projects, the shared config works. However, in one project when I run the linter, I get the following error:
Error: Failed to load plugin '@babel' declared in '--config » my-shared-config': Cannot find module '@babel/eslint-plugin'
The only difference with this project to the others - that I can think of - is that it also installs other @babel/ dependencies. My node_modules structure looks like this:
node_modules
@babel # <-- BABEL
code-frame
compat-data
core
...more, but nothing for ESLint
@my-domain
my-shared-config
node_modules
@babel # <-- BABEL
compat-data
core
eslint-parser # <-- BABEL ESLINT
eslint-plugin # <-- BABEL ESLINT
...more
When I install @babel/eslint-parser and @babel/eslint-plugin into my project, the linter works fine. Whilst that's ok, it is obviously not a solution I want and I don't want other teams that use my-shared-config to have to do the same. Does anyone know of any other solutions for this problem? I've tried deleting and re-installing my node_modules.
I think this is likely to be an issue with Yarn rather than the Babel dependencies.
UPDATE
This issue only happens when the @babel/core versions are not exactly the same. After bumping the @babel/core version in my-shared-config, the linter throws the same error in other projects until it's own @babel/core version is the same.