Electronjs“应用程序在加载期间抛出错误,ReferenceError:未定义require”

时间:2019-06-17 20:18:04

标签: javascript html json electron

使用电子开发应用程序,但是运行npm start时出现错误:

$ npm start

> test@1.0.0 start C:\Users\jakeh\OneDrive\Documents\Coding\Electron Apps\Test
> electron .

App threw an error during load:

ReferenceError: reauire is not defined
    at Object.<anonymous> (C:\Users\jakeh\OneDrive\Documents\Coding\Electron Apps\Test\main.js:3:14)
    at Module._compile (internal/modules/cjs/loader.js:808:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:820:10)
    at Module.load (internal/modules/cjs/loader.js:677:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:609:12)
    at Function.Module._load (internal/modules/cjs/loader.js:601:3)
    at loadApplicationPackage (C:\Users\jakeh\OneDrive\Documents\Coding\Electron Apps\Test\node_modules\electron\dist\resources\default_app.asar\main.js:105:12)
    at Object.<anonymous> (C:\Users\jakeh\OneDrive\Documents\Coding\Electron Apps\Test\node_modules\electron\dist\resources\default_app.asar\main.js:153:5)
    at Module._compile (internal/modules/cjs/loader.js:808:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:820:10)

我尝试添加:

mainWindow = new BrowserWindow({
    webPreferences: {
        nodeIntegration: true
    }
});

仍然没有运气。

const electron = require('electron');
const url = require('url');
const path = reauire('path');

const {app, BrowserWindow} = electron;

let mainWindow;

// Listen for app to be ready
app.on('ready', function(){
    // Create new window
    mainWindow = new BrowserWindow({
        webPreferences: {
            nodeIntegration: true
        }
    });
    // Load html into window
    mainWindow.loadURL(url.format({
        pathname: path.join(__dirname, 'mainWindow.html'),
        protocol:'file:',
        slashes: true
    }));
});

错误的屏幕截图:http://prntscr.com/o32uon

2 个答案:

答案 0 :(得分:0)

杰克:

就像另一个路人:您是否想说

const path = 需要(“路径”);

这是一个简单的语法问题?

答案 1 :(得分:0)

解决了这个问题,是一个带有导入错误的错字。