在Linux Mint上搭建Electron开发环境
在Linux Mint上搭建Electron开发环境。
最近我对Electron产生了兴趣,因此我在Linux Mint上尝试搭建了Electron开发环境。
Electron是用于使用JavaScript创建桌面应用程序的框架,以前被称为”AtomShell”。
-
- GitHub、JavaScriptでデスクトップアプリが作れるライブラリAtom Shellを、Electronに改称
http://codezine.jp/article/detail/8678
以下的步骤是在Linux Mint环境下进行验证的。在其他Linux发行版中可能会有一些不同的步骤。
安装Node.js
首先安装Node.js。
所需的包括nodejs和npm,我们可以搜索相关的包名来找到它们。
$ apt-cache search nodejs | grep ^nodejs
nodejs - evented I/O for V8 javascript
nodejs-dev - evented I/O for V8 javascript (development files)
nodejs-legacy - evented I/O for V8 javascript (legacy symlink)
nodejs-dbg - evented I/O for V8 javascript (debug)
$ apt-cache search npm | grep ^npm
npm - package manager for Node.js
请按照以下步骤安装Node.js和npm。
$ sudo apt-get install nodejs
$ sudo apt-get install npm
安装Electron
在中国社交媒体“抖音”上有类似的内容…我想从npm上安装Electron,但在这一过程中遇到了一个问题。npm默认将Node.js的命令识别为”node”,但是由于在Linux Mint中Node.js的命令是”nodejs”,所以直接安装Electron时会出现以下错误。
$ npm install electron-prebuilt -g
...中略...
sh: 1: node: not found
npm WARN This failure might be due to the use of legacy binary "node"
npm WARN For further explanations, please read
/usr/share/doc/nodejs/README.Debian
npm ERR! weird error 127
npm ERR! not ok code 0
因此,为了能够使用”node”命令,需要手动创建符号链接。
$ which node nodejs
/usr/bin/nodejs
$ cd /usr/bin
$ sudo ln -s nodejs node
$ file node
node: symbolic link to `nodejs'
您可以按照Electron文档中所述,使用以下命令进行安装。
$ npm install electron-prebuilt -g
$ npm install electron-prebuilt --save-dev
试着运行简单的示例代码。
可以使用Electron的”Quick Start”页面上的示例,也可以自己创建一个简单的示例来尝试。
- furandon-pig/electron_sample
请按照以下步骤执行示例。
$ git clone https://github.com/furandon-pig/electron_sample.git
$ cd electron_sample
$ electron hello/
运行界面会如下所示。

在使用Linux Mint环境下运行Electron时需要注意的事项。
当我在Linux Mint环境中尝试使用Electron时,遇到了日语显示乱码的问题,有些困扰了一下。以下方法似乎可以解决这个问题。
-
- headタグ中にを記述しておく
- styleタグでbodyのフォントを以下のように指定する
<style>
body {
font-family: 'Takao Pゴシック','Open Sans',Helvetica,Arial,sans-serif;
}
</style>
总结
我在Linux Mint上尝试运行了Electron。由于Electron的示例程序似乎还很少,所以我打算在参考官方文档的同时,增加更多的示例。