Cannot find module in azure function written in typescript

Viewed 48

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.

1 Answers

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:

  1. function. Json:
{
  "bindings": [
    {
      "name": "myTimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 0 10 * * *"
    }
  ],
  "scriptFile": "../dist/kpreddy/index.js"
}
  1. Package.json:
{
  "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"
  }
}
  1. Index.ts:
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;
  1. Run the project with the below command in the Terminal:
  • npm install
  • npm start

Output:

enter image description here

  1. Executed a local function from the workspace as mentioned in the snippet:

enter image description here

Output:

enter image description here

  1. Deployed local function[kpreddy] to the Azure function [Kpravallika]:

enter image description here

After clicking on Deploy to function app, select Azure subscription and Azure function app. Then a pop will display to deploy to azure:

enter image description here

Output:

enter image description here

  1. After the deployment,the local function has been deployed to the Azure function:

enter image description here

Related