Could not find "encoding" module in file. Only when build using netlfiy

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

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!)