Cloudflare Workers:找不到模块:错误:无法解析“~/node_modules/nodemailer/lib/sendmail-transport"中的“child_process" [英] Cloudflare Workers: Module not found: Error: Can't resolve 'child_process' in '~/node_modules/nodemailer/lib/sendmail-transport'
问题描述
我在 Cloudflare Worker 中的 Nodemailer 包中遇到以下错误,并想记录我的发现.
I was getting the following error with the Nodemailer package in a Cloudflare Worker and wanted to document what I found.
Module not found: Error: Can't resolve 'child_process' in '~/node_modules/nodemailer/lib/sendmail-transport'
推荐答案
在使用 Cloudflare Workers 进行开发时,我在使用 Nodemailer 包时遇到以下错误:
While developing with Cloudflare Workers, I was getting the following error with the Nodemailer package:
Module not found: Error: Can't resolve 'child_process' in '~/node_modules/nodemailer/lib/sendmail-transport'
当我研究错误时,我发现Webpack试图为客户端捆绑只能在服务器中使用的东西.请参阅 https://github.com/webpack/webpack/issues/744.
When I researched the error, I found that Webpack is trying to bundle things for the client that can only be used in the server. See https://github.com/webpack/webpack/issues/744.
一种可能的解决方案是添加 externals
属性到我的 Worker 的自定义webpack.config.js
文件 并包含所有 Webpack 无法解析的模块.请参阅 https://github.com/webpack/webpack/issues/744#issuecomment-320437402.
One possible solution was to add the externals
property to a custom webpack.config.js
file for my Worker and include all the modules that cannot be resolved by Webpack. See https://github.com/webpack/webpack/issues/744#issuecomment-320437402.
所以我的 webpack.config.js
文件看起来像这样:
So my webpack.config.js
file would look like this:
module.exports = {
target: "webworker",
entry: "./index.js",
externals: [
"child_process",
"dns",
"fs",
"net",
"tls",
]
}
但是,我发现您不能在 Cloudflare Worker 中使用像 Nodemailer 这样的包.Cloudflare Workers 没有 Node 环境,因此您不能像在具有 Node 环境的服务器端平台中那样使用 Node 包.因此,上面的 Webpack 配置无论如何都不会给您带来多大好处,因为您仍然无法使用 Nodemailer 从 Cloudflare Worker 发送电子邮件.但是,通常有其他方法可以在 Cloudflare Worker 中使用 Node 包或其他 Node 功能.例如,为了从 Worker 发送电子邮件,您必须使用 REST API 而不是 Node 包:
However, I found out that you can't use packages like Nodemailer in a Cloudflare Worker. Cloudflare Workers do not have a Node environment, so you can't use Node packages like you would in a server-side platform that has a Node environment. So the Webpack configurations above won't do you much good anyway because you still can't use Nodemailer to send email from a Cloudflare Worker. However, there are often alternative ways to use Node packages or other Node features in a Cloudflare Worker. For example, in order to send email from a Worker you have to use a REST API instead of a Node package:
这篇关于Cloudflare Workers:找不到模块:错误:无法解析“~/node_modules/nodemailer/lib/sendmail-transport"中的“child_process"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!