首页 > 解决方案 > VSCode 加载插件失败找不到模块 'eslint-plugin-prettier'

问题描述

我正在安装eslintPrettier在我的项目中,并试图让自动代码格式化通过 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 中将子目录作为“项目根”打开,它就开始在保存时为我进行所有格式化。我仍然很好奇我是否可以在没有这种解决方法的情况下完成这项工作。

标签: visual-studio-codeeslintprettiereslintrc

解决方案


我遇到过同样的问题。

在您的根工作区中,您有一个.vscode带有settings.json.

添加以下内容:

{
  "eslint.workingDirectories": [
    "./{PATH_TO_CLIENT}" // replace {PATH_TO_CLIENT} with you own path
  ]
}

相关问题:create-react-app 子文件夹项目不 lint


推荐阅读