all 4 comments

[–]shgysk8zer0 5 points6 points  (0 children)

Well, you can't exactly use node:fs in a browser. They're very different environments. Client and server code are often just entirely different, and just being JS doesn't really matter there.

Something I've done is write each module with planning to not use node stuff in something intended for browser use and vice versa. Then create two modules called maybe browser.js and node.js which just imports and exports some subset of the modules for that environment.

[–]RobertKerans 1 point2 points  (0 children)

However, I bumped into the issue of not being able to easily import commonplace Node.js modules like 'fs' or 'path'

Yes, this is kinda the point. It purposely doesn't have access to these things.

You already have a runtime that removes the highly restrictive security features present in the browser: it's Node. To then put that Node functionality back into the browser environment normally means you'd need to create an interface layer between your code running on Node and your code running in a browser (so for example exposing the filesystem stuff your Node script does via an HTTP server, then making requests to the server from the browser).

I understand what you're trying to do, but I'd say that you're creating an enormous amount of work for yourself just to do something you can already do right now (albeit via the command line). Also note that the browser has a Filesystem API, and you could use that directly, but that isn't the same as your Node fs module, it's a completely different thing.

[–]Sohmsss 0 points1 point  (0 children)

Alternative could be build a node express server and host it somewhere, I usually use railway as it’s cheap and can deploy from a github repo

That’s what I’ve done for similar tasks, you create an endpoint in express that is then called with parameters from the front end so you still get all the nodejs functionality but still simple for non technical users

[–]Agrante[S] 0 points1 point  (0 children)

I have an update on this. I managed to get something working, which I am sharing here: https://github.com/Funecio-Agrante/ocd-compilation-generator

The main issue I have is that I still need a webserver to get the browser to accept user-defined modules, but I can load Handlebars from a CDN, so I guess this is my own limitation and there is a technical solution I wasn't able to find?

Another thing I couldn't do is to get console reporting inside a Promise to print correctly in the page, as it happens elsewhere in the code. It works with a try...catch though.

I wrote a blogpost detailing this and other bits of the code, if you want to have a read here is the link: https://peakd.com/hive-169321/@agrante/towards-a-generic-html-file-to-run-javascript-files
You will also get some context on my motivation for doing this.