简体   繁体   English

无法解析 \node_modules\electron 中的“fs”

[英]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: truecontextIsolation: false

Just spent a while on this and <rant about JS community> ... eh.刚刚在这上面花了一段时间, <关于 JS 社区的咆哮> ... 嗯。 Maybe this will help others.也许这会帮助其他人。

tl;dr tl;博士

You can only use electron's imports in electron-main side.您只能在electron-main端使用电子进口。

longer story更长的故事

Electron is split into electron-main and electron-renderer . Electron 分为electron-mainelectron-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.解决方法(例如在应用程序端使用剪贴板)是使用IPCmain端和renderer端之间进行通信。

Check your webpack.config.js .检查您的webpack.config.js Looks like you target is not electron-main or electron-renderer .看起来您的target不是electron-mainelectron-renderer

暂无
暂无

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

相关问题 Webpack 5 节点接口 - 无法从 node_modules 解析加载程序 - Webpack 5 Node Interface - can't resolve loader from node_modules 无法解析'../node_modules/bootstrap/dist/css/bootstrap.min.css'? - Can't resolve '../node_modules/bootstrap/dist/css/bootstrap.min.css'? 找不到模块:无法解析“C:\react-test\src\components\content”中的“node_modules/react” - Module not found: Can't resolve 'node_modules/react' in 'C:\react-test\src\components\content' 找不到模块:无法解析“/Users/Austin/node_modules/pg/lib”中的“dns” - Module not found: Can't resolve 'dns' in '/Users/Austin/node_modules/pg/lib' 未找到模块:无法解析“E:\frontend\node_modules\@mui\styled-engine”中的“@emotion/react” - Module not found: Can't resolve '@emotion/react' in 'E:\frontend\node_modules\@mui\styled-engine' 找不到模块:无法解析“./node_modules/@material-ui/core/IconButton” - Module not found: Can't resolve './node_modules/@material-ui/core/IconButton' 找不到模块:无法在 reactjs 中解析 'node_modules\react-moment\dist' 中的 'moment' - Module not found: Can't resolve 'moment' in 'node_modules\react-moment\dist' in reactjs electronic-edge-js:无法解析fs和路径 - electron-edge-js: fs and path can't be resolve 节点找不到模块,但它存在于node_modules中? - Node can't find module but it exists in node_modules? 找不到模块:错误:无法解析&#39;X \\ node_modules \\ ng-zorro-antd \\ src \\ ng-zorro-antd.less&#39; - Module not found: Error: Can't resolve 'X\node_modules\ng-zorro-antd\src\ng-zorro-antd.less'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM