Why Microsoft azure cloud function doesn't run?

Pavlo Kozak 45 Reputation points
2023-05-13T17:07:34.82+00:00

When I try to run cloud function with "npm run start" it gives me some errors

Worker was unable to load entry point "src/functions/*.js": Cannot use import statement outside a module No job functions found. Try making your job classes and methods public. If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.).

Cloud function itself

app.http('getChatGPTSuggestion', {
  methods: ['GET'],
  authLevel: 'anonymous',
  handler: async (request, context) => {
    const response = await openai.createCompletion({
      model: 'text-davinci-003',
      prompt:
        'Write a random text prompt for DALL·E to generate an image, this prompt will be shown to the user, include details such as the genre and what type of painting it should be, options can include: oil painting, watercolor, photo-realistic, 4k, abstract, modern, black and white etc. Do not wrap the answer in quotes.',
      max_tokens: 100, // max symbols of prompt
      temperature: 0.8, // spread out the wording of prompt so that they are not too different and sharp
    })

    context.log(`Http function processed request for url "${request.url}"`)

    const responseText = response.data.choices[0].text

    return {
      body: responseText,
    }
  },
})

Openai singletone pattern

const config = new Configuration({
    organization: process.env.OPENAI_ORGANIZATION_ID,
    apiKey: process.env.OPENAI_SECRET_KEY
})

const openai = new OpenAIApi(config)

module.exports = openai 

At first the amount of error was bigger, but I install azure-tools and .NET SDK, so some of them gone.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,679 questions
0 comments No comments
{count} votes

Accepted answer
  1. VasimTamboli 4,785 Reputation points
    2023-05-14T14:52:47.2866667+00:00

    The error you're encountering indicates an issue with the entry point and module setup in your Azure Functions project. Specifically, it mentions that the worker was unable to load the entry point "src/functions/*.js" and that it cannot use an import statement outside a module.

    To resolve this issue, you can try the following steps:

    Check the entry point configuration: Ensure that the entry point for your Azure Functions is correctly specified in your project's configuration. The entry point should point to the main JavaScript file that contains your Azure Functions code.

    Verify the module system: Make sure that your project is using a module system that supports ES modules. Azure Functions supports CommonJS modules by default, so you may need to configure your project to use CommonJS syntax instead of ES modules.

    Update the import statement: If you're using an import statement in your Azure Functions code, ensure that it is used within the proper module context. In CommonJS modules, you should use the require() function instead of import statements.

    Check your job classes and methods: Verify that your job classes and methods are declared as public. Ensure that the necessary registration methods for binding extensions, such as Azure Storage, Service Bus, and Timers, are called in your startup code.

    Ensure required dependencies are installed: Confirm that you have installed all the required dependencies for your Azure Functions project. Run npm install to ensure that all dependencies listed in your package.json file are installed.

    Review the Azure Functions runtime version: Check the Azure Functions runtime version you're using and ensure that it supports the features and syntax used in your code. If necessary, update the Azure Functions runtime version to a compatible one.

    Review recent changes: If you made any recent changes to your project or dependencies, consider reverting those changes or investigating if they could have introduced the error.

    Check the Azure Functions documentation: Review the Azure Functions documentation and any specific documentation related to the version you're using. It may provide additional insights or solutions to common issues.

    If the problem persists, you can also try creating a new Azure Functions project from scratch and gradually adding your code and dependencies to identify any potential issues.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful