Skip to content

Instantly share code, notes, and snippets.

@Thinkscape
Last active May 7, 2024 14:20
Show Gist options
  • Save Thinkscape/ca9a7e47e3b44a5cc92d8e552f455eb6 to your computer and use it in GitHub Desktop.
Save Thinkscape/ca9a7e47e3b44a5cc92d8e552f455eb6 to your computer and use it in GitHub Desktop.
Fix lodash edge compatibility (vercel, cloudflare workers)

Lodash and Edge Runtimes

See: lodash/lodash#5862

Lodash uses Function(String) which upsets Next.js and is incompatible with CloudFlare workers (Vercel Edge), resulting in errors like:

Failed to compile.
Dynamic Code Evaluation (e. g. 'eval', 'new Function', 'WebAssembly.compile') not allowed in Edge Runtime

Apply those patches to work around the issue. Tested with Lodash 3.0.1.

How to use

--- node_modules/lodash/_root.js 2023-10-16 14:58:12.549911617 +1100
+++ node_modules/lodash/_root-patched.js 2023-11-10 13:28:15.641779573 +1100
@@ -4,6 +4,6 @@
var freeSelf = typeof self == 'object' && self && self.Object === Object && self;
/** Used as a reference to the global object. */
-var root = freeGlobal || freeSelf || Function('return this')();
+var root = freeGlobal || freeSelf;
module.exports = root;
--- node_modules/lodash._root/index.js 2023-10-16 14:58:13.848970002 +1100
+++ node_modules/lodash._root/index-patched.js 2023-11-10 13:26:58.912392321 +1100
@@ -43,7 +43,7 @@
*/
var root = freeGlobal ||
((freeWindow !== (thisGlobal && thisGlobal.window)) && freeWindow) ||
- freeSelf || thisGlobal || Function('return this')();
+ freeSelf || thisGlobal;
/**
* Checks if `value` is a global object.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment