dicebear/api项目出现async_hooks解析错误,求platform: 'node'添加位置
解决wrangler dev启动dicebear/api时的"async_hooks"解析错误
问题详情
运行wrangler dev命令启动dicebear/api项目时,出现以下错误:
$ wrangler dev ⛅️ wrangler 2.7.1 ------------------- ▲ [警告] 为内置模块和全局对象启用Node.js兼容模式。这是实验性功能,存在严重的权衡问题。 ⬣ 监听地址:http://0.0.0.0:8787 - http://127.0.0.1:8787 - http://192.168.0.112:8787 ╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ [b] 打开浏览器, [d] 打开开发者工具, [l] 开启本地模式, [c] 清空控制台, [x] 退出 │ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ ✘ [错误] 无法解析"async_hooks" node_modules/fastify/lib/contentTypeParser.js:3:34: 3 │ const { AsyncResource } = require('async_hooks') ╵ ~~~~~~~~~~~~~ 包"async_hooks"未在文件系统中找到,但它是Node.js的内置模块。你是否正在为Node.js打包?可以使用"platform: 'node'"来解决,这会消除此错误。 ✘ [错误] 构建失败,共1个错误: node_modules/fastify/lib/contentTypeParser.js:3:34: 错误:无法解析"async_hooks"
系统提示添加platform: 'node'解决,但不清楚配置位置。
解决步骤
- 打开项目根目录下的
wrangler.toml配置文件 - 在文件中找到或新增
[build]配置区块,添加platform = "node"配置,示例如下:
[build] platform = "node"
- 保存文件后重新运行
wrangler dev命令即可。
原理说明
async_hooks是Node.js专属的内置模块,Cloudflare Workers默认运行环境为类浏览器的Worker环境,不支持这类模块。设置platform = "node"会让wrangler切换到Node.js打包模式,从而正确识别并处理该内置模块。
内容的提问来源于stack exchange,提问作者Nishith P Shetty
相关产品推荐
相关产品推荐

