Home
Support Forums

Lambda functions + fastify error: "Cannot find module string_decoder"

Hello, I’ve built a simple lambda function using netlify.

Git Repository: GitHub - edgar1506/netlify_question: POC NEtlify Fastify
Live site: https://competent-shockley-e44c09.netlify.app/api/

When I deploy locally using netlify dev, everything works fine:

GET requests work fine:

Request:

GET https://competent-shockley-e44c09.netlify.app/api/ HTTP/1.1

Response:

HTTP/1.1 200 OK
...
{
  "data": "dummy GET"
}

Request:

POST https://competent-shockley-e44c09.netlify.app/api/ HTTP/1.1

Response:

HTTP/1.1 200 OK
...
{
  "data": "dummy POST"
}

Request

POST https://competent-shockley-e44c09.netlify.app/api/ HTTP/1.1
Content-Type: application/json

{
    "myKey": "myValue"
}

Response:

HTTP/1.1 200 OK
...
{
  "data": "dummy POST"
}

However, when I run the deployed version, I start getting an error with POST requests than include a content body (all other requests listed above except the onw with content body work fine online too):

Request

POST https://competent-shockley-e44c09.netlify.app/api/ HTTP/1.1
Content-Type: application/json

{
    "myKey": "myValue"
}

Response:

HTTP/1.1 502 Bad Gateway

{
  "errorType": "Error",
  "errorMessage": "Cannot find module 'string_decoder/'\nRequire stack:\n- /var/task/src/function.js\n- /var/task/function.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
  "trace": [
    "Error: Cannot find module 'string_decoder/'",
    "Require stack:",
    "- /var/task/src/function.js",
    "- /var/task/function.js",
    "- /var/runtime/UserFunction.js",
    "- /var/runtime/index.js",
    "    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)",
    "    at Function.Module._load (internal/modules/cjs/loader.js:667:27)",
    "    at Module.require (internal/modules/cjs/loader.js:887:19)",
    "    at require (internal/modules/cjs/helpers.js:74:18)",
    "    at Request.Readable.setEncoding (/var/task/src/function.js:3949:25)",
    "    at rawBody (/var/task/src/function.js:21964:17)",
    "    at ContentTypeParser.run (/var/task/src/function.js:21931:9)",
    "    at handleRequest (/var/task/src/function.js:14883:43)",
    "    at runPreParsing (/var/task/src/function.js:25052:9)",
    "    at Object.routeHandler [as handler] (/var/task/src/function.js:25020:11)"
  ]
}

As far as I understand, string_decoder is a core Node module, and it works fine on my computer, but it doesn’t seem to find it in Netlify’s Node installation?

Hey there,

Just out of interest, have you tried bundling or including the string_decoder dependency in your function?

Helo, thanks for reaching out. Yes, I included import stringDecoder from "string_decoder" and still got the same error.

I ultimately had to create my own build script using esbuild and include it in the command option in the netlify.toml file. The custom build script generates a single .js file into the build/ dir and I had to set up the netlify.toml file to use that directory for the functions.

Now it would be good to know why the zip it and ship it process is failing in this case. I could not find the reason or the solution…

Were you using esbuild when letting zip it and ship it build it? Or was it set to default?

Hello, there was a node_bundler = "esbuild" in my netlify.toml file as suggested in netlify’s documentation because I was using import instead of require. You can check the POC in my repo:

It’s a guess and I might be off, but chances are, either it was a problem with esbuild’s old version which Netlify might be currently using, or there might be some problem caused when Netlify forked and might have modified the original esbuild to use in zip-it-and-ship-it.

Is there a way to verify this? if the problem was caused by an old version of esbuild, other people might start getting errors too in the future…

If this is the case, wouldn’t it have failed because I am using module import instead of require?

Hey there, @edgar :wave:

Thanks for your patience here! We have not forgotten about you, and have looped in another team at Netlify to take a look. We will follow up here once we have more information for you :slight_smile:

1 Like

Thanks a lot! I hope it’s not a mistake on my side :wink:

Hi @edgar!

Thanks for the detailed report. After some investigation, this seems to be an issue with esbuild. I’ll follow up and try to fix the underlying cause, but in the meantime you can get unblocked by adding this to your netlify.toml:

[functions]
  external_node_modules = ["readable-stream"]

This essentially tells esbuild to treat readable-stream (the module that requires string_decoder) as an external module, so the esbuild resolving logic will be bypassed.

Let us know if you still encounter any issues after this.

Thanks for your patience!

2 Likes

Hello @eduardoboucas, sorry for the delay in my response.

Thanks a lot for looking into the issue, woks like a charm now!

Regards.

1 Like