在Netlify上设置Mailjet lambda

时间:2018-08-23 10:01:37

标签: node.js email netlify mailjet

我正在尝试通过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

1 个答案:

答案 0 :(得分:2)

错误的根本原因来自formidable中的superagent模块依赖性,并且是通过在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; }()));

因此:

  • Critical dependency构建期间的警告
  • 响应Cannot find module.中的错误

解决方案:

在问题中使用临时修复程序,我们将为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配置中来解决此问题。