Windows & Electron & SQLite3 ビルドエラーの解消

Electron を使用すると HTML & CSS & JS でクロスプラットフォームなデスクトップアプリを開発できます。

建前上は Linux と Windows と macOS とで同じ仕様のソフトウェアを動かすことが可能ということになっていますが、実際には node_modules 以下のモジュールのコンパイルエラーやファイルパス、Node.js のバージョンなどの影響を受けるため、ほかの OS 上で開発されたプロジェクトをそのままクローンして実行しようとするとうまくいかないことが多々あります。

とくに Windows 10 上での実行では、ほかの OS では見たこともないような得体の知れないエラーが頻出します。これは Node.js が部分的に C++ で構成されていたり、一部のモジュールのインストール時に Python のような JS 以外の言語で書かれたスクリプトが実行されたりするためです。

Windows はこうした開発者ツールを備えていませんので、必要な場合はユーザが自身でインストールして設定を行わなければなりません。

私も Windows の使い方が分からないのですが、エラーメッセージをだいぶ見慣れて耐性がついてきたので備忘録代わりに記述しておきます。

動作環境 Windows 10 (ver 1909)

Continue reading “Windows & Electron & SQLite3 ビルドエラーの解消”

Possible causes for errors in packaging an Electron App

While trying to build and package your Electron application, you may occasionally encounter file load errors. Your application works perfectly in development but the distribution returns nothing more than a blank white page.

You may get error messages such as Unable to load preload script: … resources/app.asar/preload.js, Not allowed to load local resource: file://src/index.html or even Application entry file “dist/main.js” in the linux-unpacked/resources/app.asar” does not exist. Seems like a wrong configuration.

All of the above-mentioned error messages stem from file path problems. Your packaged Electron application requires absolute paths to the HTML file, modules and extra resources. And the paths in the distribution package are not always the same as those in the development.

There are a NodeJS variable and Electron functions to avoid this problem. In whichever file (or on whichever operating system) you invoke your NodeJS process, __dirname always returns the absolute path of where you start it. Electron app API allows you to get and set paths to your application resources.

Instead of using the relative path that can cause an error in production, you can set up the paths using __dirname in the main process.

If your application won’t load the HTML file using __dirname, your problem presumably won’t be related to Electron.

When bundled by Webpack, the variable __dirname doesn’t automatically show the path to build resources but to your project root, or a single forward slash ‘/’. This may be the cause of your trouble. In this case, you just need to open your webpack config and set false to node: {__dirname: }.

Then __dirname will return the path to the current application directory. Neither setting “asar”: false nor specifying “devEngines” is required. app.getPath(name) works in the same manner. This function is really useful for saving application settings and user data. Quite apart from userData, where your app’s configuration files are stored, it returns the absolute paths to User’s home, Desktop, Downloads and Documents as well.

All the Electron Docs! | Electron

For your reference, here is my version information.

React コンポーネントに history がないときに確認すること

ReactJS を使ってシングルページアプリケーション(SPA)を作成する際、コンポーネント内に props.history が見当たらないことがあります。

例えば CRUD (DBMS) 操作を行った直後にリダイレクトを行う際などに、このエラーが生じるとページ遷移も DOM の書き換えも行われません。

そこでエラー表示を少し調べてみると this.props.history が見当たらないことはありえないとか、withRouter を使ってリダイレクトする方法があるなど、いろいろな(自然)言語で好き勝手なことが書かれていることが分かります。

しかし、このエラー表示が生じた環境において TypeScript をお使いの場合、コンポーネント、つまり props や state の各変数の型定義が行われていないことを最初に疑った方が良いかも知れません。

つまり interface にてコンポーネント内の変数の型を自身で定義して React.Component <{},{}> に渡すと問題が解決する場合があります。

以下に具体例を提示します。

少し考えてみると自明なことなのですが、ドキュメントを読み返してみても原因らしきものが思い当たらなくて困惑しました。最終的に解決に役立ったのはこちらです。


TypeScript and React – Hello React and TypeScript
https://charles-bryant.gitbook.io/hello-react-and-typescript/typescriptandreact


実行環境は下記のとおりになります。

この分野は変化が激しく、毎年のように仕様が変わるので解決法をお探しのかたは記事の書かれた時期とバージョンに細心の注意を払ってください。

最後に繰り返しになりますが、私はスクリプト言語もフロントエンドも心の底から嫌いです。嫌いなので仕事では絶対に触りたくありません。

Contact Us