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

VSCode中npm run test任务遭yarn注入,如何恢复npm执行?

问题描述

我在项目根目录通过npm run test:backend运行后端测试,但今日突然发现VSCode的任务命令被注入了yarn,执行时提示:

Executing task: yarn run test:backend 

/bin/bash: yarn: command not found

在集成终端直接运行npm run test:backend一切正常,但「运行任务」功能却强制要求使用yarn,推测是VSCode自动更新所致。我的tasks.json配置如下:

{
    "type": "npm",
    "script": "test:backend",
    "problemMatcher": [],
    "label": "npm test:backend",
    "detail": "backend tests",
    "group": "build"
}

请问如何恢复用npm run执行任务?

解决方法
  • 在任务配置中强制指定包管理器
    直接在tasks.json的对应任务里添加"packageManager": "npm"字段,明确指定使用npm执行该任务,修改后的配置如下:

    {
        "type": "npm",
        "script": "test:backend",
        "problemMatcher": [],
        "label": "npm test:backend",
        "detail": "backend tests",
        "group": "build",
        "packageManager": "npm"
    }
    
  • 检查VSCode全局包管理器设置
    打开VSCode设置(快捷键Ctrl+,),搜索npm.packageManager,确认该选项的值为npm。如果之前被自动改为yarn,改回npm即可全局生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 19:52:03