"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.

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:

  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!