如何在Electron.Atom\WebPack应用程序中使用FS模块? [英] How to use FS module inside Electron.Atom\WebPack application?
问题描述
我需要使用FS模块(fs.writeFile)在文件中写入一些数据。我的堆栈是webpack + react + redux + electron。
I need write some data in the file, using FS module (fs.writeFile). My stack is webpack + react + redux + electron.
第一个问题是:无法解析模块'fs'。
我试图使用
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']
},
经过多次尝试后,问题得以解决( node:{fs:empty} )。但是还有第二个问题:截图。
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);
你可以看到, fs 是空对象,方法是writeFile no存在。我试图改变webpack的配置。
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);
在这种情况下, fs 不为空。
In this case fs is not empty.
如何解决这个问题?有什么想法吗?
How to solve this problem? Any ideas?
推荐答案
问题解决了。
需要用于电子应用程序(你添加包):
Need use in electron app (where you add the bundle):
var remote = require('electron').remote;
var electronFs = remote.require('fs');
var electronDialog = remote.dialog;
这篇关于如何在Electron.Atom\WebPack应用程序中使用FS模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!