netlify/zip-it-and-ship-it

encoding not found when requiring node-fetch

Closed this issue · 11 comments

For some reason, when you try to package a function that requires node-fetch the encoding module isn't found.

We need to determine if:

  • This is a problem with node-fetch, or a consumption concern
  • or if we are missing this dep for some reason, or if we are requiring that it exist, but it doesn't really have to.
  • Decide if we make an exception for this very common scenario, or improve output when this pitfall is hit.

Examples in the wild:

I've seen this happen with netlify-lambda in the past. I've not used node-fetch very much and axios (my preferred package) does not have this issue.

Related customers: https://app.intercom.io/a/apps/q245f50x/inbox/inbox/unassigned/conversations/21646909770
https://app.intercom.io/a/apps/q245f50x/inbox/inbox/unassigned/conversations/21687296378

Its not us, its happening in user code. e.g. when we try to bundle a function using node-fetch with ZISI, ZISI complaints that it can't locate encoding.

Looks like it could be a user error:

This API requires an optional dependency on npm package encoding, which you need to install manually. webpack users may see a warning message due to this optional dependency.) -- https://github.com/bitinn/node-fetch#bodytextconverted

It looks like node-fetch depends on encoding as a optional peer dependency.

One thing we could also do is just print a warning instead of outright failing.

fool commented

oh, thanks for this!

Is this the cause for my deployments failing when I haven't changed anything in my functions? They worked without issue a few days ago.

Are there some detailed instructions you can point me to that will help me resolve my issue? I wrote a post about how I got my function working, and as you might see, I was following other people's instructions about how to do this kind of stuff. Meaning, this isn't a strong skill for me to begin with. :)

Any advice will be helpful. I think I'm going to turn off the function for the time being so that I am not prevented from updating my site, but I don't want to do that for long, naturally.

@bridgestew install the encoding dependency and it should resume working.

Hello, @bcomnes I've added encoding but it didn't work. It seems to look for './dist/react-hot-loader.production.min.js' from '/opt/build/repo/dist/lambda' I've never had this problem before and I didn't touch functions.

12:42:06 PM: Build script success
12:42:06 PM: Starting to prepare functions from 'dist'
12:42:06 PM: Zipping functions from /opt/build/repo/dist/lambda to /tmp/zisi-087720367
12:42:07 PM: Error: Cannot find module './dist/react-hot-loader.production.min.js' from '/opt/build/repo/dist/lambda'
12:42:07 PM: Failing build: Failed to prepare functions for deployment
12:42:07 PM: failed during stage 'preparing functions for deployment': exit status 1
12:42:08 PM: Finished processing build request in 1m47.046320424s
12:42:08 PM: Shutting down logging, 0 messages pending

It looks like a function is requiring ./dist/react-hot-loader.production.min.js but it isn't found when bundling the function. Ensuring that is available, or ensuring that your function doesn't attempt to load that module during runtime is the solution. Are you perhaps shipping your site bundle as a function?

It looks like a function is requiring ./dist/react-hot-loader.production.min.js but it isn't found when bundling the function. Ensuring that is available, or ensuring that your function doesn't attempt to load that module during runtime is the solution. Are you perhaps shipping your site bundle as a function?

I got it now. The bundling seems to have traversed one level up and got the .babelrc where I declared a plugin of react-hot-loader. I fixed it by declaring a separate .babelrc inside the functions folder

Close this issue when d3e4b2a is released.

Just for reference, this problem is not specific to node-fetch. It applies to any require("moduleName") that is inside a conditional block or a try/catch (such as encoding) and is not declared in optionalDependencies.

This has been fixed in node-fetch@3 but this has not been released yet.