netlify / zip-it-and-ship-it

Intelligently prepare Node.js Lambda functions for deployment
https://www.npmjs.com/package/@netlify/zip-it-and-ship-it
MIT License
316 stars 35 forks source link

encoding not found when requiring node-fetch #30

Closed bcomnes closed 5 years ago

bcomnes commented 5 years ago

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:

Examples in the wild:

depadiernos commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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

fool commented 5 years ago

oh, thanks for this!

bridgestew commented 5 years ago

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 commented 5 years ago

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

cliffasaurus commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

Close this issue when https://github.com/netlify/zip-it-and-ship-it/pull/35/commits/d3e4b2aae9578f631fb6dd78b55cf2404817e11d is released.

ehmicky commented 5 years ago

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.