如何在 Electron.AtomWebPack 应用程序中使用 FS 模块?

2022-01-10 00:00:00 reactjs node.js electron javascript npm

我需要在文件中写入一些数据,使用 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方法.我试图改变 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;

相关文章