Electron, After Browserify, Fs.Existssync Is Not a Function

electron, after browserify, fs.existsSync is not a function

The conceptual solution for this problem:
in "electron",

"{dialog}=require(electron)" (e.g.,)
in tabs, will not work

This is how they built electron.
probably to increase stability or just part of the issue that's based on chrome

in the main process, we can require(electron), where in tabs, we are to require npm modules with browserify
*** check for example
what modules work where in electron

Browserify / Electron / AngularJS Error: fs.existsSync is not a function

I've managed to solve my error without browserify by enabling nodeIntegration: true in main.js file (electron entry file). Then I was able to load my script (coolscript.js) which has require function inside of it to my html without problems -

<script>
require('coolscript.js')
</script>

How to resolve fs.existsSync is not a function

You can allow webpack to use the Node's require and include fs etc. by targeting node in the config:

module.exports = {
entry: './src/main.js',
target: 'node',
output: {
path: path.join(__dirname, 'build'),
filename: 'backend.js'
}
}

As described here: https://webpack.js.org/concepts/targets/ and https://webpack.js.org/configuration/target/

Browserify with require('fs')

Which filesystem should the browser use then? The HTML5 filesystem is not really comparable to a traditional filesystem. It doesn't have symlinks, and it is only accessible asynchronously outside Web Workers.

So the answer is: Write an abstraction layer yourself that can rely on the fs module when running in Node.js, and the HTML5 FS API when running in the browser. The differences are too large to have browserify translate for you.

fs.readFileSync is not a function?

You can not use "fs" in browsers because it is a Node.js specific library that enables file system operations on OS level. Browser do not support these low-level APIs so you can unfortunately only use it in Node.js.

Google currently develops a browser based file system api, maybe this helps.

Btw Selenium will also not work because it spawns another browser again with OS level APIs.

readFileSync is not a function

Node.js does not use Require.js. Require.js was built so that you could have asynchronous module loading on the client-side (in your browser).

Node.js uses CommonJS style modules. Your code using CommonJS would look like this:

var fs = require('fs');
console.log("\n *STARTING* \n");
var contents = fs.readFileSync("sliderImages", "utf8");

If we assume you saved this in a file called main.js you would then enter this command in your console (make sure you are in the same directory as the file):

node main.js

This code will not run in the browser. Node.js runs on the server. If you want to load a JSON file on the browser side then you'll need to load it using AJAX. There are numerous resources available to show you how to do this. Be aware that you must either run your page from a server or have a special flag enabled to load in files from the file system.



Related Topics



Leave a reply



Submit