# Netlify Serverless Typescript functions fail with dynamic requires

**URL:** <https://answers.netlify.com/t/netlify-serverless-typescript-functions-fail-with-dynamic-requires/52379>\
**Category:** Support\
**Tags:** lambda-functions\
**Created:** [February 22, 2022, 4:29am UTC](https://answers.netlify.com/t/netlify-serverless-typescript-functions-fail-with-dynamic-requires/52379 "2022-02-22T04:29:51Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinh-perfectstorm](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/vinh-perfectstorm/32/34648_2.png) [@vinh-perfectstorm](https://answers.netlify.com/u/vinh-perfectstorm)\
**Post date:** [February 22, 2022, 4:29am UTC](https://answers.netlify.com/t/netlify-serverless-typescript-functions-fail-with-dynamic-requires/52379/1 "2022-02-22T04:29:51Z")

</div>

Typescript serverless functions seem to be failing for me with

```auto
{"errorType":"Error","errorMessage":"Dynamic require of \"util\" is not supported","trace":["Error: Dynamic require of \"util\" is not supported"," at __require (file:///var/task/functions/data.js:28:9)"," at node_modules/util-deprecate/node.js (file:///var/task/functions/data.js:86:22)"," at__require2 (file:///var/task/functions/data.js:31:44)"," at node_modules/faunadb/src/query.js (file:///var/task/functions/data.js:6328:21)"," at __require2 (file:///var/task/functions/data.js:31:44)"," at node_modules/faunadb/index.js (file:///var/task/functions/data.js:8742:17)"," at__require2 (file:///var/task/functions/data.js:31:44)"," at file:///var/task/functions/data.js:8760:33"," at ModuleJob.run (internal/modules/esm/module_job.js:183:25)"," at process.runNextTicks [as _tickCallback] (internal/process/task_queues.js:60:5)"]}

```

Regular .js functions with the same exact code works fine.

I’m using sveltekit and faunadb

---

<div class="post-metadata">

**Author:** ![mikenikles](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/mikenikles/32/26075_2.png) [@mikenikles](https://answers.netlify.com/u/mikenikles)\
**Post date:** [February 22, 2022, 2:22pm UTC](https://answers.netlify.com/t/netlify-serverless-typescript-functions-fail-with-dynamic-requires/52379/2 "2022-02-22T14:22:11Z")

</div>

We encounter the same thing.

The last successful function execution I see was at Feb 21, 02:58:10 AM PST and the first one that threw the exception was at Feb 21, 06:40:53 AM PST.

There are no deployments from our side in this time period.

This is currently a high priority issue for us as it blocks important Netlify functions from running.

---

<div class="post-metadata">

**Author:** ![qubyte](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/qubyte/32/73093_2.png) [@qubyte](https://answers.netlify.com/u/qubyte)\
**Post date:** [February 24, 2022, 4:20pm UTC](https://answers.netlify.com/t/netlify-serverless-typescript-functions-fail-with-dynamic-requires/52379/3 "2022-02-24T16:20:49Z")

</div>

I’m also seeing this. My setup hasn’t changed, but functions started failing sometime between Monday 21st and now.

---

<div class="post-metadata">

**Author:** ![mikenikles](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/mikenikles/32/26075_2.png) [@mikenikles](https://answers.netlify.com/u/mikenikles)\
**Post date:** [February 24, 2022, 4:36pm UTC](https://answers.netlify.com/t/netlify-serverless-typescript-functions-fail-with-dynamic-requires/52379/4 "2022-02-24T16:36:09Z")

</div>

I’ve had a support email unanswered for two days about that issue ☹.

We now started to replicate our Netlify Functions as GCP Cloud Functions to ensure mission-critical code continues to be available and to have more redundancy - and a migration path off Netlify should we need to go down that path.

Btw, another idea may be to use `tsc` or similar to convert the Typescript functions to Javascript before deploying, but I feel that’s less ideal than what I described with GCP above.

---

<div class="post-metadata">

**Author:** ![qubyte](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/qubyte/32/73093_2.png) [@qubyte](https://answers.netlify.com/u/qubyte)\
**Post date:** [February 24, 2022, 4:46pm UTC](https://answers.netlify.com/t/netlify-serverless-typescript-functions-fail-with-dynamic-requires/52379/5 "2022-02-24T16:46:37Z")

</div>

My functions are written in plain JS, but with ES modules. Perhaps the way esbuild is used has changed somehow.

---

<div class="post-metadata">

**Author:** ![vinh-perfectstorm](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/vinh-perfectstorm/32/34648_2.png) [@vinh-perfectstorm](https://answers.netlify.com/u/vinh-perfectstorm)\
**Post date:** [February 24, 2022, 5:52pm UTC](https://answers.netlify.com/t/netlify-serverless-typescript-functions-fail-with-dynamic-requires/52379/6 "2022-02-24T17:52:40Z")

</div>

Yea, i’m using tsc to pre compile the scripts beforehand and since netlify automatically uses JS files before TS files it seems to be a valid workaround

---

<div class="post-metadata">

**Author:** ![hrishikesh](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/hrishikesh/32/74607_2.png) [@hrishikesh](https://answers.netlify.com/u/hrishikesh)\
**Post date:** [February 24, 2022, 6:49pm UTC](https://answers.netlify.com/t/netlify-serverless-typescript-functions-fail-with-dynamic-requires/52379/7 "2022-02-24T18:49:16Z")

</div>

Could one of you provide a reproduction of this?

EDIT: Also, this is probably related: [Working on netlify dev, not on actual deploy · Issue #1036 · netlify/zip-it-and-ship-it · GitHub](https://github.com/netlify/zip-it-and-ship-it/issues/1036)

---

<div class="post-metadata">

**Author:** ![vinh-perfectstorm](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/vinh-perfectstorm/32/34648_2.png) [@vinh-perfectstorm](https://answers.netlify.com/u/vinh-perfectstorm)\
**Post date:** [February 24, 2022, 7:11pm UTC](https://answers.netlify.com/t/netlify-serverless-typescript-functions-fail-with-dynamic-requires/52379/8 "2022-02-24T19:11:12Z")

</div>

as soon as i try to use faunadb or firebase-admin i immediatly get Dynamic Require Errors…

for example (typescript file)

Path: .netlify/functions/faunaTest.ts

```auto
import faunadb from 'faunadb' /* Import faunaDB sdk */

/* configure faunaDB Client with our secret */
const q = faunadb.query
const client = new faunadb.Client({
  secret: process.env.FAUNADB_SECRET
})

export const handler = async function (event, context) {

	const t = {success: "success"};
	return {
		statusCode: 200,
		body: "success"
	};
};

```

---

<div class="post-metadata">

**Author:** ![vinh-perfectstorm](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/vinh-perfectstorm/32/34648_2.png) [@vinh-perfectstorm](https://answers.netlify.com/u/vinh-perfectstorm)\
**Post date:** [February 24, 2022, 7:12pm UTC](https://answers.netlify.com/t/netlify-serverless-typescript-functions-fail-with-dynamic-requires/52379/9 "2022-02-24T19:12:32Z")

</div>

yes, appears to be same issue

---

<div class="post-metadata">

**Author:** ![eduardoboucas](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/eduardoboucas/32/20463_2.png) [@eduardoboucas](https://answers.netlify.com/u/eduardoboucas)\
**Post date:** [February 24, 2022, 7:28pm UTC](https://answers.netlify.com/t/netlify-serverless-typescript-functions-fail-with-dynamic-requires/52379/10 "2022-02-24T19:28:59Z")

</div>

Hi all,

Netlify engineer here. We’ve been making some changes around our ES Modules support that could explain a different behaviour in esbuild. I’ve disabled the new logic temporarily — could you let me know if you’re still experiencing the issue?

Thanks!

---

<div class="post-metadata">

**Author:** ![vinh-perfectstorm](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/vinh-perfectstorm/32/34648_2.png) [@vinh-perfectstorm](https://answers.netlify.com/u/vinh-perfectstorm)\
**Post date:** [February 24, 2022, 8:03pm UTC](https://answers.netlify.com/t/netlify-serverless-typescript-functions-fail-with-dynamic-requires/52379/11 "2022-02-24T20:03:09Z")

</div>

It looks to be working now.  
I am no longer seeing a Dynamic Require error

---

<div class="post-metadata">

**Author:** ![perry](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/perry/32/16_2.png) [@perry](https://answers.netlify.com/u/perry)\
**Post date:** [February 24, 2022, 8:16pm UTC](https://answers.netlify.com/t/netlify-serverless-typescript-functions-fail-with-dynamic-requires/52379/12 "2022-02-24T20:16:43Z")

</div>

great. we have switched off the code that make the error happened, and will continue investigating. We’ll keep you posted.

If anyone still sees the error, please let us know!

---

<div class="post-metadata">

**Author:** ![fool](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/fool/32/37_2.png) [@fool](https://answers.netlify.com/u/fool)\
**Post date:** [February 25, 2022, 1:03am UTC](https://answers.netlify.com/t/netlify-serverless-typescript-functions-fail-with-dynamic-requires/52379/13 "2022-02-25T01:03:09Z")

</div>

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](https://github.com/evanw/esbuild/issues/1921), 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.

---

<div class="post-metadata">

**Author:** ![mikenikles](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/mikenikles/32/26075_2.png) [@mikenikles](https://answers.netlify.com/u/mikenikles)\
**Post date:** [March 1, 2022, 11:50pm UTC](https://answers.netlify.com/t/netlify-serverless-typescript-functions-fail-with-dynamic-requires/52379/14 "2022-03-01T23:50:47Z")

</div>

Incredible transparency 😍. Thank you for the post-mortem!

---

<div class="post-metadata">

**Author:** ![VaughnVernon](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/vaughnvernon/32/31414_2.png) [@VaughnVernon](https://answers.netlify.com/u/VaughnVernon)\
**Post date:** [October 5, 2022, 5:29pm UTC](https://answers.netlify.com/t/netlify-serverless-typescript-functions-fail-with-dynamic-requires/52379/15 "2022-10-05T17:29:44Z")

</div>

I am experiencing this problem for the first time today with a newly published release. There was no problem for our Friday release, and I have found nothing apparent in our commits since.

Were the February 21 changes rolled out again?

cc: @fool

---

<div class="post-metadata">

**Author:** ![VaughnVernon](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/vaughnvernon/32/31414_2.png) [@VaughnVernon](https://answers.netlify.com/u/VaughnVernon)\
**Post date:** [October 6, 2022, 12:03am UTC](https://answers.netlify.com/t/netlify-serverless-typescript-functions-fail-with-dynamic-requires/52379/16 "2022-10-06T00:03:40Z")

</div>

This was pretty difficult to fix. This is what was found:

> [@Crashing Every Page, Even Static](https://answers.netlify.com/t/crashing-every-page-even-static/76856/2):
>
> This is fixed, which required a full day. It was as simple as clearing the Netlify cache before deploying. Actually it was not simple because it wasn’t apparent that the operation exists or even to try it. It seems that clearing the cache removes the node\_modules directory, which can be important even with what seems to be basic updates to dependencies.
