require(“electron”).app is undefined. I npm instal

2019-04-29 12:48发布

Yesterday, I was developing on Electron perfectly fine. Then I hop onto my computer to realize Electron isn't working at all now.

I removed node_modules and did a fresh npm install

package.json:

...
"devDependencies": {
    "devtron": "^1.4.0",
    "electron": "^1.4.7"
  },
"dependencies": {
    "electron-debug": "^1.1.0"
  }
...

This is the error I got.

I followed the suggestions used of previous issues of this problem. Nothing is resolving it.

Electron is not installed globally. Everything should be self contained in the directory.

npm list

Most of this code was taken from electron boilerplate

Edit:

main process:

'use strict';

const path = require('path');
const electron = require('electron');
const app = electron.app;

// adds debug features like hotkeys for triggering dev tools and reload
require('electron-debug')({
    showDevTools: true
});

// prevent window being garbage collected
let mainWindow;

function onClosed() {
    // dereference the window
    // for multiple windows store them in an array
    mainWindow = null;
}

function createMainWindow() {
    const display = electron.screen.getPrimaryDisplay();

    const win = new electron.BrowserWindow({
        width: display.workArea.width,
        height: display.workArea.height
    });

    const projectPath = path.dirname(path.dirname(__dirname));

    win.loadURL(`file://${projectPath}/static/index.html`);
    win.on('closed', onClosed);

    return win;
}

app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') {
        app.quit();
    }
});

app.on('activate', () => {
    if (!mainWindow) {
        mainWindow = createMainWindow();
    }
});

app.on('ready', () => {
    mainWindow = createMainWindow();
});

1条回答
家丑人穷心不美
2楼-- · 2019-04-29 13:00

So, in my case. Issue was resolved by using my original terminal rather than a plugin terminal for Atom.

For anyone out there. Double check with your vanilla terminal or even editor to double check.

查看更多
登录 后发表回答