Pluggable widget : factoryThrew Error: factoryThrew -- Loading module widgets/siemens/myfifthwidget/MyFifthWidget failed! Check script file for syntax errors.

1
Hi everyone, I am implementing a custom widget to update content in a text file. I have tried lot of approaches like fs, fs-extra . they are showing like “fs exported by fs?commonjs-external but cannot be resolved” . then I am trying to file-system. Now there is no error while building the widget. After paste it into our Mendix project widget folder, I was Run Locally to see the result. When I open a page which containing this custom widget , I am getting the error as below . Pls Guide me how to resolve this error. const { fs } = require("../../node_modules/file-system"); exports.myTest = () => { var stream = fs.WriteStream("D:/Vijayakumar/WriteFromCustomWidget.txt"); stream.once("open", function (fd) { stream.write("My first row\n"); stream.write("My second row\n"); stream.end(); }); }; factoryThrew Error: factoryThrew at d (http://localhost:8081/mxclientsystem/mxui/mxui.js?637666153178475187:5:553) at http://localhost:8081/mxclientsystem/mxui/mxui.js?637666153178475187:5:9535 at Me (http://localhost:8081/mxclientsystem/mxui/mxui.js?637666153178475187:5:9644) at Me (http://localhost:8081/mxclientsystem/mxui/mxui.js?637666153178475187:5:9280) at http://localhost:8081/mxclientsystem/mxui/mxui.js?637666153178475187:5:9835 at Fe (http://localhost:8081/mxclientsystem/mxui/mxui.js?637666153178475187:5:9702) at Be (http://localhost:8081/mxclientsystem/mxui/mxui.js?637666153178475187:5:9774) at i (http://localhost:8081/mxclientsystem/mxui/mxui.js?637666153178475187:5:10909) at HTMLScriptElement.<anonymous> (http://localhost:8081/mxclientsystem/mxui/mxui.js?637666153178475187:5:13143) Loading module widgets/siemens/myfifthwidget/MyFifthWidget failed! Check script file for syntax errors.
asked
3 answers
0

we haven't found the root cause. initially we do clear cache / incognito mode. then we don't see that error. later someone suggested to check our index.html file having one line of code cacheBust: "{{cachebust}}" available or not. in our case it has some hardcoded text. so then we removed that line updated with above line. then we dont see that error. I am not sure, this is the root cause.

answered
0

Isnt this approach kind of wrong?
Like, there are few red flags i see.
1. Using commonjs require in a pluggable widget(which runs on the browser) is not something that can be done - sure it will build but it wont work. That is a backend package that you are using!

2. saving a file to you filesystem using browser APIs is something that puts me off. (I believe that Browser doest have access to the Filesystem directly!)

If you have time can you try the below approaches?

1. Change the library approach that you are using to create and save the file.
use the Browser Blob API to create a blob data, then write it to an in-memory file which you attach it to you DOM under an onclick which on click can present itself for downloading. See the code snippet below (you can use the JS part in your widget code)
 

<!DOCTYPE html> 
<html> 
	<head> 
		<title>Generate a Text File</title> 
	</head> 
	<body> 
        <button onclick='download(blob,"dummy-file.txt");'>Download</button>
		<script type="text/javascript"> 
 
			// create the text file as a Blob: 
            // your content goes here!!!
			var blob = new Blob(["hello world"],{type: "text/plain"}); 

 
			function download(blob,name) { 
				var url = URL.createObjectURL(blob), 
					div = document.createElement("div"), 
					anch = document.createElement("a"); 
 
				document.body.appendChild(div); 
				div.appendChild(anch); 
 
				anch.innerHTML = "&nbsp;"; 
				div.style.width = "0"; 
				div.style.height = "0"; 
				anch.href = url; 
				anch.download = name; 
				 
				var ev = new MouseEvent("click",{}); 
				anch.dispatchEvent(ev); 
				document.body.removeChild(div); 
			} 
 
		</script> 
	</body> 
</html> 

2. Make a Microflow trigger which saves the file on the server. then you can present a download button to the user.

good luck.

answered
-1

Is your problem solved now?Is your problem solved now?

answered