I'm installing eslint
and Prettier
in my project and trying to get automatic code formatting to work through VSCode. When I go to a React file, I see that ESLint is in error so I open up the ESLint console where I see:
Failed to load plugin 'prettier' declared in 'js/.eslintrc.js': Cannot find module 'eslint-plugin-prettier'
I believe I have all the necessary modules installed, here is a piece of my package.json
file:
"devDependencies": {
"babel-eslint": "^10.0.3",
"babel-loader": "^8.0.6",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.0",
"eslint-loader": "^3.0.3",
"eslint-plugin-prettier": "^3.1.2",
"eslint-plugin-react": "^7.18.3",
"prettier": "1.19.1"
}
The only thing I can think of is that this is being caused by my project directory structure, which is as follows:
/
(some Java stuff out here)
js/
node_modules/
package.json
package-lock.json
.eslintrc.js
.prettierrc
For reference, here is my .eslintrc.js
:
module.exports = {
root: true,
env: {
browser: true,
node: true
},
parserOptions: {
parser: 'babel-eslint',
ecmaVersion: 2015,
sourceType: 'module'
},
extends: [
'prettier',
'plugin:prettier/recommended'
],
plugins: [
'react',
'prettier'
],
rules: {
}
}
For further reference, here is my settings.json
in VSCode:
{
"terminal.integrated.shell.osx": "/bin/zsh",
"editor.formatOnSave": false,
// turn it off for JS and JSX, we will do this via eslint
"[javascript]": {
"editor.formatOnSave": false
},
"[javascriptreact]": {
"editor.formatOnSave": false
},
// tell the ESLint plugin to run on save
"eslint.validate": [ "vue", "html", "javascript", "javascriptreact"],
"prettier.disableLanguages": [
"javascript", "javascriptreact"
],
"eslint.workingDirectories": [
{ "mode": "auto" }
],
"liveServer.settings.donotShowInfoMsg": true,
"workbench.startupEditor": "welcomePage",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"liveServer.settings.donotVerifyTags": true,
"diffEditor.ignoreTrimWhitespace": false,
}
Update: It seems like this is an issue with VSCode doing autoformatting on subdirectories. Once I opened just the subdirectory as the "project root" in VSCode then it started doing all the formatting for me on save. I'm still curious if I can get this working without this workaround.
So, the problem is that vscode-eslint fails to load eslint if it is not in the root or global directory. It should be able to load eslint also from subdirectory. The plugin loading does not seem to be an issue if there is no eslint installed globally. Sorry, something went wrong. The problem with eslint not loaded from a sub directory is know.
[Warn - 11:37:57 AM] Failed to load plugin eslint-react: Cannot find module 'eslint-plugin-eslint-react' Happend while validating p:\mseng\VSCode\Playgrounds\linters\lib est.js This can happen for a couple of reasons: 1. The plugin name is spelled incorrectly in an ESLint configuration file (e.g. .eslintrc). 2.
We are experiencing this as well. We have a node module which requires eslint-plugin-prettier as a dependency and in all our projects it installs eslint-plugin-prettier directly in node_modules (in that case everything works fine), but in one project it installs it in node_modules/OUR_NODE_MODULE/node_modules (in this case it doesn't work).
We upgraded whole project to newer packages, everything works fine, but we found out, that prettier/eslint is not working at all (at least in VS Code, in terminal it works). [Info - 3:11:15 PM] ESLint server stopped. [Info - 3:11:15 PM] ESLint server running in node v10.11.0 [Info - 3:11:15 PM] ESLint server is running.
i had the same issue.
In your root workspace you have a .vscode
folder with a settings.json
.
Add the following:
{
"eslint.workingDirectories": [
"./{PATH_TO_CLIENT}" // replace {PATH_TO_CLIENT} with you own path
]
}
related issue: create-react-app subfolder projects do not lint
VSCode/eslint will not pick newly installed npm packages in the node_modules
directory. After running npm i eslint-plugin-prettier
restart the VSCode workspace. Happens to me consistently, every time I setup a new Javascript/Node/React project and add eslint
& prettier
, using this guide in the order it suggests.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With