質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Electron

Electronは、HTML5とNode.jsというWebの技術を用いてデスクトップアプリケーションを作成できるクロスプラットフォームな実行環境です。

npm

npmは、Node Packaged Modulesの略。Node.jsのライブラリ・パッケージを管理できるツールです。様々なモジュールを簡単にインストールでき、自分でモジュールを作成し公開する際にも使用できます。

TypeScript

TypeScriptは、マイクロソフトによって開発された フリーでオープンソースのプログラミング言語です。 TypeScriptは、JavaScriptの構文の拡張であるので、既存の JavaScriptのコードにわずかな修正を加えれば動作します。

React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

Q&A

解決済

1回答

3241閲覧

electron-builderでビルドが通ったがエラーも何も表示されない

skytomo

総合スコア35

Electron

Electronは、HTML5とNode.jsというWebの技術を用いてデスクトップアプリケーションを作成できるクロスプラットフォームな実行環境です。

npm

npmは、Node Packaged Modulesの略。Node.jsのライブラリ・パッケージを管理できるツールです。様々なモジュールを簡単にインストールでき、自分でモジュールを作成し公開する際にも使用できます。

TypeScript

TypeScriptは、マイクロソフトによって開発された フリーでオープンソースのプログラミング言語です。 TypeScriptは、JavaScriptの構文の拡張であるので、既存の JavaScriptのコードにわずかな修正を加えれば動作します。

React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

0グッド

0クリップ

投稿2021/03/29 11:52

編集2021/03/29 13:10

前提・実現したいこと

electron-builderでビルドが通ったのですが、何も表示されません。
レンダリングができるようにしたいです。

発生している問題・エラーメッセージ

exeファイルをクリックすると、
白い画面が出てきますが、
それ以外、何も表示されません。

また、Ctrl+Shift+Iを押して、コンソールでどのようなエラーを吐いているか確認しましたが、
エラーが全く出ていません。

一応ビルド結果を示します。

PowerShell

1(base) PS D:\skytomo\Documents\Programming\otamashelf> npm run build:main 2 3> otamashelf@0.1.0 build:main 4 5(base) PS D:\skytomo\Documents\Programming\otamashelf> npm run build:render 6 7> otamashelf@0.1.0 build:render 8> webpack 9asset bundle.js 5.96 MiB [emitted] (name: main) 1 related asset 10asset index.html 324 bytes [compared for emit] 11613 modules 12webpack 5.28.0 compiled successfully in 24624 ms 13(base) PS D:\skytomo\Documents\Programming\otamashelf> npm run start 14 15> otamashelf@0.1.0 start 16> electron ./dist/main.js 17 18 19(base) PS D:\skytomo\Documents\Programming\otamashelf> npm run package:windows 20 21> otamashelf@0.1.0 package:windows 22> webpack && electron-builder --win --x64 23 24asset bundle.js 5.96 MiB [compared for emit] (name: main) 1 related asset 25asset index.html 324 bytes [compared for emit] 26webpack 5.28.0 compiled successfully in 23893 ms 27 • electron-builder version=22.10.5 os=10.0.19042 28 • loaded configuration file=package.json ("build" field) 29 • writing effective config file=product\builder-effective-config.yaml 30"electron-squirrel-startup" dependency is not required for NSIS 31 • packaging platform=win32 arch=x64 electron=12.0.2 appOutDir=product\win-unpacked 32 • Unpacking electron zip zipPath=undefined 33 • default Electron icon is used reason=application icon is not set 34 • building target=nsis file=product\Otamajakushi Bookshelf Setup 0.1.0.exe archs=x64 oneClick=true perMachine=false 35 • building block map blockMapFile=product\Otamajakushi Bookshelf Setup 0.1.0.exe.blockmap

該当のソースコード

Package.json

json

