ElectronJs:HellowWorld应用程序尚无法启动

时间:2016-05-19 07:18:18

标签: node.js npm chromium electron

我已经开始使用电子js hello world应用程序了 first application with electron

我正在按照指示行事。

创建了3个文件main.js, index.html, package.json

的package.json

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

main.js

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

// 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.
let win;

function createWindow() {
  // Create the browser window.
  win = new BrowserWindow({width: 800, height: 600});

  // and load the index.html of the app.
  win.loadURL(`file://${__dirname}/index.html`);

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

  // Emitted when the window is closed.
  win.on('closed', () => {
    // 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.
    win = null;
  });
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow);

// Quit when all windows are closed.
app.on('window-all-closed', () => {
  // 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();
  }
});

app.on('activate', () => {
  // On OS X it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (win === null) {
    createWindow();
  }
});

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.

的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 Globaly

npm install electron-prebuilt -g

然后尝试使用

运行应用程序
electron .

但没有发生任何事情。结果如下

C:\Users\abc\Desktop\electrons\1>electron .

C:\Users\abc\Desktop\electrons\1>

---编辑

我尝试在package.json中添加脚本来启动电子

{ ... “scripts”:{“start”:“electron main.js”} ... }

然后当我使用npm start运行应用时 结果如下

C:\Users\abc\Desktop\electrons\1>npm start

> your-app@0.1.0 start C:\Users\abc\Desktop\electrons\1
> electron main.js


npm ERR! Windows_NT 6.3.9600
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\abc\\AppData\
\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js" "start"
npm ERR! node v4.4.4
npm ERR! npm  v3.9.0
npm ERR! code ELIFECYCLE
npm ERR! your-app@0.1.0 start: `electron main.js`
npm ERR! Exit status 3221225781
npm ERR!
npm ERR! Failed at the your-app@0.1.0 start script 'electron main.js'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the your-app package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     electron main.js
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs your-app
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls your-app
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     C:\Users\abc\Desktop\electrons\1\npm-debug.log

C:\Users\abc\Desktop\electrons\1>

2 个答案:

答案 0 :(得分:1)

如果您使用的是Electron 1.1.0并且没有安装VS2015,那么您可能会错过VC++2015 runtime

答案 1 :(得分:0)

代码对我有用,所以它必须是你环境中的东西。尝试删除项目的node_modules文件夹(rm -rf node_modules),然后npm i,然后npm start(我假设您的package.json中仍有启动任务)。< / p>