You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何禁用VS Code中注释代码后自动移除依赖导入的功能

问题

在React项目中注释代码时,VS Code会自动移除相关的依赖导入,给调试工作带来极大不便,如何禁用该行为?

解决方案

1. 关闭VS Code内置的自动移除未使用导入功能

在settings.json中添加或修改以下配置项,直接禁用JS/TS的自动清理未使用导入行为:

"javascript.preferences.removeUnusedImports": false,
"typescript.preferences.removeUnusedImports": false

2. 调整ESLint自动修复规则

如果项目使用ESLint并开启了保存时自动修复,需要避免其自动移除未使用导入:

  • 若settings.json中有eslint.codeActionsOnSave.rules配置,移除其中"no-unused-vars": "fix"或"import/no-unused-modules": "fix"这类规则
  • 也可以在项目的.eslintrc配置文件中,将no-unused-vars规则的fixable属性设为false,或者直接降低该规则的级别为警告而非错误

3. 排查Tabnine插件影响

你当前启用了tabnine.experimentalAutoImports,该插件的实验性自动导入功能可能导致此问题,可尝试暂时禁用该选项测试:

"tabnine.experimentalAutoImports": false

提供的settings.json配置

{
  "tabnine.experimentalAutoImports": true,
  // "gitlens.mode.active": "zen",
  "peacock.favoriteColors": [
    {
      "name": "Angular Red",
      "value": "#dd0531"
    },
    {
      "name": "Azure Blue",
      "value": "#007fff"
    },
    {
      "name": "JavaScript Yellow",
      "value": "#f9e64f"
    },
    {
      "name": "Mandalorian Blue",
      "value": "#1857a4"
    },
    {
      "name": "Node Green",
      "value": "#215732"
    },
    {
      "name": "React Blue",
      "value": "#61dafb"
    },
    {
      "name": "Something Different",
      "value": "#832561"
    },
    {
      "name": "Svelte Orange",
      "value": "#ff3d00"
    },
    {
      "name": "Vue Green",
      "value": "#42b883"
    }
  ],
  "gitlens.hovers.currentLine.over": "line",
  "[javascript]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "[typescriptreact]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "[typescript]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "typescript.updateImportsOnFileMove.enabled": "always",
  "[json]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "yaml.schemas": {
    "file:///home/ravi/.vscode/extensions/atlassian.atlascode-2.10.1/resources/schemas/pipelines-schema.json": "bitbucket-pipelines.yml"
  },
  "bracketPairColorizer.depreciation-notice": false,
  "workbench.colorTheme": "Codey's in bed by 10",
  "workbench.editorAssociations": {
    "*.tgz": "default"
  },
  "javascript.updateImportsOnFileMove.enabled": "never",
  "editor.fontFamily": "'Cascade Code', 'Fira Code'",
  "editor.fontLigatures": true,
  "files.autoSave": "afterDelay",
  "git.confirmSync": false,
  "[javascriptreact]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "window.zoomLevel": 1,
  "redhat.telemetry.enabled": true,
  "files.saveConflictResolution": "overwriteFileOnDisk",
  "html.format.indentInnerHtml": true,
  "prettier.embeddedLanguageFormatting": "off",
  "javascript.format.insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": true,
  "typescript.format.insertSpaceAfterTypeAssertion": true,
  "eslint.codeActionsOnSave.mode": "problems",
  "eslint.codeActionsOnSave.rules": null
}

内容的提问来源于stack exchange,提问作者RAVI singh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 08:01:28