本地构建VSCode代码时npm install失败求助
本地构建VSCode源码npm install失败的解决方法
问题描述
构建VSCode源码时执行npm install出现两类错误:
- 清理阶段抛出
ENOTEMPTY错误,无法删除node_modules下多个目录(如windows-foreground-love),报错示例:
npm warn cleanup Failed to remove some directories [ npm warn cleanup [ npm warn cleanup '/Users/nathanwiseman/Documents/GitHub/vscode/node_modules/windows-foreground-love', npm warn cleanup [Error: ENOTEMPTY: directory not empty, rmdir '/Users/.../Documents/GitHub/vscode/node_modules/windows-foreground-love'] { npm warn cleanup errno: -66, npm warn cleanup code: 'ENOTEMPTY', npm warn cleanup syscall: 'rmdir', npm warn cleanup path: '/Users/.../Documents/GitHub/vscode/node_modules/windows-foreground-love' npm warn cleanup } npm warn cleanup ],
@vscode/policy-watcher模块构建失败,报错示例:
npm error code 1 npm error path /Users/.../Documents/GitHub/vscode/node_modules/@vscode/policy-watcher npm error command failed npm error command sh -c node-gyp rebuild
已尝试操作:遵循VSCode贡献指南步骤、切换Node.js 18/22/23版本、删除/重命名报错目录。
环境:MacOS 15.0.1、Apple Silicon M2、ZShell
针对性解决方法
一、解决ENOTEMPTY清理失败问题
- 强制彻底清理依赖后重装
先关闭所有可能占用node_modules的进程(VSCode、Node终端进程等),执行:rm -rf node_modules package-lock.json npm cache clean --force npm install - 用rimraf替代手动删除
全局安装rimraf(专门清理目录的工具,比rm更可靠):
项目根目录执行:npm install -g rimrafrimraf node_modules package-lock.json npm cache clean --force npm install - 检查系统锁定进程
打开「活动监视器」,搜索node/npm相关进程,强制退出后再重试清理安装。
二、解决@vscode/policy-watcher node-gyp构建失败
- 补全Apple Silicon编译依赖
安装Xcode命令行工具并接受许可:xcode-select --install sudo xcodebuild -license accept - 配置node-gyp适配arm64架构
在ZShell中设置环境变量,确保node-gyp用arm64编译:
执行完后重新运行export ARCHS=arm64 export npm_config_arch=arm64 export npm_config_target_arch=arm64npm install - 关联Python 3到node-gyp
确保Python 3已安装,执行:npm config set python python3 - 更新node-gyp版本
全局更新到最新版node-gyp:
再重新执行npm install -g node-gyp@latestnpm install
三、额外排查方案
- 检查ZShell环境变量冲突
打开.zshrc文件,注释掉可能影响Node/npm的环境变量(如自定义PATH、NODE_ENV),重启终端后重试。 - 换用pnpm构建
VSCode支持pnpm,安装后执行:
可规避部分npm的清理和依赖冲突问题。pnpm install
内容的提问来源于stack exchange,提问作者NGWi
相关产品推荐
相关产品推荐

