LoginSignup
48
40

More than 5 years have passed since last update.

ElectronのクイックスタートでCannot find module 'app'にハマった件

Last updated at Posted at 2016-06-02

Error: Cannot find module 'app'でハマる

下記を参考にElectronをインストールして実行しようとしていたところ、
electronのインストールでコケた時の解決方法 on OS X
30分で出来る、JavaScript (Electron) でデスクトップアプリを作って配布するまで

ずっとこのエラーでハマってしまった...

App threw an error during load
Error: Cannot find module 'app'
    at Module._resolveFilename (module.js:438:15)
    at Function.Module._resolveFilename (/usr/local/lib/node_modules/electron-prebuilt/dist/Electron.app/Contents/Resources/electron.asar/common/reset-search-paths.js:47:12)
    at Function.Module._load (module.js:386:25)
    at Module.require (module.js:466:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/Applications/MAMP/htdocs/app/index.js:1:90)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:456:32)
    at tryModuleLoad (module.js:415:12)

npm-install-missingなども試すもダメ...

解決

変数宣言部分を変更。

"use strict";

const electron = require("electron");
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
let mainWindow;

// 全てのウィンドウが閉じたら終了
app.on('window-all-closed', function() {
  if (process.platform != 'darwin') {
    app.quit();
  }
});

// Electronの初期化完了後に実行
app.on('ready', function() {
  // メイン画面の表示。ウィンドウの幅、高さを指定できる
  mainWindow = new BrowserWindow({width: 800, height: 600});
  mainWindow.loadURL('file://' + __dirname + '/index.html');

  // ウィンドウが閉じられたらアプリも終了
  mainWindow.on('closed', function() {
    mainWindow = null;
  });
});

Electronのバージョンが変わって
宣言の仕方が変わったって感じですかね。

JavaScript自体も初心者レベルでElectronも初めて触ります。
「その宣言おかしいよ」
「それって当たり前じゃね?」
「◯◯で解決した方がいいよ」
等あればご指摘お願いいたします。

参考

electronのインストールでコケた時の解決方法 on OS X
30分で出来る、JavaScript (Electron) でデスクトップアプリを作って配布するまで
ドットインストールでElectron講座を期間限定で無償で公開していたので視聴して実際試した

48
40
3

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
48
40