Electron (Windows10) 2016-05-23
自分用メモ。
Windows10 64bit
調べながらだと1時間くらいかかった。
参考記事
http://qiita.com/kusohage/items/00a4fb624894f88970dc
http://qiita.com/nyanchu/items/15d514d9b9f87e5c0a29
Node.js インストール
nodejs ダウンロード
自分はCドライブ直下に"Development"フォルダを作って以下にインストール。バージョンはv4.4.4
C:\Development\nodejs\v4.4.4
##環境変数の設定
-
[スタート]を右クリック
-
[システム] > [システムの詳細設定] > [詳細設定]タブから[環境変数]をクリック
-
NODE_HOME変数作成
- ○○のユーザ環境変数で[新規]をクリック
- 変数名「NODE_HOME」/ 変数値「C:\Program Files\nodejs\v4.4.4」設定。
-
PATH変数修正
- PATHを選択して[編集]をクリック
- 変数値に「;%NODE_HOME%」を追記。先程の例だと以下
C:\Development\nodejs\v4.4.4;%NODE_HOME%
##Electronのインストール
先ほどのnode.exeのあるディレクトリで
> npm -g install electron-prebuilt
※自分はこのあたりでPCを再起動した。
##ElectronのHello World
公式のQuick Startを見ながら3つのファイルを用意。全部コピペ。
https://github.com/electron/electron/blob/v1.1.1/docs/tutorial/quick-start.md
フォルダ構造
your-app/
├── package.json
├── main.js
└── index.html
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 .
以上。