vscode的TS-JS语言服务频繁崩溃问题

VS Code TS/JS 语言服务频繁崩溃问题的排查与解决

#tech / dev #type / debug #status / evergreen #resource / vscode #media / tool

VS Code TS/JS 语言服务频繁崩溃问题

现状

鼠标悬停代码时类型提示一直显示 loading 状态,右下角弹出错误提示:

The JS/TS language service crashed 5 times in the last 5 Minutes. This may be caused by a plugin contributed by one of these extensions: GitHub.copilot-chat, Vue.volar Please try disabling these extensions before filing an issue against VS Code.

IntelliSense 完全失效,自动补全不工作,CPU 占用异常偏高。

原因

常见触发原因:

  1. TypeScript Server 内存溢出:项目过大(大量 node_modules 或超大类型定义文件),TS Server 超出默认内存限制后崩溃
  2. 扩展冲突:Volar、Copilot Chat 等扩展注入的 TypeScript 插件互相冲突
  3. TypeScript 版本不兼容:VS Code 内置的 TS 版本与项目 node_modules 中的版本差异过大
  4. VS Code 版本 Bug:特定版本的 VS Code 存在 TS Server 稳定性问题
  5. 损坏的 TypeScript 安装node_modules 中的 typescript 包文件损坏

排查步骤

1. 查看 TS Server 日志

Ctrl+Shift+P → "TypeScript: Open TS Server Log"

日志文件通常位于:

Windows: %APPDATA%\Code\logs\<session>\exthost\output_logging_<level>\TypeScript

关注 ErrorOOM 相关条目。

2. 检查 TS Server 版本

Ctrl+Shift+P → "TypeScript: Select TypeScript Version"

确认使用的是工作区版本还是 VS Code 内置版本。

3. 检查扩展影响

禁用可疑扩展后观察是否恢复:

  • Vue.volar(Vue 项目)
  • GitHub.copilot-chat
  • 其他注入 TS 插件的扩展

解决方案

方案一:重启 TS Server(快速修复)

Ctrl+Shift+P → "TypeScript: Restart TS Server"

或点击状态栏的 TypeScript 版本号,选择 “Restart TS Server”。

方案二:更新 VS Code

检查是否有 VS Code 更新,新版本通常修复了 TS Server 的已知问题。

方案三:增大 TS Server 内存限制

在 VS Code settings.json 中添加:

{
  "typescript.tsserver.maxTsServerMemory": 4096
}

默认值为 3072MB(3GB),可增大到 4096 或 8192。

方案四:排除大型目录

减少 TS Server 需要处理的文件量:

// .vscode/settings.json
{
  "files.exclude": {
    "**/node_modules": true,
    "**/dist": true,
    "**/.next": true,
    "**/build": true
  },
  "search.exclude": {
    "**/node_modules": true,
    "**/dist": true,
    "**/*.min.js": true
  },
  "typescript.tsserver.watchOptions": {
    "excludeDirectories": [
      "**/node_modules",
      "**/dist",
      "**/.next"
    ]
  }
}

方案五:切换 TypeScript 版本

Ctrl+Shift+P → "TypeScript: Select TypeScript Version" → "Use Workspace Version"

或在 settings.json 中:

{
  "typescript.tsdk": "node_modules/typescript/lib"
}

方案六:清理并重装依赖

Remove-Item -Path "node_modules" -Recurse -Force
pnpm install
# 或 npm install

预防措施

  • 项目中 .vscode/settings.json 配置好 excludeDirectories
  • 大型 monorepo 中使用 TypeScript project references 减少单次分析量
  • 定期更新 VS Code 和 TypeScript 版本
  • 避免同时安装多个注入 TS Server 的扩展

信息参考

创建于 2025/1/1 更新于 2026/5/27