在 Netlify 上设置 Mailjet lambda
Set up a Mailjet lambda on Netlify
我正在尝试通过 Netlify Lambda 发送电子邮件,lambda 在没有 Mailjet 集成的情况下运行顺利,我已经在节点测试脚本中尝试了 Mailjet 集成
const { MJ_USER, MJ_PASSWORD } = process.env;
const mailjet = require('node-mailjet').connect(MJ_USER, MJ_PASSWORD)
exports.handler = async (event, context) => {
if (event.httpMethod !== "POST") {
return { statusCode: 405, body: "Method Not Allowed" };
}
const data = JSON.parse(event.body)
const msg = { "Messages":[
{
"From": {
"Email": "sender@gmail.com",
"Name": "Paul"
},
"To": [
{
"Email": "receiver@gmail.com",
"Name": "Emma"
}
],
"TemplateID": 511035,
"TemplateLanguage": true,
"Subject": "Test mail",
"Variables": {
"input": "Test"
}
}
]}
mailjet.post("send", {'version': 'v3.1'}).request(msg)
.then((result) => {
return{ statusCode: 200, body: result.body}
}).catch((err) => {
return err
})
}
导致
{"errorMessage":"i is not a function","errorType":"TypeError","stackTrace":["n (/var/task/hello.js:1:220)","/var/task/hello.js:1:1019","Object.<anonymous> (/var/task/hello.js:1:1030)","Module._compile (module.js:652:30)","Object.Module._extensions..js (module.js:663:10)","Module.load (module.js:565:32)","tryModuleLoad (module.js:505:12)","Function.Module._load (module.js:497:3)","Module.require (module.js:596:17)"]}
该模块似乎没有正确加载,但我找不到解决方法
更新:
我在 Netlify 中找到了更多日志:
2:27:00 PM: Critical dependency: require function is used in a way in which dependencies cannot be statically extracted
2:27:00 PM: @ /opt/build/repo/node_modules/formidable/lib/index.js
2:27:00 PM: @ /opt/build/repo/node_modules/superagent/lib/node/index.js
2:27:00 PM: @ /opt/build/repo/node_modules/node-mailjet/mailjet-client.js
2:27:00 PM: @ /opt/build/repo/node_modules/node-mailjet/index.js
2:27:00 PM: @ ./hello.js
错误的根本原因来自于superagent
中的formidable
模块依赖,是netlify函数中使用webpack的babel transpile引起的
具体来说,issue can be tracked here
if (global.GENTLY) require = GENTLY.hijack(require);
转译代码:
var require;if (global.GENTLY) require = GENTLY.hijack(!(function webpackMissingModule() { var e = new Error("Cannot find module \".\""); e.code = 'MODULE_NOT_FOUND'; throw e; }()));
因此:
Critical dependency
构建期间警告
- 响应错误
Cannot find module.
解决方案:
使用 temporary fix in the issue 我们将为 global.GENTLY
添加一个全局设置,因此 webpack 会将值设置为 false
而不会注意到该行的其余部分。此行已确认仅用于测试目的。
创建一个文件来保存名为 webpack.config.js
的额外 webpack 配置设置,并将其放入项目的根目录中。
webpack.config.js
var webpack = require('webpack')
module.exports = {
plugins: [
new webpack.DefinePlugin({ "global.GENTLY": false })
]
}
现在更改函数的构建命令以包含配置作为 webpack 的附加设置。
旧命令
netlify-lambda build <folder>
新命令
netlify-lambda build <folder> -c webpack.config.js
如果一切顺利,您应该不会在模块上出现警告或运行时故障。
注意: 命令 netlify-lambda
是 Netlify Lambda CLI 工具,用于帮助捆绑您的 netlify 函数部署。如果你不使用 CLI,那么你可以通过将插件添加到你的 webpack 配置来解决这个问题。
我正在尝试通过 Netlify Lambda 发送电子邮件,lambda 在没有 Mailjet 集成的情况下运行顺利,我已经在节点测试脚本中尝试了 Mailjet 集成
const { MJ_USER, MJ_PASSWORD } = process.env;
const mailjet = require('node-mailjet').connect(MJ_USER, MJ_PASSWORD)
exports.handler = async (event, context) => {
if (event.httpMethod !== "POST") {
return { statusCode: 405, body: "Method Not Allowed" };
}
const data = JSON.parse(event.body)
const msg = { "Messages":[
{
"From": {
"Email": "sender@gmail.com",
"Name": "Paul"
},
"To": [
{
"Email": "receiver@gmail.com",
"Name": "Emma"
}
],
"TemplateID": 511035,
"TemplateLanguage": true,
"Subject": "Test mail",
"Variables": {
"input": "Test"
}
}
]}
mailjet.post("send", {'version': 'v3.1'}).request(msg)
.then((result) => {
return{ statusCode: 200, body: result.body}
}).catch((err) => {
return err
})
}
导致
{"errorMessage":"i is not a function","errorType":"TypeError","stackTrace":["n (/var/task/hello.js:1:220)","/var/task/hello.js:1:1019","Object.<anonymous> (/var/task/hello.js:1:1030)","Module._compile (module.js:652:30)","Object.Module._extensions..js (module.js:663:10)","Module.load (module.js:565:32)","tryModuleLoad (module.js:505:12)","Function.Module._load (module.js:497:3)","Module.require (module.js:596:17)"]}
该模块似乎没有正确加载,但我找不到解决方法
更新:
我在 Netlify 中找到了更多日志:
2:27:00 PM: Critical dependency: require function is used in a way in which dependencies cannot be statically extracted
2:27:00 PM: @ /opt/build/repo/node_modules/formidable/lib/index.js
2:27:00 PM: @ /opt/build/repo/node_modules/superagent/lib/node/index.js
2:27:00 PM: @ /opt/build/repo/node_modules/node-mailjet/mailjet-client.js
2:27:00 PM: @ /opt/build/repo/node_modules/node-mailjet/index.js
2:27:00 PM: @ ./hello.js
错误的根本原因来自于superagent
中的formidable
模块依赖,是netlify函数中使用webpack的babel transpile引起的
具体来说,issue can be tracked here
if (global.GENTLY) require = GENTLY.hijack(require);
转译代码:
var require;if (global.GENTLY) require = GENTLY.hijack(!(function webpackMissingModule() { var e = new Error("Cannot find module \".\""); e.code = 'MODULE_NOT_FOUND'; throw e; }()));
因此:
Critical dependency
构建期间警告- 响应错误
Cannot find module.
解决方案:
使用 temporary fix in the issue 我们将为 global.GENTLY
添加一个全局设置,因此 webpack 会将值设置为 false
而不会注意到该行的其余部分。此行已确认仅用于测试目的。
创建一个文件来保存名为 webpack.config.js
的额外 webpack 配置设置,并将其放入项目的根目录中。
webpack.config.js
var webpack = require('webpack')
module.exports = {
plugins: [
new webpack.DefinePlugin({ "global.GENTLY": false })
]
}
现在更改函数的构建命令以包含配置作为 webpack 的附加设置。
旧命令
netlify-lambda build <folder>
新命令
netlify-lambda build <folder> -c webpack.config.js
如果一切顺利,您应该不会在模块上出现警告或运行时故障。
注意: 命令 netlify-lambda
是 Netlify Lambda CLI 工具,用于帮助捆绑您的 netlify 函数部署。如果你不使用 CLI,那么你可以通过将插件添加到你的 webpack 配置来解决这个问题。