How to use FS module inside Electron.Atom\WebPack application?


#1

Hi guys. I need write some data in the file, using FS module (fs.writeFile). My stack is webpack + react + redux + electron.

The first problem was: Cannot resolve module ‘fs’.
I tried to use
target: “node”,

node: {
global: true,
fs: “empty”,
}

resolve: {
root: path.join(__dirname),
fallback: path.join(__dirname, ‘node_modules’),
modulesDirectories: [‘node_modules’],
extensions: [’’, ‘.json’, ‘.js’, ‘.jsx’, ‘.scss’, ‘.png’, ‘.jpg’, ‘.jpeg’, ‘.gif’]
},

After several attempts, the problem is resolved ( node: {fs: “empty”} ). But then there was a second problem: screenshot.

//In method componentDidMount (React)
console.log('fs', fs);
console.log('typeOf', typeof fs.writeFile);

//By clicking on the button
console.log(fs);
console.log(typeof fs.writeFile);

You can see, that fs is empty object, and method writeFile no exists. I tried to change the webpack’s configuration.
const path = require(‘path’);
const fs = require(‘fs’);
const webpack = require(“webpack”);
console.log(fs);

In this case fs is not empty.

How to solve this problem? Any ideas?


#2

I have the same issue :confused: Any updates?


#3

Same pbm for me !
Hope someone have a solution


#4

Hi guys. There is a solution:

var {remote} = require('electron');
var electronFs = remote.require('fs');
electronFs.writeFile(...)

#5

index.html

<html>
...
    <script src="js/electron.js"></script>
...
</html>

electron.js

electron = require('electron')
fs = electron.remote.require('fs')

fs.writeFile(path, content, (err) => {
      if (err) throw err;
      console.log('It\'s saved!');
});

you should customize “path” and “content”.
path is file path and content is a writing.
i think that my code isn’t good code. because i started “Electron” before 1week.


#6

I have the same issue :confused: Any updates?