我做了什么:
在 Web ui 上创建工作线程并使用
wrangler init --from-dash
在本地进行初始化
我用
npm i twilio
, 安装了 twilio
这是我尝试运行的代码(index.js)
export default {
async fetch(request, ctx) {
const accountSid = "<>";
const authToken = "<>";
const client = require('twilio')(accountSid, authToken);
client.messages
.create({
from: 'whatsapp:<>',
body: 'Hello there finally!',
to: 'whatsapp:<>'
});
return new Response("test");
},
};
当我运行 npm start 时,出现以下错误
✘ [ERROR] Could not resolve "events"
node_modules/agent-base/dist/src/index.js:5:25:
5 │ const events_1 = require("events");
╵ ~~~~~~~~
The package "events" wasn't found on the file system but is built into node.
Add "node_compat = true" to your wrangler.toml file and make sure to prefix the module name with "node:" to enable Node.js compatibility.
✘ [ERROR] Could not resolve "buffer"
node_modules/buffer-equal-constant-time/index.js:3:21:
3 │ var Buffer = require('buffer').Buffer; // browserify
╵ ~~~~~~~~
The package "buffer" wasn't found on the file system but is built into node.
Add "node_compat = true" to your wrangler.toml file and make sure to prefix the module name with "node:" to enable Node.js compatibility.
✘ [ERROR] Could not resolve "net"
node_modules/https-proxy-agent/dist/agent.js:15:38:
15 │ const net_1 = __importDefault(require("net"));
╵ ~~~~~
The package "net" wasn't found on the file system but is built into node.
Add "node_compat = true" to your wrangler.toml file and make sure to prefix the module name with "node:" to enable Node.js compatibility.
✘ [ERROR] Could not resolve "tls"
node_modules/https-proxy-agent/dist/agent.js:16:38:
16 │ const tls_1 = __importDefault(require("tls"));
╵ ~~~~~
The package "tls" wasn't found on the file system but is built into node.
Add "node_compat = true" to your wrangler.toml file and make sure to prefix the module name with "node:" to enable Node.js compatibility.
✘ [ERROR] Could not resolve "url"
node_modules/https-proxy-agent/dist/agent.js:17:38:
17 │ const url_1 = __importDefault(require("url"));
╵ ~~~~~
The package "url" wasn't found on the file system but is built into node.
Add "node_compat = true" to your wrangler.toml file and make sure to prefix the module name with "node:" to enable Node.js compatibility.
✘ [ERROR] Could not resolve "assert"
node_modules/https-proxy-agent/dist/agent.js:18:41:
18 │ const assert_1 = __importDefault(require("assert"));
╵ ~~~~~~~~
The package "assert" wasn't found on the file system but is built into node.
Add "node_compat = true" to your wrangler.toml file and make sure to prefix the module name with "node:" to enable Node.js compatibility.
✘ [ERROR] Could not resolve "crypto"
node_modules/jsonwebtoken/sign.js:12:65:
12 │ ...KeyObject, createSecretKey, createPrivateKey } = require('crypto')
╵ ~~~~~~~~
The package "crypto" wasn't found on the file system but is built into node.
Add "node_compat = true" to your wrangler.toml file and make sure to prefix the module name with "node:" to enable Node.js compatibility.
✘ [ERROR] Could not resolve "crypto"
node_modules/jsonwebtoken/verify.js:9:62:
9 │ ... {KeyObject, createSecretKey, createPublicKey} = require("crypto");
╵ ~~~~~~~~
The package "crypto" wasn't found on the file system but is built into node.
Add "node_compat = true" to your wrangler.toml file and make sure to prefix the module name with "node:" to enable Node.js compatibility.
✘ [ERROR] Could not resolve "crypto"
node_modules/jwa/index.js:3:21:
3 │ var crypto = require('crypto');
╵ ~~~~~~~~
The package "crypto" wasn't found on the file system but is built into node.
Add "node_compat = true" to your wrangler.toml file and make sure to prefix the module name with "node:" to enable Node.js compatibility.
✘ [ERROR] Could not resolve "util"
node_modules/jwa/index.js:5:19:
5 │ var util = require('util');
╵ ~~~~~~
The package "util" wasn't found on the file system but is built into node.
Add "node_compat = true" to your wrangler.toml file and make sure to prefix the module name with "node:" to enable Node.js compatibility.
✘ [ERROR] Could not resolve "stream"
node_modules/jws/lib/data-stream.js:3:21:
3 │ var Stream = require('stream');
╵ ~~~~~~~~
The package "stream" wasn't found on the file system but is built into node.
Add "node_compat = true" to your wrangler.toml file and make sure to prefix the module name with "node:" to enable Node.js compatibility.
✘ [ERROR] Could not resolve "util"
node_modules/jws/lib/data-stream.js:4:19:
4 │ var util = require('util');
╵ ~~~~~~
The package "util" wasn't found on the file system but is built into node.
Add "node_compat = true" to your wrangler.toml file and make sure to prefix the module name with "node:" to enable Node.js compatibility.
节点版本 - v20.11.0
npm 版本 - 10.2.4
(使用官网的tar安装node js)
操作系统 - 流行操作系统 21.04
一些专为 Node 设计的库需要启用节点兼容性 Polyfill 才能在 Cloudflare Workers 中工作。
https://developers.cloudflare.com/workers/wrangler/configuration/#node-compatibility
在
wrangler.toml
文件顶部添加:
node_compat = true