1{ 2 "name": "otamashelf", 3 "version": "0.1.0", 4 "description": "OTM-JSONファイルで書かれた辞書が開けるアプリ", 5 "main": "main.js", 6 "scripts": { 7 "test": "echo \"Error: no test specified\" && exit 1", 8 "eslint": "eslint . --fix", 9 "start": "electron ./dist/main.js", 10 "build:main": "tsc", 11 "build:render": "webpack", 12 "package:mac": "webpack && electron-builder --mac --x64", 13 "package:windows": "webpack && electron-builder --win --x64", 14 "package:linux": "webpack && electron-builder --linux --x64", 15 "package": "electron-forge package", 16 "make": "electron-forge make" 17 }, 18 "build": { 19 "productName": "Otamajakushi Bookshelf", 20 "appId": "todo.example.com", 21 "directories": { 22 "output": "./product" 23 }, 24 "files": [ 25 "./dist/**/*.js", 26 "./index.html", 27 "./main.js", 28 "./icon/**/*" 29 ], 30 "extraMetadata": { 31 "main": "./dist/main.js" 32 }, 33 "dmg": { 34 "contents": [ 35 { 36 "x": 410, 37 "y": 150, 38 "type": "link", 39 "path": "/Applications" 40 }, 41 { 42 "x": 130, 43 "y": 150, 44 "type": "file" 45 } 46 ] 47 } 48 }, 49 "author": "skytomo", 50 "license": "MIT", 51 "dependencies": { 52 "@material-ui/core": "^4.11.3", 53 "@material-ui/icons": "^4.11.2", 54 "@material-ui/lab": "^4.0.0-alpha.57", 55 "@mdi/font": "^5.9.55", 56 "@mdi/js": "^5.9.55", 57 "@mdi/react": "^1.4.0", 58 "electron-squirrel-startup": "^1.0.0", 59 "otamajakushi": "^1.1.0", 60 "path": "^0.12.7", 61 "react": "^17.0.1", 62 "react-dom": "^17.0.1", 63 "react-redux": "^7.2.2", 64 "redux": "^4.0.5", 65 "typescript-fsa": "^3.0.0", 66 "typescript-fsa-reducers": "^1.2.2" 67 }, 68 "devDependencies": { 69 "@electron-forge/cli": "^6.0.0-beta.54", 70 "@electron-forge/maker-deb": "^6.0.0-beta.54", 71 "@electron-forge/maker-rpm": "^6.0.0-beta.54", 72 "@electron-forge/maker-squirrel": "^6.0.0-beta.54", 73 "@electron-forge/maker-zip": "^6.0.0-beta.54", 74 "@electron-forge/plugin-webpack": "^6.0.0-beta.54", 75 "@types/material-ui": "^0.21.8", 76 "@types/react": "^17.0.2", 77 "@types/react-dom": "^17.0.1", 78 "@types/react-redux": "^7.1.16", 79 "@typescript-eslint/eslint-plugin": "^4.16.1", 80 "@typescript-eslint/parser": "^4.16.1", 81 "electron": "^12.0.1", 82 "electron-builder": "^22.10.5", 83 "eslint": "^7.21.0", 84 "eslint-config-airbnb": "^18.2.1", 85 "eslint-config-prettier": "^8.1.0", 86 "eslint-import-resolver-webpack": "^0.13.0", 87 "eslint-loader": "^4.0.2", 88 "eslint-plugin-import": "^2.22.1", 89 "eslint-plugin-jsx-a11y": "^6.4.1", 90 "eslint-plugin-node": "^11.1.0", 91 "eslint-plugin-promise": "^4.3.1", 92 "eslint-plugin-react": "^7.22.0", 93 "eslint-plugin-react-hooks": "^4.2.0", 94 "html-webpack-plugin": "^5.2.0", 95 "prettier": "^2.2.1", 96 "ts-loader": "^8.0.17", 97 "typescript": "^4.1.5", 98 "webpack": "^5.28.0", 99 "webpack-cli": "^4.5.0" 100 }, 101 ...(省略)... 102}

index.html

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Otamajakushi Bookshelf</title> </head> <body style="margin: 0px;"> <div id="contents"></div> </body> </html>

main.ts

ts

