
Electron入門|デスクトップアプリケーションを作成しましょう
- SE
- Electoronはどのようなプログラミング言語なのですか。
- PM
- HTMLとCSS、JavaScriptなどのウェブ技術を使ってデスクトップアプリケーションを作成できるプログラミング言語です。
目次
Electron入門
今回は、Electron入門編です。Electronとは、HTMLとCSS、JavaScriptなどのウェブ技術でデスクトップアプリケーションを作成できる仕組みです。
Electronの導入方法から、デスクトップアプリケーションを作成・パッケージ化について紹介します。Electron入門編に興味のある方はぜひご覧ください。
package.jsonファイル作成
Electronの開発には、Node.jsが必要です。ここでは、Node.jsはインストールしている前提で進めます。
まず、入門編として、package.jsonファイルを作成します。アプリケーションを格納するディレクトリを作成し、コマンドプロンプトでそのディレクトリに移動してください。
以下のように、npm initコマンドを実行し、デフォルトのままEnterキーを押します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
C:\prog\electron\sample>npm init This utility will walk you through creating a package.json file. It only covers the most common items, and tries to guess sensible defaults. See `npm help init` for definitive documentation on these fields and exactly what they do. Use `npm install <pkg>` afterwards to install a package and save it as a dependency in the package.json file. Press ^C at any time to quit. package name: (sample) version: (1.0.0) description: entry point: (index.js) test command: git repository: keywords: author: license: (ISC) About to write to C:\prog\electron\sample\package.json: { ""name"": ""sample"", ""version"": ""1.0.0"", ""description"": """", ""main"": ""index.js"", ""scripts"": { ""test"": ""echo \""Error: no test specified\"" && exit 1"" }, ""author"": """", ""license"": ""ISC"" } Is this OK? (yes) yes C:\prog\electron\sample> |
カレントディレクトリにpackage.jsonが自動生成されます。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
{ ""name"": ""sample"", ""version"": ""1.0.0"", ""description"": """", ""main"": ""index.js"", ""scripts"": { ""test"": ""echo \""Error: no test specified\"" && exit 1"" }, ""author"": """", ""license"": ""ISC"" } |
Electronのインストール
入門編として、Electronをインストールします。以下のように、npm installコマンドを実行してください。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
C:\prog\electron\sample>npm install --save-dev electron > core-js@3.6.5 postinstall C:\prog\electron\sample\node_modules\core-js > node -e ""try{require('./postinstall')}catch(e){}"" Thank you for using core-js ( https://github.com/zloirock/core-js ) for polyfilling JavaScript standard library! The project needs your help! Please consider supporting of core-js on Open Collective or Patreon: > https://opencollective.com/core-js > https://www.patreon.com/zloirock Also, the author of core-js ( https://github.com/zloirock ) is looking for a good job -) > electron@9.1.0 postinstall C:\prog\electron\sample\node_modules\electron > node install.js Downloading electron-v9.1.0-win32-x64.zip: [===================================================] 100% ETA: 0.0 seconds npm notice created a lockfile as package-lock.json. You should commit this file. npm WARN sample@1.0.0 No description npm WARN sample@1.0.0 No repository field. + electron@9.1.0 added 87 packages from 98 contributors and audited 87 packages in 78.897s 5 packages are looking for funding run `npm fund` for details found 0 vulnerabilities C:\prog\electron\sample> |
次に、以下のコマンドを実行してみます。
1 |
C:\prog\electron\sample>.\node_modules\.bin\electron |
Electronの画面が開くと、インストールに成功しています。
×ボタンで閉じておいてください。
アプリのエントリーポイント作成
入門編として、アプリケーションのエントリポイントを作成します。
package.json と同じディレクトリに index.js を作成します。index.js は package.json 内に記載しているjsonファイルと同じ名前になるようにしてください。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
// index.js const {app, BrowserWindow} = require('electron'); let win; function createWindow () { // BrowserWindowを作成 win = new BrowserWindow({ webPreferences: { nodeIntegration: true, }, width: 800, height: 600, }); // index.htmlをロード win.loadFile('index.html'); // ウィンドウが閉じられた場合 win.on('closed', () => { win = null }); } // 初期化が完了した場合 app.on('ready', createWindow); // ウィンドウが閉じられた場合 app.on('window-all-closed', () => { if (process.platform !== 'darwin') { app.quit(); } }); // アプリケーションがアクティブになった場合 app.on('activate', () => { if (win === null) { createWindow(); } }); |
HTMLファイル作成
入門編として、index.html ファイルを作成します。index.json 内に記述した html と同じ名前になるようにしてください。
ここでは、Hello World!を表示するだけのhtmlにします。
1 2 3 4 5 6 7 8 9 10 11 12 |
<html> <head> <meta charset=""UTF-8""> <title>Hello World!</title> </head> <body> <h1>Hello World!</h1> </body> </html> |
アプリケーションの起動
入門編として、Electronで作成したアプリケーションを起動してみます。
electronコマンドの引数にindex.jsを指定してください。
1 |
C:\prog\electron\sample>.\node_modules\.bin\electron index.js |
画面が開き、Hello World!が表示されることが分かります。
パッケージ化
入門編として、作成したアプリケーションを、配布のためにパッケージ化します。
まず、electron-packagerをインストールします。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
C:\prog\electron\sample>npm install -save-dev electron-packager npm WARN saveError EPERM: operation not permitted, rename 'C:\prog\electron\sample\package.json.1684761739' -> 'C:\prog\electron\sample\package.json' npm WARN sample@1.0.0 No description npm WARN sample@1.0.0 No repository field. + electron-packager@15.0.0 added 86 packages from 58 contributors and audited 173 packages in 26.339s 8 packages are looking for funding run `npm fund` for details found 0 vulnerabilities C:\prog\electron\sample> |
electron-packagerのインストールが完了したら、パッケージ化します。
以下のコマンドでプラットフォームやアーキテクチャを指定します。
1 2 3 4 5 |
C:\prog\electron\sample>.\node_modules\.bin\electron-packager . electron-hello-world --platform=win32 --arch=x64 --overwrite Packaging app for platform win32 x64 using electron v9.1.0 Wrote new app to C:\prog\electron\sample\electron-hello-world-win32-x64 C:\prog\electron\sample> |
“”electron-hello-world-win32-x64″”フォルダに””electron-hello-world.exe””が作成されます。
exeファイルをダブルクリックすると、アプリケーションが起動できます。
- SE
- Electronはデスクトップアプリケーションを作成することができるのですね。
- PM
- 今回紹介したのは入門レベルでしたが、ご自身でソースコードを書いて理解を深めてください。
まとめ
Electron入門編、いかがでしたでしょうか。Electronとは、HTMLとCSS、JavaScriptなどのウェブ技術でデスクトップアプリケーションを作成できる仕組みです。
Electronの導入方法から、デスクトップアプリケーションを作成・パッケージ化について紹介しました。ここで紹介したのは入門レベルですので、ぜひご自身でソースコードを書いて、理解を深めてください。