[英]Can't resolve 'fs' in \node_modules\electron
I am using the electron-react boilerplate and want to use an electron dialog
in App.tsx:我正在使用 electron-react 样板文件并想在 App.tsx 中使用 electron
dialog
:
const { dialog } = require('@electron/remote') //also tried with import
const Hello = () => {
const readFromFile = async () => {
dialog.showOpenDialog({})
}
return (
<>
<button onClick={() => readFromFile()} >Test</button>
</>
)
}
in main.ts I placed the following line at the top在 main.ts 中,我将以下行放在顶部
require('@electron/remote/main').initialize()
In the end I always get this error:最后我总是得到这个错误:
Module not found: Error: Can't resolve 'fs' in 'C:\Users\myUsername\source\repos\electronTest\node_modules\electron'
I also tried nodeIntegration: true
and contextIsolation: false
我也试过
nodeIntegration: true
和contextIsolation: false
Just spent a while on this and <rant about JS community> ... eh.刚刚在这上面花了一段时间, <关于 JS 社区的咆哮> ... 嗯。 Maybe this will help others.
也许这会帮助其他人。
You can only use electron's imports in electron-main
side.您只能在
electron-main
端使用电子进口。
Electron is split into electron-main
and electron-renderer
. Electron 分为
electron-main
和electron-renderer
。 As others suggested, you need to update webpack
's config with the target
pointing at the correct electron .正如其他人所建议的那样,您需要更新
webpack
的配置, target
指向正确的electron 。
In case you're using one of the electron builder boilerplates , you might see directory .erb
with a few webpack configs.如果您使用的是electron 构建器样板之一,您可能会看到带有一些 webpack 配置的目录
.erb
。 Especially, there might be one with target = "electron-main"
and another with target = ["web", "electron-renderer"]
.特别是,可能有一个
target = "electron-main"
和另一个target = ["web", "electron-renderer"]
。 So it feels like mission accomplished;所以感觉就像任务完成了; however, according to webpack's doc on target , if you pass a list, then a common config is set.
但是,根据webpack 在target上的文档,如果您传递一个列表,则会设置一个通用配置。 Since
web
doesn't include fs
, the common part also won't include fs
.由于
web
不包括fs
,公共部分也不包括fs
。
For the reason above, some of electron
imports, eg clipboard
, can be only used from the "electron-main" side of your application.由于上述原因,某些
electron
导入,例如clipboard
,只能从应用程序的“主要电子”端使用。
The work-around, eg using clipboard on the application side, is to use IPC to communicate between main
and renderer
sides.解决方法(例如在应用程序端使用剪贴板)是使用IPC在
main
端和renderer
端之间进行通信。
Check your webpack.config.js
.检查您的
webpack.config.js
。 Looks like you target
is not electron-main
or electron-renderer
.看起来您的
target
不是electron-main
或electron-renderer
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.