I've been scratching my head non stop trying to setup an ESlint configuration in my monorepo. I'm using Yarn 3.2.4 as the package manager. Here is a GitHub repo containing an example project.. Here is the project structure :
/monorepo
├── /configs
│ ├── /eslint
│ │ ├── index.js
│ │ └── package.json
│ ├── /jest
│ │ ├── index.js
│ │ └── package.json
│ ├── /prettier
│ │ ├── index.js
│ │ └── package.json
│ └── /typescript
│ ├── tsconfig.json
│ └── package.json
├── /packages
│ └── /mypackage
│ ├── .eslintrc.js
│ └── package.json
└── package.json
With the following :
/monorepo/package.json
(Root package.json)
{
...,
"workspaces": [
"configs/*",
"packages/*"
],
"scripts": {
"lint": "yarn workspaces foreach -pt run lint"
}
}
/monorepo/configs/eslint/package.json
{
...,
"name": "@monorepo/eslint-config",
"main": "index.js",
"peerDependencies": {
"eslint": "^8.27.0",
...
* other eslint plugins / configs *
...
}
}
/monorepo/configs/eslint/index.js
is just my normal eslint config file, extending and using the eslint config packages listed above/monorepo/packages/mypackage/package.json
{
...,
"devDependencies": {
"@monorepo/eslint-config": "workspace:*",
"eslint": "^8.27.0",
...
* other eslint plugins / configs *
...
}
}
/monorepo/packages/mypackage/.eslintrc.js
module.exports = {
extends: ['@monorepo/eslint-config']
};
Now with this config, IT WORKS. However what I find really annoying is that I need to specify the eslint configs/plugins dependencies in EVERY package of my mono repo. If I try to place these dependencies in the devDependencies
of the eslint-config package, and remove them from the mypackage
package (leaving only eslint
as a dev dependency) I get the following error message :
Error: Failed to load plugin '@typescript-eslint' declared in '.eslintrc.js » @monorepo/eslint-config': Your application tried to access @typescript-eslint/eslint-plugin, but it isn't declared in your dependencies; this makes the require call ambiguous and unsound.
Required package: @typescript-eslint/eslint-plugin
Required by: /path/to/monorepo/packages/mypackage/
Require stack:
- /path/to/monorepo/packages/mypackage/__placeholder__.js
Referenced from: /path/to/monorepo/.yarn/__virtual__/@monkvision-eslint-config-virtual-abbbf308a5/1/configs/eslint/index.js
at Function.require$$0.Module._resolveFilename (/path/to/monorepo/.pnp.cjs:16089:13)
at Function.resolve (node:internal/modules/cjs/helpers:108:19)
at Object.resolve (/path/to/monorepo/.yarn/cache/@eslint-eslintrc-npm-1.3.3-9e3a462140-f03e9d6727.zip/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:2325:46)
at ConfigArrayFactory._loadPlugin (/path/to/monorepo/.yarn/cache/@eslint-eslintrc-npm-1.3.3-9e3a462140-f03e9d6727.zip/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3392:33)
at ConfigArrayFactory._loadExtendedPluginConfig (/path/to/monorepo/.yarn/cache/@eslint-eslintrc-npm-1.3.3-9e3a462140-f03e9d6727.zip/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3212:29)
at ConfigArrayFactory._loadExtends (/path/to/monorepo/.yarn/cache/@eslint-eslintrc-npm-1.3.3-9e3a462140-f03e9d6727.zip/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3133:29)
at ConfigArrayFactory._normalizeObjectConfigDataBody (/path/to/monorepo/.yarn/cache/@eslint-eslintrc-npm-1.3.3-9e3a462140-f03e9d6727.zip/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3074:25)
at _normalizeObjectConfigDataBody.next (<anonymous>)
at ConfigArrayFactory._normalizeObjectConfigData (/path/to/monorepo/.yarn/cache/@eslint-eslintrc-npm-1.3.3-9e3a462140-f03e9d6727.zip/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3019:20)
at _normalizeObjectConfigData.next (<anonymous>)
So my question is : Is there any way to only declare the ESlint plugin depenencies ONCE (in the config project) and not in every package of my monorepo ?
Sorry for the long post and thanks in advance.