Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

encoding not found when requiring node-fetch #30

Closed
bcomnes opened this issue Apr 16, 2019 · 11 comments
Closed

encoding not found when requiring node-fetch #30

bcomnes opened this issue Apr 16, 2019 · 11 comments

Comments

@bcomnes
Copy link
Contributor

bcomnes commented Apr 16, 2019

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:

@depadiernos
Copy link

depadiernos commented Apr 17, 2019

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

@bcomnes
Copy link
Contributor Author

bcomnes commented Apr 17, 2019

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.

@bcomnes
Copy link
Contributor Author

bcomnes commented Apr 17, 2019

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

@fool
Copy link

fool commented Apr 17, 2019

oh, thanks for this!

@bridgestew
Copy link

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.

@bcomnes
Copy link
Contributor Author

bcomnes commented Apr 23, 2019

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

@cliffasaurus
Copy link

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

@bcomnes
Copy link
Contributor Author

bcomnes commented Apr 29, 2019

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?

@cliffasaurus
Copy link

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

@bcomnes
Copy link
Contributor Author

bcomnes commented May 17, 2019

Close this issue when d3e4b2a is released.

@ehmicky ehmicky closed this as completed Sep 19, 2019
@ehmicky
Copy link
Contributor

ehmicky commented Sep 19, 2019

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants