Netlify Serverless Typescript functions fail with dynamic requires

On February 21st at 09:33 UTC, we rolled out an update to the Netlify Functions bundling logic that changed the module format of functions in certain conditions. Rather than transpiling all functions using ES Modules to CommonJS, as we’d been doing since we introduced support for that format, we started generating pure ESM functions under certain conditions:

  1. When the configured functions runtime environment is Node 14, and
  2. When there is a package.json file with {"type": "module"} as an ancestor of the function file.

If any of the conditions above weren’t met, we continued transpiling the functions to CJS.When we started generating ESM functions, we inadvertently hit a known issue with esbuild, the open-source JavaScript bundler we use internally. This issue makes esbuild generate faulty code when:

  1. An ESM file imports a CJS file, and
  2. The CJS file requires a Node built-in module (like fs or path ), and
  3. The target format of esbuild is set to esm

We’ve since rolled back the update and we’re back to transpiling all ESM functions to CJS until we have a temporary solution in place. We’ve also updated our test suite to include this specific setup, to ensure we don’t see this regression again.

4 Likes