skip to Main Content

Well, this is probably a typescript question but it is also related to Azure:

In my Azure function Nodejs code:

import {
  app,
  HttpRequest,
  HttpResponseInit,
  InvocationContext,
} from "@azure/functions";
import { workerExec } from "./worker";

export async function myappfunction1(
  request: HttpRequest,
  context: InvocationContext
): Promise<HttpResponseInit> {
  context.log(`Http function processed request for url "${request.url}"`);
  try {
    console.log(`Incoming event: ${JSON.stringify(event)}`);
    const result = await workerExec(event, context);
    console.log("Successfully processed event;");
    return result;
  } catch (err) {
    console.log(err);
    throw err;
  }
}

app.http("myappfunction1", {
  methods: ["GET", "POST"],
  authLevel: "anonymous",
  handler: myappfunction1,
});


I get this error when building with the tsc command:

error TS2305: Module '"@azure/functions"' has no exported member 'app'.
error TS2724: '"@azure/functions"' has no exported member named 'HttpResponseInit'. 
error TS2305: Module '"@azure/functions"' has no exported member 'InvocationContext'

Here is my tsconfig.json:

{
  "compilerOptions": {
    "target": "es2020",
    "strict": true,
    "preserveConstEnums": true,
    "noEmit": true,
    "sourceMap": false,
    "module": "es2015",
    "moduleResolution": "node",
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "baseUrl": "src/functions",
    "paths": {
      "*": [
        "*",
        "node_modules/*"
      ]
    },
  },
  "exclude": [
    "node_modules",
    "**/*.test.ts"
  ],
  "include": [
    "**/*"
  ]
}

what is wrong?

2

Answers


  1. Chosen as BEST ANSWER

    I fixed it by running:

    npm install @azure/functions@preview
    

  2. I have reproduced the issue at my environment and got expected result

    I have referred ms docs to create a function in v4 model using typescript.

    Initially when I created the function, I was getting the error too.

    enter image description here

    My folder structure was

    enter image description here

    Then I executed npm run build command. Upon successful execution, it created dist in my folder structure, where you can see your functions .js file has created. Because in my tsconfig file, outDir is set to "outDir" : "dist".

    In your case, outDir is missing in your tsconfig file.

    enter image description here

    enter image description here

    Then, it worked for me.

    code:

    import { app, HttpRequest, HttpResponseInit, InvocationContext } from  "@azure/functions";
    
    export  async  function  myappfunction1(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('myappfunction1', {
    methods: ['GET', 'POST'],
    authLevel:  'anonymous',
    handler:  myappfunction1
    });
    

    local.setting.json:

    {
    "IsEncrypted": false,
    "Values": {
    "AzureWebJobsStorage": "",
    "FUNCTIONS_WORKER_RUNTIME": "node",
    "AzureWebJobsFeatureFlags": "EnableWorkerIndexing"
    }
    }
    

    tsconfig.json:

    {
    "compilerOptions": {
    "module": "commonjs",
    "target": "es6",
    "outDir": "dist",
    "rootDir": ".",
    "sourceMap": true,
    "strict": false
    }
    }
    

    Output:

    enter image description here

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search