So this is a bit embarrassing, since I deployed several Netlify sites, with functions that are way more complex than this. But I’m kinda stumped, so here I am:
The novelty here is that I’m trying to deploy a function only - no front-end page whatsoever.
I created a simple function that uses node-fetch to call an API. I used netlify-lambda to package and serve the function. I keep getting an error r is not a function on the line when I call fetch.
-
My package.json includes
"node-fetch": "^2.6.0". -
My
netlify.tomllooks like this:
[build]
command = "npm run build"
functions = ".netlify/functions/"
With the build script being "build": "netlify-lambda build functions".
-
The output from the build operation includes the line
[37] ../node_modules/node-fetch/lib/index.mjs 39.9 KiB {0} [built] -
The code (with unnecessary parts removed) would look like this:
const fetch = require('node-fetch');
const constants = require('./constants');
class X {
async _callAPI(requestXML) {
try {
const uri = constants.apiUrl + requestXML;
console.log(uri);
console.log(fetch);
const response = await fetch(uri);
console.log('ok');
if (!response.ok) {
throw new Error(response.statusText);
}
const body = await response.text();
return body;
}
catch(ex) {
throw ex;
}
}
}
So I get to the line where the uri is printed - all OK. I then decided to print fetch, and to my surprise got:
Object [Module] {
Headers: [Getter],
Request: [Getter],
Response: [Getter],
FetchError: [Getter],
default: [Function: H] {
isRedirect: [Function (anonymous)],
Promise: [Function: Promise]
}
}
Clearly, it is not a function. But why?
I went over the “compiled” code of the function, and the node-fetch code is included, as well as my code.
What am I doing wrong? About the only thing I can think of is that this code resides in a sub folder next to the actual function code with the handler, but that still makes no sense.
PS: I tried searching around and found this: https://github.com/node-fetch/node-fetch/issues/450. Turns out it was a great help: I removed node-fetch and installed node-fetch@1.7.3 and now everything works!
But the question still remains why?. AFAIK I’m not using webpack (unless netlify-lambda does). And how can this be fixed going forward? I don’t want to use a 2-versions-back version of a module.
Did anything else print after that? I also just checked out what I think is your function and it seems to be returning a response body that’s not an error. Have you gotten this working in the meantime? If not, would you mind sending your Netlify URL so we can dig in further? Thanks!