M
M
Max2016-06-23 14:36:06
Node.js
Max, 2016-06-23 14:36:06

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

Hello. There is a task to open a file through input[type=file] to read data from it and write new ones to it.
The first part is easily solved using the HTML5 File Api. But writing new data into it is already problematic.
Used by webpack + react + redux. When all this is built, the result is wrapped using electron.atom to get a desktop application.
To solve the problem, I tried to use fs.writeFile , but then failure awaited me:
The first stone turned out to be:
Cannot resolve module 'fs'
Tried to solve in such images:

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']
},

As a result, after the Nth attempt, the option with node: {fs: "empty"} helped.
But the joy did not last long, because the second stone immediately appeared:
Q2KVq7Xu98kyLm.jpg
I brought this to the console:
//В реактовском методе componentDidMount 
console.log('fs', fs);
console.log('typeOf', typeof fs.writeFile);

//При клике на кнопку
console.log(fs);
console.log(typeof fs.writeFile);

As you can see, fs turned out to be an empty object, and the writeFile method is accordingly undefined.
If everything is built in this way, and wrapped in an electron, then the fs object is also empty. And it turns out that the files will not work.
The most interesting thing is that if the webpack config is made like this:
const path = require('path');
const fs = require('fs');
const webpack = require("webpack");
console.log(fs);

then fs looks correct and is not empty.
How would you solve this problem? Any ideas?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2016-06-23
@matios

Try looking at this question . (unfortunately, in English)
ps in the comments solved the problem, so read the last one.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question