Skip to content

How to use FS module inside Electron.AtomWebPack application?

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?

Answer

Problem is solved.

Need use in electron app (where you add the bundle):

var remote = require('electron').remote;
var electronFs = remote.require('fs');
var electronDialog = remote.dialog;