记一次 Wrangler CLI 创建 Cloudflare Worker 模板启动失败原因

因为想用 Worker API 绑定 Cloudflare R2 便于使用,所有遵循官方文档创建模板。

但是遇上了一个莫名其妙的 Bug 报错...

报错过程

使用脚手架创建模板

npm create cloudflare@latest r2-worker

按步骤创建好后,运行 npm run dev 遭遇一下报错:

Bug

通过查看日志文件详细信息如下:

--- 2024-06-30T19:28:07.425Z debug
🪵  Writing logs to "C:\Users\...\.wrangler\logs\wrangler-2024-06-30_19-28-07_302.log"
---

--- 2024-06-30T19:28:07.425Z debug
Failed to load .env file ".env": Error: ENOENT: no such file or directory, open 'C:\Cycles of Seasons\100 - Virtues\110 - Demiourgia\test-wrangler\hello-world-js\.env'
    at Object.openSync (node:fs:573:18)
    at Object.readFileSync (node:fs:452:35)
    at tryLoadDotEnv (C:\...\test-wrangler\hello-world-js\node_modules\wrangler\wrangler-dist\cli.js:158768:72)
    at loadDotEnv (C:\...\test-wrangler\hello-world-js\node_modules\wrangler\wrangler-dist\cli.js:158777:12)
    at C:\...\test-wrangler\hello-world-js\node_modules\wrangler\wrangler-dist\cli.js:202740:20
    at C:\...\test-wrangler\hello-world-js\node_modules\wrangler\wrangler-dist\cli.js:162911:16
    at maybeAsyncResult (C:\...\test-wrangler\hello-world-js\node_modules\wrangler\wrangler-dist\cli.js:161132:44)
    at C:\...\test-wrangler\hello-world-js\node_modules\wrangler\wrangler-dist\cli.js:162910:14
    at C:\...\test-wrangler\hello-world-js\node_modules\wrangler\wrangler-dist\cli.js:161119:22
    at Array.reduce (<anonymous>) {
  errno: -4058,
  code: 'ENOENT',
  syscall: 'open',
  path: 'C:\\...\\test-wrangler\\hello-world-js\\.env'
}
---

--- 2024-06-30T19:28:07.432Z log

 ⛅️ wrangler 3.62.0
[38;2;255;136;0m-------------------[39m

---

--- 2024-06-30T19:28:07.450Z debug
Metrics dispatcher: Posting data {"type":"event","name":"run dev","properties":{"local":true,"usesTypeScript":false}}
---

--- 2024-06-30T19:28:07.455Z debug
Failed to load .env file "C:\...\test-wrangler\hello-world-js\.dev.vars": Error: ENOENT: no such file or directory, open 'C:\...\test-wrangler\hello-world-js\.dev.vars'
    at Object.openSync (node:fs:573:18)
    at Object.readFileSync (node:fs:452:35)
    at tryLoadDotEnv (C:\...\test-wrangler\hello-world-js\node_modules\wrangler\wrangler-dist\cli.js:158768:72)
    at loadDotEnv (C:\...\test-wrangler\hello-world-js\node_modules\wrangler\wrangler-dist\cli.js:158777:12)
    at getVarsForDev (C:\...\test-wrangler\hello-world-js\node_modules\wrangler\wrangler-dist\cli.js:200152:18)
    at getBindings (C:\...\test-wrangler\hello-world-js\node_modules\wrangler\wrangler-dist\cli.js:207740:10)
    at getBindingsAndAssetPaths (C:\...\test-wrangler\hello-world-js\node_modules\wrangler\wrangler-dist\cli.js:207621:20)
    at getDevReactElement (C:\...\test-wrangler\hello-world-js\node_modules\wrangler\wrangler-dist\cli.js:207276:40)
    at startDev (C:\...\test-wrangler\hello-world-js\node_modules\wrangler\wrangler-dist\cli.js:207343:60)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  errno: -4058,
  code: 'ENOENT',
  syscall: 'open',
  path: 'C:\\...\\test-wrangler\\hello-world-js\\.dev.vars'
}
---

--- 2024-06-30T19:28:07.571Z log
[2m⎔ Starting local server...[22m
---

--- 2024-06-30T19:28:07.580Z debug
*** Received structured exception #0xc0000005: access violation; stack: 7ffcf4ed2f57 7ff7e9e1643b 7ff7e9e16503 7ff7e9e0588c 7ff7e9e05837 7ff7e9649c1e 7ff7e9649f2f 7ff7e8531ad6 7ff7e85318ba 7ff7e97cb6ef 7ff7e97d28a6 7ff7e97cbc0c 7ff7e97d28a6 7ff7e97c957c 7ff7e8521551 7ff7eaf00f7f 7ffd07c2257c 7ffd0824af27
---

--- 2024-06-30T19:28:07.632Z debug
*** Received structured exception #0xc0000005: access violation; stack: 7ffcf4ed2f57 7ff7e9e1643b 7ff7e9e16503 7ff7e9e0588c 7ff7e9e05837 7ff7e9649c1e 7ff7e9649f2f 7ff7e8531ad6 7ff7e85318ba 7ff7e97cb6ef 7ff7e97d28a6 7ff7e97cbc0c 7ff7e97d28a6 7ff7e97c957c 7ff7e8521551 7ff7eaf00f7f 7ffd07c2257c 7ffd0824af27
---

明显可以看到因缺少 .env.dev.vars 报错,但这不是重点,即使添加了文件依旧报错。

问题出在这:

--- 2024-06-30T19:28:07.580Z debug
*** Received structured exception #0xc0000005: access violation; stack: 7ffcf4ed2f57 7ff7e9e1643b 7ff7e9e16503 7ff7e9e0588c 7ff7e9e05837 7ff7e9649c1e 7ff7e9649f2f 7ff7e8531ad6 7ff7e85318ba 7ff7e97cb6ef 7ff7e97d28a6 7ff7e97cbc0c 7ff7e97d28a6 7ff7e97c957c 7ff7e8521551 7ff7eaf00f7f 7ffd07c2257c 7ffd0824af27
---

--- 2024-06-30T19:28:07.632Z debug
*** Received structured exception #0xc0000005: access violation; stack: 7ffcf4ed2f57 7ff7e9e1643b 7ff7e9e16503 7ff7e9e0588c 7ff7e9e05837 7ff7e9649c1e 7ff7e9649f2f 7ff7e8531ad6 7ff7e85318ba 7ff7e97cb6ef 7ff7e97d28a6 7ff7e97cbc0c 7ff7e97d28a6 7ff7e97c957c 7ff7e8521551 7ff7eaf00f7f 7ffd07c2257c 7ffd0824af27
---

这个报错有点不明所以,只能去官方 Github 仓库 woker-sdk 下的 issues 寻找解决方法。

解决方法

经过一番查找,最终在 #6170 下找到了解决方法。

该报错可能是由于电脑的 Microsoft Visual C++ 与 wrangler 的依赖包版本不兼容导致的,大多发生在 Windows 11 系统,所以有两种解决方法。


使用更低版本的 wrangler

经测试,使用 3.57.1 版本的 wrangler,可以正常运行。可以直接运行下面的命令来降级:

npm uninstall wrangler && npm install [email protected] -D

不过该方法指标不治本,只能用于临时应急,不太推荐。


更新 Microsoft Visual C++ 到最新版本

wrangler 引用的是下面两个版本的库,所以我们只需要更新它们即可。

Microsoft Visual C++

图中是 wrangler 能够正常运行的版本,也是我现在的最新版本,建议更新时选择该版本及以上版本。

这里推荐直接去“官方下载地址”下载最新软件包更新。