LoginSignup
7
6

More than 3 years have passed since last update.

WebStormでElectronを始める手順

Last updated at Posted at 2019-07-09

はじめに(2020/4/8 追記)

展示会やデジタルサイネージなど、Webとは異なり、特定のPCでの動作を想定したアプリケーションの作成は、Adobe Air や Unityなどで作成する事がありますが、最近はJavaScriptだけで色々出来るようになってきたので、Electronを使ってアプリケーションを作成したいと思いました。IDEは、WebStormを使っているので、WebStormでElectronを開発するための手順を書いています。

※追記(2020/4/8)

最近この記事を元にElectronを作ってみたら、デバッグが起動しなくなってしまったので、
新しく「WebStormでElectronの始め方とアプリの作り方」という記事を書きました。動かない場合はこちらを参照してみてください。

目次

  1. 環境
  2. WebStormでの準備
  3. index.htmlとmain.jsファイルを作る
  4. デバッグの設定
  5. 参考サイト

1. 環境

  • Windows10 Pro
  • Node.js : v10.15.3
  • WebStorm : 2019.1.3

2. WebStormでの準備

  1. Node.jsは既にインストールされている状態で、WebStormを起動します。
    起動したら、create project を選択します。
    ws01.PNG

  2. New Project画面で、Node.js を選択し、Locationに任意のプロジェクトを作ります。(ここではelectron_webstorm としています)
    ws02.PNG

  3. create を選択すると、初期画面が表示されます。
    ws03.PNG

  4. File > Settings... を選択し、設定画面を開きます。
    ws04.PNG

  5. Language & Frameworks > Node.js and NPM を選択します。
    ws05.PNG

  6. + を選択して、Available Packages画面の検索枠に electron と入力し、リスト内のelectronを選択します。
    ws06.PNG

  7. INSTALL PACKAGE を選択して、インストールします。
    Package 'electron' installed successfully と出ればインストールできていますので、画面を閉じます。

  8. package.jsondependencies が追加され、electron という項目が増えています。同様に、node_modulesに、electron が追加されていることが確認できます。
    ws080.PNG

3. index.htmlとmain.jsファイルを作る

  1. WebStormでここまで作成した場合、package.json には、mainファイルindex.js と指定されていますが、Electronのサイトでは main.js がデフォルトみたいなので、ここの記載を変更します。
    ws08.PNG

  2. index.htmlmain.js ファイルを作る
    ws09.PNG

  3. Electronのサイトから、ベースとなるindex.htmlの中身とmain.jsの中身をコピペします。

    コードは Writing Your First Electron App から引用

main.js
const { app, BrowserWindow } = require('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,
    webPreferences: {
      nodeIntegration: true
    }
  })

  // and load the index.html of the app.
  win.loadFile('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 macOS 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 macOS 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>

4. デバッグの設定

  1. 画面右上の ADD CONFIGURATION... を選択し、Run/Debug Configurations 画面を開く
  2. + を押して、Node.jsのテンプレートを選び新規デバッグを作成します。
    ws10.PNG

  3. Node interpreter の入力欄の右にある...を選択し、開いた画面の + を選択し、Add Local... を選択して、最初に追加したElectronの electron.cmd を、node_moduleより追加します。
    ws11.PNG

  4. Node parameters. を入力

  5. Working directory は変更なし

  6. JavaScript filemain.jsを入力

  7. Application parameters--remote-debugging-port=9222を入力

  8. Environment variables は変更なし

  9. OK を選択して画面を閉じます、
    ws12.PNG

  10. 画面右上の歯車アイコンを押すとデバッグウィンドウが立ち上がり、Hello World の画面が出れば成功です。お疲れ様でした。
    ws13.PNG

5. 参考サイト

7
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6