使用Yeoman Botbuilder生成器创建机器人启动报错求助
Botbuilder机器人启动报错解决(Node.js 18 + restify冲突)
问题场景
用Yeoman Botbuilder生成器创建机器人,已通过npm install -g yo generator-botbuilder完成安装,配置后执行npm start或node index.js启动时出现以下错误:
C:\Users\Anant\Documents\Bots\anant>node index.js C:\Users\Anant\Documents\Bots\anant\node_modules\restify\lib\request.js:848 Request.prototype.closed = function closed() { ^ TypeError: Cannot set property closed of #<Readable> which has only a getter at patch (C:\Users\Anant\Documents\Bots\anant\node_modules\restify\lib\request.js:848:30) at Object.<anonymous> (C:\Users\Anant\Documents\Bots\anant\node_modules\restify\lib\server.js:33:1) at Module._compile (node:internal/modules/cjs/loader:1159:14) at Module._extensions..js (node:internal/modules/cjs/loader:1213:10) at Module.load (node:internal/modules/cjs/loader:1037:32) at Module._load (node:internal/modules/cjs/loader:878:12) at Module.require (node:internal/modules/cjs/loader:1061:19) at require (node:internal/modules/cjs/helpers:103:18) at Object.<anonymous> (C:\Users\Anant\Documents\Bots\anant\node_modules\restify\lib\index.js:10:14) at Module._compile (node:internal/modules/cjs/loader:1159:14) Node.js v18.12.1
已尝试更新所有包、重新安装windows-build-tools(提示已废弃并集成到Node),问题仍未解决。
错误原因
Node.js 18对Stream模块的API做了调整,Readable流的closed属性改为只读,但Yeoman生成器依赖的旧版restify库仍试图给这个属性赋值,导致兼容性冲突。
解决方案
1. 降级Node.js到16.x版本(最快捷)
Node.js 16.x与旧版restify完全兼容,用版本管理器切换:
- 安装nvm(Node版本管理器,Windows选nvm-windows,Linux/macOS用nvm)
- 执行命令:
nvm install 16 nvm use 16 - 切换完成后重新运行
npm start即可。
2. 升级项目中的restify到兼容版本
手动替换项目里的restify为支持Node 18的新版本:
- 卸载旧版restify:
npm uninstall restify - 安装最新稳定版:
npm install restify@latest - 注意:升级后可能需要调整机器人代码,比如restify的路由定义、服务器启动逻辑等,因为新版本API有变动。
3. 使用Bot Framework官方最新模板
Yeoman的generator-botbuilder维护可能滞后,改用微软官方最新的Bot Framework模板,这些模板已适配Node.js 18及以上版本,避免依赖旧版restify。
内容的提问来源于stack exchange,提问作者Anantashayana
相关产品推荐
相关产品推荐

