电子快速启动应用程序不工作

electron quick start application not working

我是 electron 的新手,正在尝试学习不同的教程。目前我正在关注 link 到 Write my First Electron App

我的app结构是这样的

your-app/
 ├── package.json
 ├── main.js
 └── index.html

package.json的格式为

 {
  "name"    : "your-app",
  "version" : "0.1.0",
  "main"    : "main.js"
}

这是我的main.js

'use strict';

const electron = require('electron');
const app = electron.app;  // Module to control application life.
const BrowserWindow = electron.BrowserWindow;  // Module to create native browser window.

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
var mainWindow = null;

// Quit when all windows are closed.
app.on('window-all-closed', function() {
  // On OS X it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform != 'darwin') {
    app.quit();
  }
});

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on('ready', function() {
  // Create the browser window.
  mainWindow = new BrowserWindow({width: 800, height: 600});

  // and load the index.html of the app.
  mainWindow.loadURL('file://' + __dirname + '/index.html');

  // Open the DevTools.
  mainWindow.webContents.openDevTools();

  // Emitted when the window is closed.
  mainWindow.on('closed', function() {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    mainWindow = null;
  });
});

index.html是我要显示的网页:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Hello World!</title>
  </head>
  <body>
    <h1>Hello World!</h1>
    We are using node <script>document.write(process.versions.node)</script>,
    Chrome <script>document.write(process.versions.chrome)</script>,
    and Electron <script>document.write(process.versions.electron)</script>.
  </body>
</html>

现在,当我在应用程序的源目录中执行 运行 electron 命令时,它会显示

而不是这个结果

每次我必须将 index.html 拖到第一张图片的空 space 以获得与第二张图片一样的结果。

我不知道我做错了什么。请帮我看看这个简单的应用程序哪里错了。任何形式的帮助将不胜感激。

如果在 运行 'electron' 命令时没有提供任何参数,请将 main.js 重命名为 index.js (电子默认运行)

您必须将 package.json 所在的目录作为参数传递给 electron 命令。 所以如果你从你的应用程序目录中执行电子,你必须写

electron .

我必须 运行 electron index.html 来解决这个问题。