admin管理员组文章数量:1401595
I am using the electron-react boilerplate and want to use an electron dialog
in App.tsx:
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
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
I am using the electron-react boilerplate and want to use an electron dialog
in App.tsx:
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
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
2 Answers
Reset to default 4Just spent a while on this and <rant about JS munity>... eh. Maybe this will help others.
tl;dr
You can only use electron's imports in electron-main
side.
longer story
Electron is split into electron-main
and electron-renderer
. As others suggested, you need to update webpack
's config with the target
pointing at the correct electron.
In case you're using one of the electron builder boilerplates, you might see directory .erb
with a few webpack configs. Especially, there might be one with target = "electron-main"
and another with target = ["web", "electron-renderer"]
. So it feels like mission acplished; however, according to webpack's doc on target, if you pass a list, then a mon config is set. Since web
doesn't include fs
, the mon part also won't include fs
.
For the reason above, some of electron
imports, e.g. clipboard
, can be only used from the "electron-main" side of your application.
The work-around, e.g. using clipboard on the application side, is to use IPC to municate between main
and renderer
sides.
Check your webpack.config.js
. Looks like you target
is not electron-main
or electron-renderer
.
本文标签: javascriptCan39t resolve 39fs39 in nodemoduleselectronStack Overflow
版权声明:本文标题:javascript - Can't resolve 'fs' in node_moduleselectron - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744213718a2595540.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论