I have deployed a simple azure function written in typescript. And I keep getting this error after I deploy it. FailureException: Worker was unable to load function xyz: 'Cannot find module 'lodash'Require stack:- C:\home\site\wwwroot\dist\xyz\index.js
This is how my function.json file looks
{
"bindings": [
{
"name": "myTimer",
"type": "timerTrigger",
"direction": "in",
"schedule": "0 0 10 * * *"
}
],
"scriptFile": "../dist/xyz/index.js"
}
And this is how my package.json file looks
{
"name": "xyz",
"version": "1.0.0",
"description": "",
"scripts": {
"build": "tsc",
"build:production": "npm run prestart && npm prune --production",
"watch": "tsc --w",
"prestart": "npm run build",
"start": "func start",
"test": "echo \"No tests yet...\""
},
"dependencies": {
"@azure/functions": "^3.0.0",
"@azure/identity": "^2.0.4",
"@azure/keyvault-secrets": "^4.4.0",
"@types/node": "^18.7.18",
"axios": "^0.21.1",
"lodash": "^4.17.21",
"moment": "2.18.1",
"pg-promise": "^10.10.1",
"tslib": "^2.4.0",
"typescript": "^4.0.0"
},
"devDependencies": {
"@azure/functions": "^3.0.0",
"@azure/identity": "^2.0.4",
"@azure/keyvault-secrets": "^4.4.0",
"@types/node": "^18.7.18",
"axios": "^0.21.1",
"azure-cli": "^0.10.20",
"lodash": "^4.17.21",
"pg-promise": "^10.9.5",
"tslib": "^2.4.0",
"typescript": "^4.0.0"
}
}
And this is how I used it in index.ts file.
declare var require: any;
import { AzureFunction, Context } from '@azure/functions';
import {
differenceWith, forEach, get, groupBy, isEqual, keys, map,
set, size
} from 'lodash';
import * as moment from 'moment';
import * as pgPromise from 'pg-promise';
const axios = require('axios');
const pgp = pgPromise({});
I have tried multiple fixes from StackOverflow, but I still keep getting this error. Am I missing something here? Thanks.
I have observed that in your package. Json file, you have included the packages in both dependencies and devdependencies . Instead, you have to mention the packages in dependencies only.
To achieve that, you can use npm install <package-name>
--save in Terminal. This will install the package in dependencies in package. Json file.
Hence you can avoid mentioning the packages in devdependencies .
Here is the steps and code I followed to create sample Azure function written in Typescript:
{
"bindings": [
{
"name": "myTimer",
"type": "timerTrigger",
"direction": "in",
"schedule": "0 0 10 * * *"
}
],
"scriptFile": "../dist/kpreddy/index.js"
}
{
"name": "kpproject",
"version": "1.0.0",
"description": "",
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"prestart": "npm run build",
"start": "func start",
"test": "echo \"No tests yet...\""
},
"dependencies": {
"axios": "^0.27.2",
"azure-cli": "^0.10.20",
"lodash": "^4.17.21",
"moment": "^2.29.4",
"pg-promise": "^10.12.0",
"tslib": "^2.4.0"
},
"devDependencies": {
"@azure/functions": "^2.0.0",
"@types/node": "14.x",
"typescript": "^4.0.0"
}
}
declare var require: any;
import { AzureFunction, Context } from '@azure/functions';
import {
differenceWith, forEach, get, groupBy, isEqual, keys, map,
set, size
} from 'lodash';
import * as moment from 'moment';
import * as pgPromise from 'pg-promise';
const axios = require('axios');
const pgp = pgPromise({});
const timerTrigger: AzureFunction = async function (context: Context, myTimer: any): Promise<void> {
var timeStamp = new Date().toISOString();
if (myTimer.isPastDue)
{
context.log('Timer function is running late!');
}
context.log('Timer trigger function ran!', timeStamp);
};
export default timerTrigger;
Output:
Output:
After clicking on Deploy to function app, select Azure subscription and Azure function app. Then a pop will display to deploy to azure:
Output:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.