Azure Functions + node.js - 找不到模块
Azure Functions + node.js - cannot find module
我已经使用 Azure CLI 创建了一个应用程序和一个函数,它具有适当的结构,所以:
/host.json
/local.settings.json
/mycustomfunction/function.json
/mycustomfunction/index.js
/mycustomfunction/package.json
/node_modules
源代码从BitBucket下载,用Kudu部署,构建(node_modules被获取,azure是其中之一),一切都是绿色的。
当谈到依赖时,只有一个 - "azure": "^2.0.0-preview"
但是当我 运行 Azure 上的函数时,出现错误
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)
当 运行 和 func host start
...
时,相同的函数在本地工作正常
我做错了什么? :)
根本原因应该是因为您没有在函数应用程序的 Kudu 控制台中 运行 npm install 命令来安装定义的必要节点模块在您的函数应用程序的 package.json.
遵循本指南:Node version and Package Management
以下是上述参考指南中的一些要点。
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'});
问题似乎出在 Azure Functions 的 Shared/Consumption 模型上。我注意到,当 运行 npm install
时,大多数情况下进程会超时,node_modules
文件夹中不会显示任何包,只留下 .staging
文件夹。使用专用 App Service Plan 创建新的 Function App 后,一切正常。
另一个(可能更好的)解决方案 是包含 azure-sb
模块,instead of azure
。它提供了足够的功能来查询 Azure 服务总线,同时它的体积要小得多,而且即使使用共享层资源,Kudu 也能够获取它。
我已经使用 Azure CLI 创建了一个应用程序和一个函数,它具有适当的结构,所以:
/host.json
/local.settings.json
/mycustomfunction/function.json
/mycustomfunction/index.js
/mycustomfunction/package.json
/node_modules
源代码从BitBucket下载,用Kudu部署,构建(node_modules被获取,azure是其中之一),一切都是绿色的。
当谈到依赖时,只有一个 - "azure": "^2.0.0-preview"
但是当我 运行 Azure 上的函数时,出现错误
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)
当 运行 和 func host start
...
我做错了什么? :)
根本原因应该是因为您没有在函数应用程序的 Kudu 控制台中 运行 npm install 命令来安装定义的必要节点模块在您的函数应用程序的 package.json.
遵循本指南:Node version and Package Management
以下是上述参考指南中的一些要点。
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'});
问题似乎出在 Azure Functions 的 Shared/Consumption 模型上。我注意到,当 运行 npm install
时,大多数情况下进程会超时,node_modules
文件夹中不会显示任何包,只留下 .staging
文件夹。使用专用 App Service Plan 创建新的 Function App 后,一切正常。
另一个(可能更好的)解决方案 是包含 azure-sb
模块,instead of azure
。它提供了足够的功能来查询 Azure 服务总线,同时它的体积要小得多,而且即使使用共享层资源,Kudu 也能够获取它。