I'm having troubles with the node module "fs"

26 views Asked by At

I created a local server using Node, and my objective would be to use the 'fs' module to be able to open my .exe file. However, when I run my code it gives the following error "Uncaught ReferenceError: fs is not defined at HTMLButtonElement. ((index):16:11)".

Below is my complete code: enter image description here

I hope someone could help me to fix that error.

1

There are 1 answers

0
SpaghettyCodes On

You're trying to execute NodeJS (server) code inside a browser (client) environment. Javascript running in the browser does not have access to the OS-level features Node has such as the file system.

This is by design -- arbitrary Javascript could be getting loaded in every time you load any page on the internet. If that arbitrary Javascript could directly access your filesystem or other OS features it would be pretty dangerous to visit anywhere on the internet (more dangerous than it already is!), and so browser Javascript is limited only to the set of features that the browser it is running in exposes to it.

If you still want to create a program that renders a UI allowing you to open up a local executable, everything will need to run on your Node program. You can build a basic UI on your desktop with electron that opens your executable.

You won't be able to actually run your executable with the fs.open call that you're doing, you'll need to use some Node functions specifically for running executable files instead (see this question).

I'd recommend starting with building a program that runs your exe without any UI first, and then when you have that working well trying to layer your UI onto it.