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).