Azure / azure-functions-nodejs-worker

The Node.js worker for the Azure Functions runtime - https://functions.azure.com
MIT License
106 stars 44 forks source link

A function can only be registered during app startup. #718

Closed wojtekmaj closed 1 year ago

wojtekmaj commented 1 year ago

Investigative information

Please provide the following:

Repro steps

I have created a simple v4 Azure Function:

import { app } from '@azure/functions';

app.http('get', {
  methods: ['GET'],
  handler: async (req) => {
    return {
      status: 200,
      body: 'Hello world (from get)',
    };
  },
});

I then compiled it with TypeScript.

I have added function.json with manually defined triggers, because otherwise I got "No HTTP triggers found." error.

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

I renamed dist/index.js to dist/index.mjs, because Azure Functions apparently can't properly import ESM modules even if type: module is set in package.json unless they have .mjs extension.

Expected behavior

Hello world

Actual behavior

Result: Failure Exception: Worker was unable to load function get: 'A function can only be registered during app startup.' Stack: Error: Worker was unable to load function get: 'A function can only be registered during app startup.' at Object.t.registerFunction (/azure-functions-host/workers/node/dist/src/worker-bundle.js:2:28273) at generic (/home/site/wwwroot/get/node_modules/@azure/functions/dist/azure-functions.js:494:17) at Object.http (/home/site/wwwroot/get/node_modules/@azure/functions/dist/azure-functions.js:428:5) at file:///home/site/wwwroot/get/dist/index.mjs:2:5 at ModuleJob.run (internal/modules/esm/module_job.js:183:25) at async Loader.import (internal/modules/esm/loader.js:178:24)

Known workarounds

Provide a description of any known workarounds.

Related information

Provide any related information