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

How to use FS module inside Electron.AtomWebPack application?(如何在 Electron.AtomWebPack 应用程序中使用 FS 模块?)
本文介绍了如何在 Electron.AtomWebPack 应用程序中使用 FS 模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我需要在文件中写入一些数据,使用 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;

这篇关于如何在 Electron.AtomWebPack 应用程序中使用 FS 模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
Ordinals in words javascript(javascript中的序数)