domino icon indicating copy to clipboard operation
domino copied to clipboard

Angular 8 universal not working gives an error strict mode code may not contain 'with' statements

Open Garudkar-Dnyaneshwar opened this issue 5 years ago • 4 comments

This error comes to me via usage of Angular Universal. When attempting to serve my development server and accessing any of the pages, I am greeted to a blank screen and this error in the console:

SyntaxError: strict mode code may not contain 'with' statements

specifically pointing to this line as the issue: './node_modules/domino/lib/sloppy.js'

module.exports = { Window_run: function _run(code, file) { if (file) code += '\n//@ sourceURL=' + file; with(this) eval(code); ==>>>showing error }, EventHandlerBuilder_build: function build() { try { with(this.document.defaultView || Object.create(null)) with(this.document) with(this.form) with(this.element) return eval("(function(event){" + this.body + "})"); } catch (err) { return function() { throw err; }; } } };

Garudkar-Dnyaneshwar avatar Oct 22 '20 20:10 Garudkar-Dnyaneshwar

any updates , i faced this issue

khaled-elrifaay avatar Jul 26 '21 17:07 khaled-elrifaay

any updates , i faced this issue

Nope, I tried everything but cant find any solution. So finally I migrate my whole project into nextjs

Garudkar-Dnyaneshwar avatar Jul 26 '21 17:07 Garudkar-Dnyaneshwar

solution

ummm okay thanks

khaled-elrifaay avatar Jul 26 '21 18:07 khaled-elrifaay

Depending on your use case you can use patch-package to remove the non-compliant code. I'm using domino as a dependancy of turndown and as far as I can tell sloppy.js isn't needed in my case.

I created the patch file:

diff --git a/node_modules/domino/lib/sloppy.js b/node_modules/domino/lib/sloppy.js

index b5d8950..e920db1 100644
--- a/node_modules/domino/lib/sloppy.js
+++ b/node_modules/domino/lib/sloppy.js
@@ -6,19 +6,9 @@
 /* jshint -W085 */
 module.exports = {
   Window_run: function _run(code, file) {
-    if (file) code += '\n//@ sourceURL=' + file;
-    with(this) eval(code);
+    console.log("Window_run removed")
   },
   EventHandlerBuilder_build: function build() {
-    try {
-      with(this.document.defaultView || Object.create(null))
-        with(this.document)
-          with(this.form)
-            with(this.element)
-              return eval("(function(event){" + this.body + "})");
-    }
-    catch (err) {
-      return function() { throw err; };
-    }
+    console.log("EventHandlerBuilder_build removed")
   }
 };

which I deploy to the server by adding "postinstall": "patch-package"

as a package.json script.

This solved the problem for me.

jb-modist avatar Nov 05 '21 17:11 jb-modist