【问题标题】:How can i use package management in NodeJS Azure Functions?如何在 NodeJS Azure Functions 中使用包管理?
【发布时间】:2021-03-29 00:03:06
【问题描述】:

我正在尝试将此示例项目构建为 Azure 函数,它需要一些包 https://github.com/OfficeDev/O365-Nodejs-Microsoft-Graph-App-only

我可以看到我可以使用 https://azure.microsoft.com/en-us/documentation/articles/functions-reference/#nodejavascript-api 在 NodeJS Azure Functions 中使用数据包管理

我试过用这个

var request = require('request');

此声明

You can include packages in your function directory (i.e. via npm install) and then import them to your function in the usual ways (i.e. via require('packagename'))

所以我在其中创建了一个 project.json,就像 C# Azure 函数使用的一样:

{
  "frameworks": {
    "net46":{
      "dependencies": {
        "chalk": "^1.1.1",
        "q": "^1.4.1",
        "request": "^2.67.0"
      }
    }
  }
}

得到这个错误

2016-04-06T19:49:42.026 Exception while executing function: Functions.MicrosoftGraphWebHookNode. mscorlib: One or more errors occurred. Error: Cannot find module 'request'
    at Function.Module._resolveFilename (module.js:339:15)
    at Function.Module._load (module.js:290:25)
    at Module.require (module.js:367:17)
    at require (internal/module.js:16:19)
    at Object.<anonymous> (D:\home\site\wwwroot\MicrosoftGraphWebHookNode\index.js:1:77)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Module.require (module.js:367:17).

project.json 应该工作吗?

【问题讨论】:

    标签: azure-functions


    【解决方案1】:

    您可以将package.json 包含在您的函数目录中并运行npm install,就像您使用 Kudu 或 Azure 门户中的控制台通常使用 Node.js 项目一样。

    更多信息可以在这里找到: https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-node?tabs=v2#dependency-management

    【讨论】:

    • 谢谢。现在完全有道理,这就是它如何挂在一起。
    • 在 Kudu 中执行此操作的步骤如下:在浏览器中打开“https://.scm.azurewebsites.net/DebugConsole”; "cd 站点\wwwroot\"; “npm 安装”。
    • npm install 在 Azure 函数控制台中安装软件包需要花费大量时间。我可以看到创建了 node_modules 文件夹,但所有包都在 .staging 文件夹下
    • @Fabio 是否可以在功能应用级别而不是功能级别安装它们?使用 Azure 工件源是否有任何性能优势?
    【解决方案2】:

    我做了类似的事情,并且 SMTP 在不使用 SendGrid 的情况下工作。 下面是我的天蓝色函数代码。

    const nodemailer = require('nodemailer');
    module.exports = async function (context, myTimer) {
    
    let transport = nodemailer.createTransport({
        host: '',
        port: 2525,
        auth: {
           user: '',
           pass: ''
        }
    });
    
    
    const message = {
        from: '', // Sender address
        to: '',         // List of recipients
        subject: '', // Subject line
        text: '' // Plain text body
    };
    
    transport.sendMail(message, function(err, info) {
        if (err) {
          console.log(err)
        } else {
          console.log(info);
        }
    });
    
    };

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-24
      • 2019-02-15
      • 2020-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-08
      相关资源
      最近更新 更多