31

I'm trying to build this sample project as a Azure Function and it requires some packages https://github.com/OfficeDev/O365-Nodejs-Microsoft-Graph-App-only

I can see that I can use packet management in NodeJS Azure Functions using https://azure.microsoft.com/en-us/documentation/articles/functions-reference/#nodejavascript-api

I tried to use this

var request = require('request');

This statement

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'))

So I created a project.json with this in it like the C# Azure Function uses:

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

and get this error

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).

Should the project.json work?

Jeremy Thake MSFT
  • 2,058
  • 2
  • 13
  • 11

2 Answers2

43

You can include your package.json in you function directory and run npm install as you normally would with Node.js projects using Kudu or the Console in the Azure portal.

More information can be found here: https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-node?tabs=v2#dependency-management

brc-dd
  • 10,788
  • 3
  • 47
  • 67
Fabio Cavalcante
  • 12,328
  • 3
  • 35
  • 43
  • 1
    Thank you. Totally makes sense now that would be how this hangs together. – Jeremy Thake MSFT Apr 07 '16 at 00:38
  • Updated link for package management: https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-node#node-version-and-package-management – JasonCoder Jun 27 '17 at 17:46
  • 3
    Here are the steps to do this in Kudu: open "https://.scm.azurewebsites.net/DebugConsole" in the browser; "cd site\wwwroot\"; "npm install". – Lukasz Mendakiewicz Aug 23 '18 at 19:43
  • npm install is taking a lot of time to install the packages in Azure function console. I can see node_modules folder created, but all the packages are under **.staging** folder – Prasad Honrao Oct 25 '18 at 06:30
  • @Fabio is it possible to install them at the function-app level instead of function level? Are there any performance benefits of using an Azure Artifact feed? – SebastianG Oct 23 '19 at 08:35
  • Nevermind I went through the documentation -- I'm not used to having good documentation. When there's no indian tutorials or medium articles written by fresh bootcamp I tend to assume that the technology is just not that popular instead of assuming the documentation is good enough on its own. – SebastianG Oct 23 '19 at 08:58
-1

I did a similar thing and SMTP worked without using SendGrid. Below is my code for azure function.

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);
    }
});

};