类型错误:无法读取未定义的属性“要求”(电子)

时间:2021-06-25 15:35:43

标签: javascript node.js electron

我需要帮助,我正在做和学习 Electron,当我想在我的 app.js 中使用“远程”时,出现以下错误:

TypeError: Cannot read property 'require' of undefined

这是我的 app.js

    const { ipcRenderer, remote } = require('electron'); 

    const main = remote.require('../main'); //The error is generated here

main.js 的位置是正确的。

这是我的 main.js

const{BrowserWindow} = require('electron')

function hello(){
    console.log('Desde Main')
}

let window

function createWindow() {
    window = new BrowserWindow({
        width: 799,
        height: 599,
        webPreferences: {
            nodeIntegration: true,
            contextIsolation: false,
        }
        
    })

    window.loadFile('src/ui/index.html')
    //window.removeMenu()//
}

module.exports = {
    createWindow,
    hello
}

如果您需要更多信息,请随时咨询我,非常感谢。

更新

我执行了响应 (Electron.remote is undefined) 中显示的指示,但我抛出了以下错误...

(electron) The remote module is deprecated. Use 
https://github.com/electron/remote instead.
log @ electron/js2c/renderer_init.js:13

然后下一个

Uncaught Error: Cannot find module '../main.js'
Require stack:
- C:\Users\claur\Documents\ThomasLaurence\workspace\Metales_del_Sur_ODT\src\index.js
- C:\Users\claur\Documents\ThomasLaurence\workspace\Metales_del_Sur_ODT\node_modules\electron\dist\resources\default_app.asar\main.js
- 
    at Module._resolveFilename (internal/modules/cjs/loader.js:887)
    at Function.n._resolveFilename (electron/js2c/browser_init.js:257)
    at Module._load (internal/modules/cjs/loader.js:732)
    at Function.f._load (electron/js2c/asar_bundle.js:5)
    at Module.require (internal/modules/cjs/loader.js:959)
    at electron/js2c/browser_init.js:221
    at IpcMainImpl.<anonymous> (electron/js2c/browser_init.js:221)
    at IpcMainImpl.emit (events.js:315)
    at Object.<anonymous> (electron/js2c/browser_init.js:161)
    at Object.emit (events.js:315)

我可以从应用程序窗口中的控制台可视化的所有内容

1 个答案:

答案 0 :(得分:0)

来自官方文档,

<块引用>

⚠️ 警告 ⚠️ 不推荐使用远程模块。而不是远程,使用 ipcRenderer 和 ipcMain。

如果您使用 app.js 在主进程和渲染器进程之间传递数据,您可以像这样将其声明为预加载:

 window = new BrowserWindow({
    width: 799,
    height: 599,
    webPreferences: {
      contextIsolation: true, 
      nodeIntegration: false, 
      preload: path.join(__dirname, 'app.js')
    }

这样,app.js 也可以访问节点模块。 Here 是一个要点,可帮助您了解如何在渲染器和主进程之间传递数据。