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

运行yo code创建VSCode扩展时遇ERR_INVALID_ARG_TYPE错误求助

解决yo code创建VSCode TypeScript扩展时的源路径不存在及类型错误问题

问题重现

按照VSCode官方《你的第一个扩展》教程创建TypeScript扩展,运行yo code完成配置后触发错误:

  • 提示源路径不存在:C:\Users\{username}\AppData\Roaming\npm\node_modules\generator-code\generators\app\templates\ext-command-ts\vscode
  • 抛出类型错误:TypeError [ERR_INVALID_ARG_TYPE]: The "code" argument must be of type number. Received type string ('ERR_ASSERTION')

已尝试重装Node.js、删除C:\Users\{username}\AppData\Roaming\npm目录,问题仍未解决。环境信息:Windows 11、PowerShell终端、Node.js v20.17.0。完整错误日志:

PS C:\Users\{username}> yo code

     _-----_     ╭──────────────────────────╮
    |       |    │   Welcome to the Visual  │
    |--(o)--|    │   Studio Code Extension  │
   `---------´   │        generator!        │
    ( _´U`_ )    ╰──────────────────────────╯
    /___A___\   /
     |  ~  |
   __'.___.'__
 ´   `  |° ´ Y `

? What type of extension do you want to create? New Extension (TypeScript)
? What's the name of your extension? test
? What's the identifier of your extension? test
? What's the description of your extension?
? Initialize a git repository? Yes
? Which bundler to use? unbundled
? Which package manager to use? yarn

Writing in C:\Users\{username}\test...
✖️ An error occured while running code:app#writing
Error code

Trying to copy from a source that does not exist: C:\Users\{username}\AppData\Roaming\npm\node_modules\generator-code\generators\app\templates\ext-command-ts\vscode
TypeError [ERR_INVALID_ARG_TYPE]: The "code" argument must be of type number. Received type string ('ERR_ASSERTION')
    at process.set [as exitCode] (node:internal/bootstrap/node:123:9)
    at process.exit (node:internal/process/per_thread:180:24)
    at onError (C:\Users\{username}\AppData\Roaming\npm\node_modules\yo\lib\cli.js:117:11)
    at C:\Users\{username}\AppData\Roaming\npm\node_modules\yo\lib\cli.js:168:54
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async init (C:\Users\{username}\AppData\Roaming\npm\node_modules\yo\lib\cli.js:168:5)
    at async pre (C:\Users\{username}\AppData\Roaming\npm\node_modules\yo\lib\cli.js:83:3)
    at async C:\Users\{username}\AppData\Roaming\npm\node_modules\yo\lib\cli.js:193:3

解决方案

1. 彻底清理并重装依赖包

  • 全局卸载yo和generator-code:
    npm uninstall -g yo generator-code
    
  • 清理npm缓存:
    npm cache clean --force
    
  • 手动删除残留文件:
    前往C:\Users\{username}\AppData\Roaming\npm\node_modules,删除yo和generator-code文件夹;同时删除C:\Users\{username}\AppData\Roaming\npm-cache目录。

2. 安装兼容版本的generator-code

Node.js v20与最新版generator-code存在兼容性问题,安装稳定兼容版本:

npm install -g yo generator-code@1.8.0

3. 以管理员权限运行终端

Windows文件权限不足可能导致模板文件未完整安装,右键PowerShell选择「以管理员身份运行」后再执行yo code。

4. 切换包管理器为npm

创建扩展时选择npm作为包管理器,避免yarn缓存或配置问题导致文件缺失。

内容的提问来源于stack exchange,提问作者武田淳一

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 22:03:23