Can anyone lead me in the right direction of getting this Azure Function up where it is outputting "Cannot Find Module '@azure/functions'?

Viewed 965

I'm trying to get an Azure Function running locally using TypeScript. I have a relatively fair amount of experience in TypeScript, and generally in the JS world, but not so much when it comes to these consumption type cloud services. ​ I am running the latest version of core tools, so running func -v returns 3.0.3160 ​ I've tried reinstalling the tooling in. The same issue persists across multiple machines and operating systems. ​

function.ts

const httpTrigger: AzureFunction = async function (context: Context, req: HttpRequest): Promise<void> {
    context.log('HTTP trigger function processed a request.');
    
    // Contents of foo are irrelevant
    foo(context, req);
    context.res = {
        status: 200,
    };
};
export default httpTrigger;

package.json

"scripts": {
    "clean": "rm -rf ./dist/*",
    "build": "yarn run clean && tsc",
    "prestart": "yarn run build && func extensions install",
    "start:host": "func start --verbose",
    "start": "yarn run start:host && yarn run watch",
    "build:start": "yarn run build && yarn run start:host",
},
"dependencies": {
    "@types/node": "^12.12.6",
    "applicationinsights": "^1.8.8",
    "axios": "^0.21.0",
    "case": "^1.6.3",
    "node-fetch": "^2.6.1",
    "tslib": "^2.0.3",
    "@azure/functions": "^1.2.2"
},
"devDependencies": {
    "@types/node-fetch": "^2.5.7",
    "typescript": "^4.1.2"
}

​

tsconfig.json

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es6",
        "outDir": "dist",
        "rootDir": "./src",
        "moduleResolution": "Node"
    },
    "exclude": [
        "node_modules"
    ]
}

​

host.json

​

{
  "version": "2.0",
  "extensionBundle": {
        "id": "Microsoft.Azure.Functions.ExtensionBundle",
        "version": "[1.*, 2.0.0]"
    },
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  }
}

​

function.json

​

​
{
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ],
  "scriptFile": "../dist/function.js"
}

​

The Error

​ When running the azure function locally, I run "yarn run build:start" and from the script element it performs a yarn clean, yarn build, and a yarn run start. This is what the terminal outputs to the console. ​

Worker was unable to load function src: 'Error: Cannot find module '@azure/functions'
[2020-12-23T19:16:24.136Z] Require stack:
[2020-12-23T19:16:24.136Z] - /~/Projects/events/dist/function.js
[2020-12-23T19:16:24.136Z] - /usr/local/Cellar/azure-functions-core-tools@2/2.7.3023/workers/node/worker-bundle.js
[2020-12-23T19:16:24.136Z] - /usr/local/Cellar/azure-functions-core-tools@2/2.7.3023/workers/node/dist/src/nodejsWorker.js'
[2020-12-23T19:16:24.146Z] Worker failed to function id cef7538d-d82e-400d-b29a-dbb3dacf3e7f.
[2020-12-23T19:16:24.146Z] Result: Failure
[2020-12-23T19:16:24.147Z] Exception: Worker was unable to load function src: 'Error: Cannot find module '@azure/functions'
[2020-12-23T19:16:24.147Z] Require stack:
[2020-12-23T19:16:24.147Z] - /~/Projects/events/dist/function.js
[2020-12-23T19:16:24.147Z] - /usr/local/Cellar/azure-functions-core-tools@2/2.7.3023/workers/node/worker-bundle.js
[2020-12-23T19:16:24.147Z] - /usr/local/Cellar/azure-functions-core-tools@2/2.7.3023/workers/node/dist/src/nodejsWorker.js'
[2020-12-23T19:16:24.147Z] Stack: Error: Cannot find module '@azure/functions'
[2020-12-23T19:16:24.147Z] Require stack:
[2020-12-23T19:16:24.147Z] - /~/Projects/events/dist/function.js
[2020-12-23T19:16:24.147Z] - /usr/local/Cellar/azure-functions-core-tools@2/2.7.3023/workers/node/worker-bundle.js
[2020-12-23T19:16:24.147Z] - /usr/local/Cellar/azure-functions-core-tools@2/2.7.3023/workers/node/dist/src/nodejsWorker.js
[2020-12-23T19:16:24.147Z]     at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)
[2020-12-23T19:16:24.147Z]     at Function.Module._load (internal/modules/cjs/loader.js:667:27)
[2020-12-23T19:16:24.147Z]     at Module.require (internal/modules/cjs/loader.js:887:19)
[2020-12-23T19:16:24.147Z]     at Module.patchedRequire [as require] (/~/Projects/events/node_modules/diagnostic-channel/dist/src/patchRequire.js:14:46)
[2020-12-23T19:16:24.147Z]     at require (internal/modules/cjs/helpers.js:74:18)
[2020-12-23T19:16:24.147Z]     at Object.<anonymous> (/~/Projects/events/dist/function.js:47:1)
[2020-12-23T19:16:24.147Z]     at Module._compile (internal/modules/cjs/loader.js:999:30)
[2020-12-23T19:16:24.147Z]     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
[2020-12-23T19:16:24.147Z]     at Module.load (internal/modules/cjs/loader.js:863:32)
[2020-12-23T19:16:24.147Z]     at Function.Module._load (internal/modules/cjs/loader.js:708:14).
2 Answers

On occasion you can get flaky issues (for myriad reasons) with node_modules and it's worth deleting that folder and running yarn again.

You didn't post your yarn.lock file to verify if @azure/functions has actually been installed; might seem like a silly thing to overlook but can happen.

Also looks like you're missing the imports for function.ts.

import { AzureFunction, Context, HttpRequest } from "@azure/functions";

https://www.npmjs.com/package/@azure/functions#usage

'Error: Cannot find module '@azure/functions'

From the error you show, it seems the error comes from the node_modules folder that doesn't have the azure function modules. So you can do the global install-module command in the terminal:

npm install @azure/functions

After that, it should be ok(And please start the VS Code with the administrator.).

Related