1/* eslint-disable global-require */ 2/* eslint-disable @typescript-eslint/no-var-requires */ 3import { app, BrowserWindow, dialog, ipcMain } from 'electron'; 4import { readFileSync } from 'fs'; 5 6// セキュアな Electron の構成 7// 参考: https://qiita.com/pochman/items/64b34e9827866664d436 8 9const createWindow = () => { 10 const win = new BrowserWindow({ 11 width: 500, 12 height: 700, 13 webPreferences: { 14 nodeIntegration: true, 15 nodeIntegrationInWorker: true, 16 contextIsolation: false, 17 // preload: `${__dirname}/preload.js` 18 }, 19 frame: false, 20 resizable: true, 21 }); 22 23 // 読み込む index.html。 24 // tsc でコンパイルするので、出力先の dist の相対パスで指定する。 25 const path = require('path'); 26 win.loadFile('index.html'); 27 28 if (process.argv.find(arg => arg === '--debug')) { 29 win.webContents.openDevTools(); 30 } 31 32 ipcMain.handle('window-minimize', () => { 33 win.minimize(); 34 }); 35 36 let fullScreen = false; 37 38 ipcMain.handle('window-maximize', () => { 39 win.setFullScreen((fullScreen = !fullScreen)); 40 }); 41 42 ipcMain.handle('window-close', () => { 43 app.quit(); 44 }); 45 46 ipcMain.handle('file-open', async event => { 47 // ファイルを選択 48 const paths = dialog.showOpenDialogSync(win, { 49 buttonLabel: '開く', // 確認ボタンのラベル 50 filters: [{ name: 'OTM-JSON', extensions: ['json'] }], 51 properties: [ 52 'openFile', // ファイルの選択を許可 53 'createDirectory', // ディレクトリの作成を許可 (macOS) 54 ], 55 }); 56 57 // キャンセルで閉じた場合 58 if (paths === undefined) { 59 return { status: undefined }; 60 } 61 62 // ファイルの内容を返却 63 try { 64 const filePath = paths[0]; 65 const buff = readFileSync(filePath); 66 67 return { 68 status: true, 69 path: filePath, 70 text: buff.toString(), 71 }; 72 } catch (error) { 73 return { status: false, message: error.message }; 74 } 75 }); 76}; 77 78// Electronの起動準備が終わったら、ウィンドウを作成する。 79app.whenReady().then(createWindow); 80 81// すべての ウィンドウ が閉じたときの処理 82app.on('window-all-closed', () => { 83 // macOS 以外では、メインプロセスを停止する 84 // macOS では、ウインドウが閉じてもメインプロセスは停止せず 85 // ドックから再度ウインドウが表示されるようにする。 86 if (process.platform !== 'darwin') { 87 app.quit(); 88 } 89}); 90 91app.on('activate', () => { 92 // macOS では、ウインドウが閉じてもメインプロセスは停止せず 93 // ドックから再度ウインドウが表示されるようにする。 94 if (BrowserWindow.getAllWindows().length === 0) { 95 createWindow(); 96 } 97});

その他のソースコードは
https://github.com/skytomo221/otamashelf
に公開しています。

試したこと

最初は

Not allowed to load local resource: file:///.../product/win-unpacked/resources/app.asar/index.html

というようなエラーが表示されており、

などを参考に、色々とソースコードを変更した結果、ビルドは通るようになったのですが、
何も表示されません。

app.asarの中身を見るとこのようになってました。

dist ├bundle.js └main.js node_modules index.html package.json

このindex.htmlは次のようになっていました。

html

1<!DOCTYPE html> 2<html lang="en"> 3 4<head> 5 <meta charset="UTF-8" /> 6 <meta name="viewport" content="width=device-width, initial-scale=1.0" /> 7 <title>Otamajakushi Bookshelf</title> 8</head> 9 10<body style="margin: 0px;"> 11 <div id="contents"></div> 12</body> 13 14</html>

一方で、dist/index.htmlでは

html

1<!DOCTYPE html> 2<html lang="en"> 3 4<head> 5 <meta charset="UTF-8" /> 6 <meta name="viewport" content="width=device-width, initial-scale=1.0" /> 7 <title>Otamajakushi Bookshelf</title> 8<script defer src="bundle.js"></script></head> 9 10<body style="margin: 0px;"> 11 <div id="contents"></div> 12</body> 13 14</html>

でした。
おそらく、
この<script defer src="bundle.js">が含まれていないことが問題であるような気がしますが、
解決方法が思いつきません。

おそらく何か設定を間違えているのでしょうが、具体的にどこを修正すればいいか分かりませんでした。

補足情報(FW/ツールのバージョンなど)

  • OS: Windows 10 Home
  • npm: 7.6.3
  • tsc: Version 4.2.3

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

本当に設定ミスでした。

main.ts

typescript

1 win.loadFile('index.html');

typescript

1 win.loadFile(path.join(__dirname, './index.html'));

に変更。

package.json
{ ... "build": { ... "files": [ "./dist/**/*.js", "./dist/index.html", /* ここ */ "./main.js", "./icon/**/*" ],

に変更したら
うまくいきました。

投稿2021/03/29 12:29

skytomo

総合スコア35

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問