简体   繁体   English

如何在 Electron.Atom\\WebPack 应用程序中使用 FS 模块?

[英]How to use FS module inside Electron.Atom\WebPack application?

I need write some data in the file, using FS module (fs.writeFile).我需要使用 FS 模块 (fs.writeFile) 在文件中写入一些数据。 My stack is webpack + react + redux + electron.我的堆栈是 webpack + react + redux + electron。

The first problem was: Cannot resolve module 'fs' .第一个问题是:无法解析模块 '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"} ).经过多次尝试,问题得到解决( node: {fs: "empty"} )。 But then there was a second problem: screenshot .但是还有第二个问题: 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.您可以看到, fs是空对象,并且方法 writeFile 不存在。 I tried to change the webpack's configuration.我试图更改 webpack 的配置。

const path = require('path');
const fs = require('fs');
const webpack = require("webpack");
console.log(fs);

In this case fs is not empty.在这种情况下fs不为空。

How to solve this problem?如何解决这个问题? Any ideas?有什么想法吗?

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;

In addition to the accepted answer.除了接受的答案。

If you are using Webpack (like when you are using Angular, React or other frameworks) require will be resolved by webpack , which will screw it's usage at runtime.如果你正在使用Webpack (比如当你使用 Angular、React 或其他框架时) require将被webpack解析,这会在运行时webpack它的使用。

Use window.require instead.改用window.require

Ex:例如:

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

Note: There is no need to use remote in order to access any of Node API from a renderer process as it's fully exposed.注意:没有必要使用 remote 来从渲染器进程访问任何 Node API,因为它是完全公开的。

const fs = window.require('fs');
const path = window.require('path');

will do.会做。

Update更新

Starting from v5 of Electron, the Node API is no longer exposed by default in the renderer process!从 Electron v5 开始,Node API 不再默认暴露在渲染进程中!

The default for the nodeIntegration flag changed from true to false . nodeIntegration标志的默认值从 true 更改为false

You can enable it when creating the Browser Window:您可以在创建浏览器窗口时启用它:

app.on('ready', () => {
    mainWindow = new BrowserWindow({
        webPreferences: {
            nodeIntegration: true, // <--- flag
            nodeIntegrationInWorker: true // <---  for web workers
        }
    });
});

The security risk of activating nodeIntegration激活节点集成的安全风险

nodeIntegration: true is a security risk only when you're executing some untrusted remote code in your application. nodeIntegration: true仅当您在应用程序中执行一些不受信任的远程代码时才存在安全风险。 For example, suppose your application opens up a third party webpage.例如,假设您的应用程序打开了第三方网页。 That would be a security risk because the third party webpage will have access to node runtime and can run some malicious code on your user's filesystem.这将是一个安全风险,因为第三方网页将可以访问节点运行时,并且可以在您用户的文件系统上运行一些恶意代码。 In that case it makes sense to set nodeIntegration: false .在这种情况下,设置nodeIntegration: false是有意义的。 If your app is not displaying any remote content, or is displaying only trusted content, then setting nodeIntegration: true is okay.如果您的应用没有显示任何远程内容,或者只显示受信任的内容,那么设置nodeIntegration: true就可以了。

And finally, the recommended secure way from the doc:最后,文档中推荐的安全方式:

https://electronjs.org/docs/tutorial/security#2-do-not-enable-nodejs-integration-for-remote-content https://electronjs.org/docs/tutorial/security#2-do-not-enable-nodejs-integration-for-remote-content

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM