LoginSignup
33
24

More than 3 years have passed since last update.

nexeでNode.jsのアプリをパッケージングしてみた

Last updated at Posted at 2018-05-25

Node.jsのパッケージングというとpkgとかElectronで無理やりとかしか知らなかったのですが、
nexeというものを新しく知ったので使ってみました。

nexeだと1ファイルにまとまるので他の人に使ってもらうとかの場合に導入しやすいと思います。

使う前の準備

npm -g i nexe

単純なアプリの場合

ソース

index.js
console.log('hello world', process.version);

パッケージングする

nexe index.js

ここで必要に応じて prebuilt node.js のダウンロードが行われる。Proxy環境の場合、環境変数 HTTPS_PROXY=http://proxyuser:proxypass@proxyserver:port のように指定してやれば通り抜けられる。

実行結果

hello world v8.10.0

v8.11.2にしたら以下のエラーが出てしまっていました。まだ対応していないとかかな?

Error: windows-x64-8.11.2 not available, create it using the --build flag

分かったこと、気づいたこと

  1. Node.jsのバージョンはデフォルトは、node -vで取得できるものを使用する(パッケージング時にバージョン指定もできる)
  2. exeの名前のデフォルトは親ディレクトリ名?(少なくともpackage.jsonからではなかった)

外部モジュールをrequireする場合

ソース

index.js
const _ = require('underscore');

console.log('hello world', process.version);
console.log(_.zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]));

パッケージングする

nexe index.js

実行結果

hello world v8.10.0
[ [ 'moe', 30, true ],
  [ 'larry', 40, false ],
  [ 'curly', 50, false ] ]

node_modulesディレクトリを消してから実行してみる

hello world v8.10.0
[ [ 'moe', 30, true ],
  [ 'larry', 40, false ],
  [ 'curly', 50, false ] ]

依存パッケージはexeの中に埋め込まれているみたいですね

分かったこと、気づいたこと

  1. requireの行をコメントアウトしてパッケージングしてみると、ちょうどunderscore.js本体の容量(57KB)だけexeファイルが軽くなっていたので特に圧縮とかはしていない様子(underscore-min.jsなら18KB)

まとめ

Node.jsがそのまま入るようで、ファイルサイズは大きくなりがちですが、1ファイルの実行可能ファイルができるのは便利そうです。

参考
プラットフォームの指定などもできるようですね
http://d.hatena.ne.jp/Kazuhira/20180212/1518447476

33
24
1

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
33
24