代码之家  ›  专栏  ›  技术社区  ›  kwn

在Netlify上设置Mailjet lambda

  •  0
  • kwn  · 技术社区  · 6 年前

    我试图通过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)"]}
    

    模块似乎加载不正确,但我找不到解决方法

    更新 :

    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 回复  |  直到 6 年前
        1
  •  4
  •   talves    6 年前

    错误的根本原因来自 formidable superagent 是由于在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.

    解决方案:

    使用 暂时的 解决此问题我们将为 global.GENTLY 所以webpack将值设置为 false 不去关注其他的事情。这条线被证实只作测试之用。

    创建一个文件以保存调用 webpack.config.js 把项目的根源放进去。

    webpack.config.js网站

    var webpack = require('webpack')
    
    module.exports = {
      plugins: [
        new webpack.DefinePlugin({ "global.GENTLY": false })
      ]
    }
    

    现在更改函数的build命令,将配置作为webpack的附加设置包括在内。

    旧命令

    netlify-lambda build <folder>
    

    新命令

    netlify-lambda build <folder> -c webpack.config.js
    

    如果一切顺利,则不应在模块上出现警告或运行时故障。

    注: 指挥部 netlify-lambda Netlify Lambda CLI 用于帮助将功能打包以供netlify部署的工具。如果不使用CLI,那么可以通过将插件添加到webpack配置中来解决此问题。

    推荐文章