Error: Could not find “encoding” module in file, Netlify function

Has anyone run into this problem?

My build is currently failing to build a function which only uses the ‘node-fetch’ lib [0].

The build runs successfully using the local netlify deploy but when I trigger a build using a push it fails saying:

3:41:04 PM: Prepping functions with zip-it-and-ship-it 0.3.1
3:41:05 PM: Error: Could not find “encoding” module in file: /repo/dist/utility-data.js.
3:41:05 PM: Please ensure “encoding” is installed in the project.
3:41:05 PM: Error prepping functions

[0] https://github.com/apearson/website/blob/master/src/functions/utility-data.ts
[1] https://app.netlify.com/sites/apearsonio/deploys/5d361f1e1f70200008913e49

Hi @apearson,

Are you bundling your function as part of your build process? or are you zipping it?

Hi @futuregerald

I’m using netlify-lambda build src/functions to bundle the functions. In the bundled js file there is a require(‘encoding’) but that is coming from the ‘aws-lambda’ package which shouldn’t (hopefully) be causing problems.

Are you transcoding the ts to js using netlify-lambda as well? You mentioned that there’s a require import for encoding, where is that require statement? It looks like we’re trying to zip your function but that’s failing because the encoding dependency isn’t there. Do you have a separate package.json in your functions directory?

Yes I’m transcoding the ts to js using netlify-lambda.
I don’t have a separate package.json in my functions dir.

I ran the command locally and it compiled correctly. I then opened the compiled file and did a search for require('encoding') and found it inside the compiled js.

Sounds like I should test the zip-it-and-ship-it part of the chain on my local machine and see if that fails.

That’d be a goodtest - could you let us know how it goes @apearson ?

1 Like

I believe I’ve figured out what’s going on.

The zip-it-and-ship-it also failed on my local machine. Once I installed encoding the zipping process succeeded.

I looked through the node-fetch code and found they are doing a require(‘encoding’) [0] without actually having that in their package.json [1]. This causes the zip-it-and-ship-it to attempt to zip the non-existent encoding dependency when walking the dependency tree.

It seems like the node-fetch maintainers might not be up for including encoding as a dependency. [3]

I guess the workaround, for now, is to just have encoding as a dependency in my own project.

[0] https://github.com/bitinn/node-fetch/blob/95286f52bb866283bc69521a04efe1de37b26a33/src/body.js#L14
[1] https://github.com/bitinn/node-fetch/blob/master/package.json
[3] https://github.com/bitinn/node-fetch/issues/316#issuecomment-317622258

3 Likes

great find and thanks so much for sharing that! I think you’re right as I’ve heard that before (but apologies for not remembering it!)

I’m currently having the same problem. I am confused as to why this is happening now, i have not altered the lambda code between the currently deployed master branch and the preview deploy that is currently failing the build.

Any ideas?

Hey @AG-Labs, could you please share a Netlify site name and link to a deploy log so we can take a look? We shipped a fix to the ‘encoding’ module problem last year:

so I’m curious if there’s a specific version of our zip-it-and-ship-it module that’s causing this.

Hi Jen
distracted-rosalind-fec89b is the Netlify site name with one of the failed deploys log can be found here https://www.dropbox.com/s/qwy5e6oa0crmwfa/deplay%20failure.rtf?dl=0.

Ive currently installed encoding as a dependency with the plan to switch to axios, would be good to not have to change for no reason.

@jen Not who you asked originally, but I’m also experiencing this problem with the encoding module causing a build to fail.

Site name: csis-careers
Deploy log: https://gist.github.com/jnschrag/d65282d323c503a0340b0e3d23690db3

Any guidance on how to resolve the issue is much appreciated! Also happy to provide more info if helpful.

Hello! Sadly, the fix I mentioned above is in a version of our zip-it-and-ship-it module that has not rolled out across our platform yet.

The options for now seem to be:

  • If you’re deploying via the CLI (@ag-labs), the next version of the CLI that we release on npm will include this fix. That said, our CLI is on GitHub now, so you could also download it directly before we’ve packaged the release for npm:
    https://github.com/netlify/cli

  • If you’re deploying via a Git provider (@jschrag), the fix is available now through our Build Plugins beta, and will roll out across the board at the end of May. Here’s where you can pick which sites to enable the Build Plugins beta for:
    https://app.netlify.com/teams/YOUR-USERNAME/builds/

  • If neither of these sounds good, you could go the route described above: include ‘encoding’ as a dependency of your project.

Whew, that should cover it! Please let us know if you try these and continue to run into issues.

Thank you for the info, @jen

I’ll wait for the end of May and hope the fix is available through Build Plugins.

I’ve run into the same problem just last night when I decided to use ‘node-fetch’ in my build, I’ll try the suggestion to include ‘encoding’ in my build as well.

1 Like

Hey! I hit this problem to on node-fetch. If you upgrade to v3 of node-fetch they no longer use encoding so it goes away. node-fetch v3 is still beta, and can be install by using

npm install node-fetch@next

Hopefully it will move out of beta soon, but I’ve been using it and had zero problems with it. The beta seems quite stable at this point.

2 Likes

Hi guys, I had the same issue with node-fetch & encoding. I switched to isomorphic-fetch and it solved the problem.

Hope you find it helpful.

1 Like

Hey @trungbauz, appreciate the feedback and glad to hear that you’ve found a solution that works.

Added “encoding” to package.json

Worked for me.
NB! Had to remove package-lock as well :wink:

1 Like