Can't get node-fetch to work (my first Azure function)

Jim Burke 26 Reputation points
2022-02-17T18:28:30.757+00:00

Hi,

I am writing my first Azure function in node.js. I would like to post a result to a URL, I would like to use Node Fetch. However, when I try to include the fetch function with

const fetch = require('node-fetch');

The function fails with:

HTTP response code
500 Internal Server Error

and an error in the log:

FailureException: Error: Cannot find module 'node-fetch'

I have used the shell to run npm, and npm install node-fetch and both are successful. I have set the function to run in Node V 16.

Is there anything I am doing obviously wrong?

Regards

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

Accepted answer
  1. MayankBargali-MSFT 70,936 Reputation points Moderator
    2022-02-21T07:46:31.327+00:00

    @Jim Burke Looks like the module is not correctly installed. Please make sure that you have installed the module in D:\home\site\wwwroot directory and please verify that node_modules is created and verify whether your package is installed correctly as documented here. Node 16 is in the preview and only supported in 4.x function runtime.


1 additional answer

Sort by: Most helpful
  1. Jim Burke 26 Reputation points
    2022-02-21T17:02:29.09+00:00

    It looks like node-fetch is not compatible with requires in this version of Node (v16), it works with imports but that requires the azure function to be type: module which I don't think can be achieved. I have solved this by using axios which does work with requires in this version.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.