VSCode 无法加载插件找不到模块“eslint-plugin-prettier”

Bra*_*ing 11 eslint visual-studio-code prettier eslintrc

我正在安装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"
  }
Run Code Online (Sandbox Code Playgroud)

我唯一能想到的是这是由我的项目目录结构引起的,如下所示:

/
(some Java stuff out here)
js/
  node_modules/
  package.json
  package-lock.json
  .eslintrc.js
  .prettierrc
Run Code Online (Sandbox Code Playgroud)

作为参考,这是我的.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: {
    }
  }
Run Code Online (Sandbox Code Playgroud)

为了进一步参考,这是我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,
}
Run Code Online (Sandbox Code Playgroud)

更新:这似乎是 VSCode 在子目录上进行自动格式化的问题。一旦我在 VSCode 中打开子目录作为“项目根目录”,它就开始在保存时为我进行所有格式化。我仍然很好奇是否可以在没有此解决方法的情况下使其正常工作。

Jul*_*lez 22

我遇到过同样的问题。

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

添加以下内容:

{
  "eslint.workingDirectories": [
    "./{PATH_TO_CLIENT}" // replace {PATH_TO_CLIENT} with you own path
  ]
}
Run Code Online (Sandbox Code Playgroud)

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

  • 杰出的。我的项目位于一个子文件夹中,这修复了它。 (2认同)

Bri*_*den 5

VSCode/eslint 不会选择node_modules目录中新安装的 npm 包。运行后npm i eslint-plugin-prettier重新启动 VSCode 工作区。每次我设置一个新的 Javascript/Node/React 项目并添加eslint&时prettier,都会发生这种情况,按照本指南建议的顺序使用。