I am trying to create an Azure Function with typescript. I am fairly new to both. I followed the docs and create one Azure function using the steps mentioned in the Azure Docs.
Docs:
AppInsight-Typescript
HOST.json
Azure function With Typecript
When I created the function using Httptrigger template it worked just fine. I was able to access httptrigger and got the Hello World! in response. I am using programming model v4, host.json v2, Runtime worker: node, worker runtime version is ~4 and node version 20.
My current directory structure is as follows:
az-createquote-functionapp/
├── .vscode/
├── dist/
├── node_modules/
├── src/
│ ├── functions/
│ │ └── createQuoteHttp.ts
│ ├── index.ts
│ └── telemetry.ts
├── .gitignore
├── .funcignore
├── eslint.config.js
├── host.json
├── local-settings.json
├── package-lock.json
├── package.json
└── tsconfig.json
My tsconfig.json:
{
"compilerOptions": {
"module": "ESNext",
"target": "ESNext",
"moduleResolution": "Node",
"outDir": "dist",
"rootDir": ".",
"sourceMap": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"noImplicitAny": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"forceConsistentCasingInFileNames": true,
},
"include": [
"./**/*.ts",
],
"exclude": [
"node_modules",
"dist"
]
}
My package.json
{
"name": "az-createquote-functionapp",
"version": "1.0.0",
"description": "",
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"clean": "rimraf dist",
"prestart": "npm run clean && npm run build",
"start": "func start --verbose",
"lint": "eslint '**/*.{js,mjs,cjs,ts}'",
"lint:fix": "eslint '**/*.{js,mjs,cjs,ts}' --fix",
"test": "echo "No tests yet...""
},
"dependencies": {
"@azure/functions": "^4.5.0",
"@azure/functions-opentelemetry-instrumentation": "^0.1.0",
"@azure/monitor-opentelemetry-exporter": "^1.0.0-beta.24",
"@azure/openapi": "^3.0.104",
"@opentelemetry/api": "^1.9.0",
"@opentelemetry/auto-instrumentations-node": "^0.49.1",
"@opentelemetry/exporter-trace-otlp-http": "^0.52.1",
"@opentelemetry/sdk-node": "^0.52.1"
},
"devDependencies": {
"@eslint/js": "^9.8.0",
"@types/node": "^20.x",
"eslint": "^9.8.0",
"globals": "^15.9.0",
"rimraf": "^5.0.0",
"typescript": "^4.0.0",
"typescript-eslint": "^8.0.0"
},
"overrides": {
"eslint": "^9.8.0"
},
"type": "module",
"main": "dist/src/{index.js,functions/*.js}"
}
//index.ts
import "./telemetry";
import { app } from '@azure/functions';
app.setup({
enableHttpStream: true,
});
//telemetry.ts
import { AzureFunctionsInstrumentation } from '@azure/functions-opentelemetry-instrumentation';
import { AzureMonitorLogExporter, AzureMonitorTraceExporter } from '@azure/monitor-opentelemetry-exporter';
import { getNodeAutoInstrumentations, getResourceDetectors } from '@opentelemetry/auto-instrumentations-node';
import { registerInstrumentations } from '@opentelemetry/instrumentation';
import { detectResourcesSync } from '@opentelemetry/resources';
import { LoggerProvider, SimpleLogRecordProcessor } from '@opentelemetry/sdk-logs';
import { NodeTracerProvider, SimpleSpanProcessor } from '@opentelemetry/sdk-trace-node';
const resource = detectResourcesSync({ detectors: getResourceDetectors() });
const tracerProvider = new NodeTracerProvider({ resource });
tracerProvider.addSpanProcessor(new SimpleSpanProcessor(new AzureMonitorTraceExporter()));
tracerProvider.register();
const loggerProvider = new LoggerProvider({ resource });
loggerProvider.addLogRecordProcessor(new SimpleLogRecordProcessor(new AzureMonitorLogExporter()));
registerInstrumentations({
tracerProvider,
loggerProvider,
instrumentations: [getNodeAutoInstrumentations(), new AzureFunctionsInstrumentation()],
});
// my http trigger
import { app, HttpRequest, HttpResponseInit, InvocationContext } from "@azure/functions";
export async function createQuoteHttp(request: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> {
context.log(`Http function processed request for url "${request.url}"`);
const name = request.query.get('name') || await request.text() || 'world';
return { body: `Hello, ${name}!` };
};
app.http('createQuoteHttp', {
route: 'V1/createQuote',
methods: ['GET', 'POST'],
authLevel: 'anonymous',
handler: createQuoteHttp
});
As soon as I added telemetry, I started getting error
Error [ERR_MODULE_NOT_FOUND]: Worker was unable to load entry point "dist/src/index.js": Cannot find module '/x/y/projects/az-createquote-functionapp/dist/src/telemetry'.
However, files are present in the correct location in the dist folder. Trigger with default template works but as soon as I try to create any directory or other files and import it either in the index.ts or in my trigger.ts. I start getting this error. I need to build lot of files and directories to seggregate logics but I am not why it is failing.
Here is the exact error:
I am trying to build an API using Azure function with typescript. I tried changing Node versions. I tried changing model to “CommonJS”. I tried adding telemetry code in index.ts itself but not sure if I even know the right cause. I am fairly new to both Azure and Typescript so I am trying to implement this using official documentation.