gpt4 book ai didi

node.js - 在 Netlify 上设置 Mailjet lambda

转载 作者:太空宇宙 更新时间:2023-11-04 03:02:18 25 4
gpt4 key购买 nike

我正在尝试通过 Netlify Lambda 发送电子邮件,Lambda 在没有 Mailjet 集成的情况下也可以顺利工作,并且我已经在 Node 测试脚本中尝试了 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 转译引起的。

具体来说,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; }()));

因此:

  • 构建期间出现关键依赖项警告
  • 响应错误找不到模块。

解决方案:

使用问题中的临时修复,我们将为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-lambdaNetlify Lambda CLI用于帮助捆绑 netlify 部署功能的工具。如果您不使用 CLI,那么您可以通过将该插件添加到您的 webpack 配置中来解决此问题。

关于node.js - 在 Netlify 上设置 Mailjet lambda,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51983191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com