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] Netlify App

2 Likes

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] node-fetch/body.js at 95286f52bb866283bc69521a04efe1de37b26a33 · node-fetch/node-fetch · GitHub
[1] node-fetch/package.json at main · node-fetch/node-fetch · GitHub
[3] `node-fetch@2.0.0-alpha.6` throws error "Cannot find module 'encoding'" · Issue #316 · node-fetch/node-fetch · GitHub

4 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:
https://github.com/netlify/zip-it-and-ship-it/pull/35

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 Dropbox - File Deleted.

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: CSIS Careers deploy log - encoding failed · GitHub

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:
    GitHub - netlify/cli: Netlify Command Line Interface

  • 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:
    Netlify App

  • 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.

4 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

just ran into this.

Builds were working fine last night, and I already had node-fetch included.

So I’m not sure what changed. But npm install encoding and then rebuilding worked.

Confirming that this issue still exists. Using Node 12.x for our build and the error only appears deployed to Netlify. Even using netlify cli doesn’t have this issue. I switched from node-fetch to isomorphic-fetch as @trungbauz but the error persisted.

From deployment attempt:

10:20:18 AM:   Error message
10:20:18 AM:   A Netlify Function failed to require one of its dependencies.
10:20:18 AM:   Please make sure it is present in the site's top-level "package.json".
​
10:20:18 AM:   In file "/opt/build/repo/lambda/forwardEmail.js"
10:20:18 AM:   Cannot find module 'encoding'
10:20:18 AM:   Require stack:
10:20:18 AM:   - /opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/node_modules/@netlify/zip-it-and-ship-it/src/node_dependencies/resolve.js
10:20:18 AM:   - /opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/node_modules/@netlify/zip-it-and-ship-it/src/node_dependencies/index.js
10:20:18 AM:   - /opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/node_modules/@netlify/zip-it-and-ship-it/src/main.js
10:20:18 AM:   - /opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/src/plugins_core/functions/index.js
10:20:18 AM:   - /opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/src/commands/get.js
10:20:18 AM:   - /opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/src/core/main.js
10:20:18 AM:   - /opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/src/core/bin.js

Also confirming that simply adding encoding to my dependencies fixed it. :face_with_raised_eyebrow:

1 Like