Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Functions + node.js - cannot find module

I've created an app and a function with Azure CLI, which has a proper structure, so:

/host.json
/local.settings.json
/mycustomfunction/function.json
/mycustomfunction/index.js
/mycustomfunction/package.json
/node_modules

Source code gets downloaded from BitBucket, deployed with Kudu, built (node_modules are fetched, azure is one of them), everything is green.

When it comes to dependencies there is only one - "azure": "^2.0.0-preview"

But when I run the function on Azure, I get error

2017-09-08T13:59:06.091 JavaScript HTTP trigger function processed a request. 2017-09-08T13:59:06.216 Exception while executing function: Functions.mycustomfunction. mscorlib: Error: Cannot find module 'azure' at Function.Module._resolveFilename (module.js:469:15)

The same function works fine locally when run with func host start...

What am I doing wrong? :)

like image 452
Lech Migdal Avatar asked Sep 08 '17 14:09

Lech Migdal


2 Answers

The root cause should be because you haven't not run the npm install command in the Kudu console of your function app to install the necessary node modules defined in your function app's package.json.

Follow this guide: Node version and Package Management

Below are some highlights from the reference guide above.

After the package.json file is uploaded, run the npm install command in the Kudu remote execution console.

This action downloads the packages indicated in the package.json file and restarts the function app.

After the packages you need are installed, you import them to your function by calling require('packagename'), as in the following example:

// Import the underscore.js library
var _ = require('underscore');
var version = process.version; // version === 'v6.5.0'

module.exports = function(context) {
    // Using our imported underscore.js library
    var matched_names = _
        .where(context.bindings.myInput.names, {first: 'Carla'});
like image 50
juvchan Avatar answered Nov 02 '22 07:11

juvchan


It seems that the problem was with the Shared/Consumption model for Azure Functions. I've noticed that when running npm install, most of the time the process would timeout with no packages showing up in the node_modules folder leaving only the .staging folder behind. After creating a new Function App with with dedicated App Service Plan, everything works as expected.

Another (potentially better) solution is to include azure-sb module, instead of azure. It provides enough capabilities to query Azure Service Bus, while it's significantly smaller and Kudu is able to fetch it even with Shared Tier resources.

like image 30
Lech Migdal Avatar answered Nov 02 '22 07:11

Lech Migdal