visual-studio-code - VSCode 加载插件失败找不到模块 'eslint-plugin-prettier'
问题描述
我正在安装eslint
并Prettier
在我的项目中,并试图让自动代码格式化通过 VSCode 工作。当我转到一个 React 文件时,我看到 ESLint 有错误,所以我打开 ESLint 控制台,我看到:
无法加载在“js/.eslintrc.js”中声明的插件“prettier”:找不到模块“eslint-plugin-prettier”
我相信我已经安装了所有必要的模块,这是我的package.json
文件的一部分:
"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"
}
我唯一能想到的是,这是由我的项目目录结构引起的,如下:
/
(some Java stuff out here)
js/
node_modules/
package.json
package-lock.json
.eslintrc.js
.prettierrc
作为参考,这是我的.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: {
}
}
为了进一步参考,这是我settings.json
在 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,
}
更新:这似乎是 VSCode 在子目录上进行自动格式化的问题。一旦我在 VSCode 中将子目录作为“项目根”打开,它就开始在保存时为我进行所有格式化。我仍然很好奇我是否可以在没有这种解决方法的情况下完成这项工作。
解决方案
我遇到过同样的问题。
在您的根工作区中,您有一个.vscode
带有settings.json
.
添加以下内容:
{
"eslint.workingDirectories": [
"./{PATH_TO_CLIENT}" // replace {PATH_TO_CLIENT} with you own path
]
}
推荐阅读
- c - 使用待机模式后无法刷写代码。初始化 ST-LINK 设备时出错。原因:(4) 在目标上找不到设备
- python - 用硒刮擦以运行脚本
- javascript - 如何使用 ngx-scanner 模块角度桌面应用程序?
- reactjs - 如何在 Typescript 中使用 composeWithDevTools 创建存储而不会出现类型错误?
- ios - popViewController 抛出“在键值观察者仍在注册时释放”错误
- python-3.x - 多线程 PyOpenCV 显示
- javascript - 如何解决开始时间为上午 10:55 和结束时间为晚上 10:00 的问题?
- javascript - nodejs中如何根据key和salt进行加密?
- nginx - NGINX 的重定向规则
- c++ - 不带参数的可变模板函数