Inkstone · blog

VSCode插件build问题和解决方案

522 words 2 min read #VSCode#Extension#npm#Build

📁 项目背景

项目名:annotate-with-git-blame

目标:使用 vsce package 成功打包插件为 .vsix 文件,用于本地或市场发布。


✅ 最终目标达成

你最终成功构建并打包插件,问题全部解决。


🚧 遇到的关键问题与解决方案


❌ 问题 1:

403 Forbidden

,npm 无法安装依赖

报错信息:

403 Forbidden - GET https://registry-npm.firstshare.cn/estraverse...

原因分析:

  • 系统中某处配置使用了错误的私有 npm 镜像(firstshare.cn),即使使用 npm config set registry 切换为官方/淘宝镜像,仍然无效。

初步尝试(未成功):

  • 修改 ~/.npmrc
  • 删除 node_modules 和 package-lock.json
  • 清除缓存:npm cache clean —force

仍然失败。


✅ 解决方法(最终有效)

你使用 nrm 工具切换了 registry 源,成功绕开了强绑定的私有镜像:

npm install -g nrm
# 先尝试官方 npm 源(未成功)
nrm use npm
# 切换淘宝镜像源,成功安装依赖
nrm use taobao

此后执行 npm install 成功完成所有依赖安装。


❌ 问题 2:

ENOTEMPTY

 文件系统错误,安装过程中目录无法重命名

报错信息:

ENOTEMPTY: directory not empty, rename 'node_modules/bl' -> 'node_modules/.bl-*'

原因分析:

  • 上次安装失败后,node_modules 中残留临时目录(如 .bl-xxxx),导致无法重命名或覆盖。

解决方法:

rm -rf node_modules package-lock.json
npm cache clean --force
npm install

✅ 清理后问题解决,依赖安装完成。


✅ 打包成功

使用以下命令打包插件:

vsce package

出现警告:

WARNING: Using '*' activation is usually a bad idea as it impacts performance.

这是 VSCode 对 activationEvents: [”*”] 的性能提示,可忽略或优化为更精细的事件触发(如 onCommand)。

你输入 y 成功继续打包,生成 .vsix 文件。


✅ 总结:关键命令回顾

# 安装 nrm 并切换 registry
npm install -g nrm
nrm use taobao
# 清理环境
rm -rf node_modules package-lock.json
npm cache clean --force
# 安装依赖
npm install
# 打包插件
vsce package

💡 建议

  • 使用 nrm 管理镜像源,避免因隐藏配置源导致的安装问题
  • 遇到 ENOTEMPTY 错误时优先清除临时目录
  • activationEvents 建议改为按需激活(如 onCommand)提升性能