"Error: Cannot find module 'node-fetch'" when deploying function

I have several functions working well, and I can see them in the control panel. I added a new one today, and for some reason, didn’t see it reflected in the panel.

I had been building them out, then using netlify deploy to get them out there, and all was working fine. So I figured maybe I should try to push everything up to GitHub and have Netlify rebuild from scratch.

But then I started to get random "Error: Cannot find module 'node-fetch 502 errors and the site started breaking (sometimes they would work, sometimes they wouldn’t).

Just re-did everything by building at home, and then netlify deploy-ing and all is back to normal.

So now I pushed that branch up, the functions are now all listed. I can see all the branches in the pull-down for the Functions…and still I get this with the new function:

 ERROR  Request failed with status code 404

  at createError (node_modules/axios/lib/core/createError.js:16:15)
  at settle (node_modules/axios/lib/core/settle.js:17:12)
  at IncomingMessage.handleStreamEnd (node_modules/axios/lib/adapters/http.js:237:11)
  at IncomingMessage.emit (events.js:208:15)
  at IncomingMessage.EventEmitter.emit (domain.js:471:20)
  at endReadableNT (_stream_readable.js:1154:12)
  at processTicksAndRejections (internal/process/task_queues.js:77:11)

I’ve narrowed it down to something with my setup - when I push to GitHub and have Netlify automatically rebuild, I have issues with functions.

When I build at home, and then deploy via netlify deploy, all works great.

hi @jhull, do you have your function dependencies in a package.json inside the function folder? If so, our buildbot will not automatically run npm install in those folders. You’ll need to add something like cd path_where_function lives/ && npm install && rest of your build command. This will ensure your dependencies are installed.

Our buildbot only runs npm install (or yarn install) for the root package.json. That’s why that added step is needed. You can, instead, have your function dependencies in your main package.json, then you can leave your build command as-is and things should still work.

2 Likes

I also updated your post title to reflect the exact issue better. Hope you don’t mind.

Not sure if it’s related, but I had issues for a while getting netlify to build my functions with node-fetch as a dep. Building functions myself and deploying from my machine worked, but getting netlify to build them never worked with node-fetch as a dep. Maybe you’re seeing something similar?

For reference:

No problem at all.

Tried adding node-fetch to root package.json and still not got the error:

Cannot find module 'node-fetch'

Went into build command in netlify.toml, and added:

[build]
  command = "cd ./functions/my-function/ && npm install && cd ../../ && npm run generate"

and install seemed to go well.

Hitting that function and it worked! BUT…other functions that ALSO use node-fetch are still saying they can’t find the module.

Am I supposed to cd into each individual one? That seems a bit redundant…what did I do wrong about adding node-fetch to root with simple npm install node-fetch?

Was able to solve this by pulling out all the dependencies from within each functions folder - so now, my functions folder is simply a list of those function files (no folders inside).

I also installed encoding dependency where I read elsewhere had some effect with node-fetch.

1 Like

thanks for sharing this, @jhull!

I was having this problem also and tried the steps from here, Netlify Functions Can't find module and Cannot use node-fetch in lambda function :

  • Installing deps in project root
  • Installing deps in functions specific folder e.g. having functions/testfunc/package.json and changing command to install these and also
  • Changing const fetch = require('node-fetch') to:
    • const fetch = require('node-fetch').default
    • const fetch = request('node-fetch').default
  • Adding node-fetch peer dependency encoding

However, none of these things worked. I then tried seeing if copying the Fetch example from playgrounds would work and it did. The issue was that I had a variable in my function static

const static = ['/blog', '/about', '/'];

For some reason having a variable called static causes

Error: Cannot find module 'node-fetch'

This can be replicated by:

  1. Creating a new function, test.js with the code from the Fetch example in Functions playground
  2. Installing node-fetch in the projects root dependecies
  3. Deploying to netlify with automatic git commit deploys
  4. Going to /.netlify/functions/test and seeing the joke
  5. Change the function to:
const fetch = require("node-fetch");

const API_ENDPOINT = "https://icanhazdadjoke.com/";

exports.handler = async (event, context) => {
  const static = ["hello", "goodbye"];

  return fetch(API_ENDPOINT, { headers: { Accept: "application/json" } })
    .then((response) => response.json())
    .then((data) => ({
      statusCode: 200,
      body: data.joke,
    }))
    .catch((error) => ({ statusCode: 422, body: String(error) }));
};
  1. Commit and deploy
  2. Go back to /.netlify/functions/test and you’ll see the error

@perry is this the expected behaviour? No other variable causes this problem and the function works fine in dev (netlify dev and going to localhost)

Hey @r-bt,
Well that is super weird! I did some tests with the static var and it seems to not be a problem with other dependencies. I’d guess it is a reserved word in the node-fetch world. Thanks for reporting this!