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
Error Ts2739: Type '{}' Is Missing the Following Properties from Type
How to Disable Button After (Click) in Angular
How to Display the Entire Object in Console in React Native
How to Add Button on Each Row in Datatable
How to Refresh Specific Div Using Javascript/Jquery With the Variables on It
Call 2 Functions Within Onchange Event
How to Change Image Source on Hover Using Javascript, But Applies to Each Individual Photo
How to Count Text Lines Inside an Dom Element Can I
Updating Array in Object Properties Using Property'S Name
Chat Box, Auto Scroll to Bottom
How to Parse Xml File in React
Cannot Open Local File - Chrome: Not Allowed to Load Local Resource
Redirecting With JavaScript If a Radiobox Is Checked
Firebase.Firestore() Is Not a Function When Trying to Initialize Cloud Firestore
How to Convert Uint8 Array to Base64 Encoded String
Only Allowing Numbers Upto Certain Length in Material Ui
Youtube API - Failed to Execute 'Postmessage' on 'Domwindow'