问题
I have started with electron js hello world application first application with electron
I am doing exactly what is instructed.
Created 3 files 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>
then installed Electron Globaly
npm install electron-prebuilt -g
then trying to run the application using
electron .
but nothing is happening. Here is the result
C:\Users\abc\Desktop\electrons\1>electron .
C:\Users\abc\Desktop\electrons\1>
--- EDIT
I have tried added script in package.json to start the electron
{ ... "scripts":{"start":"electron main.js"} ... }
and then when I ran the app using npm start
Here is the result
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>
回答1:
If you're using Electron 1.1.0 and don't have VS2015 installed then you might be missing the VC++2015 runtime.
回答2:
The code works for me, so it must be something in your environment. Try deleting the node_modules folder of the project (rm -rf node_modules
), then npm i
and then npm start
(I'm assuming you still have the start task in your package.json).
来源:https://stackoverflow.com/questions/37316414/electronjs-hellowworld-application-is-not-starting