nodejs / node-gyp
node-gyp is a cross-platform command-line tool written in Node.js forcompiling native addon modules for Node.js.
Node.js 自体のビルドには node-gyp は使用されないことに注意してください。0.8, …, 4, 5, 6, などの複数のターゲットバージョンの Node.js がサポートされています。
特徴
- サポートされているどのプラットフォームでも同じビルドコマンドで動作します
- 異なるバージョンの Node.js のターゲティングをサポートする。js
Installation
You can install node-gyp using npm:
npm install -g node-gyp
Different on your operating system, you will need to install:
On Unix
- Python v3.6, v3.7, v3.8, または v3. No.1。9
make- GCC のような適切な C/C++ コンパイラー ツールチェーン
macOS
ATTENTION: あなたの Mac が macOS Catalina (10.X) にアップグレードされた場合、このツールチェーンが必要です。15) の場合は macOS_Catalina.md.
- Python v3.6, v3.7, v3.8, または v3.9
- Xcode
- また
XCode Command Line Toolsをxcode-select --installでインストールする必要があるようです。 または、すでにXcodeのフルバージョンがインストールされている場合は、メニューXcode -> Open Developer Tool -> More Developer Tools...で見つけることができます。 このステップでは、clang、clang++、およびmakeをインストールします。
Windows では
Microsoft Store パッケージから最新バージョンの Python をインストールします。
ツールと設定を手動でインストールする:
- Install Visual C++ Build Environment.をインストールします。 Visual Studio Build Tools(using “Visual C++ build tools” workload) or Visual Studio 2017 Community(using the “Desktop development with C++” workload)
- Launch cmd,
npm config set msvs_version 2017
上記の手順がうまくいかない場合は、Microsoft の Node.js Guidelines for Windows でその他のヒントをご覧になってください。
ARM 上の Windows 10 で ARM64 ネイティブ Node.js をターゲットにするには、「ARM64 用 Visual C++ コンパイラーおよびライブラリ」と「ARM64 用 Visual C++ ATL」を追加します。
Python 依存関係の構成
node-gypには、互換バージョンの Python (バージョン 3.6, v3.7, v3.8 または v3.9) をインストールしていることが必要です。- コマンドラインオプション
--pythonを設定することにより、どのバージョンの Python をnode-gypが使用するかを特定することができます。:
node-gyp <command> --python /path/to/executable/python
-
node-gypがnpmによって呼ばれ、複数のバージョンの Python がインストールされている場合、npmの ‘python’ 設定キーに適切な値を設定することが可能です。
npm config set python /path/to/executable/python
-
環境変数
PYTHONに Python 実行ファイルのパスを設定すると、互換性があればそのバージョンが使用されるようになります。 -
環境変数
NODE_GYP_FORCE_PYTHONに Python 実行ファイルのパスが設定されている場合、他の設定された Python 検索パスや内蔵のパスではなく、そのパスが使用されます。
How to Use
ネイティブアドオンをコンパイルするには、まずそのルートディレクトリに移動します:
cd my_node_addon
次のステップは、現在のプラットフォームに適したプロジェクト構築ファイルを生成します。 これには
configureを使用します:node-gyp configure
Auto-detection fails for Visual C++ Build Tools 2015, so
--msvs_version=2015needs to be added (not needed when npm run as configured above):node-gyp configure --msvs_version=2015
注:
configureステップは処理用に currentdirectory でbinding.gypファイルを探します。ここで、
Makefile(Unix プラットフォーム) またはvcxprojファイル (Windows) がbuild/ディレクトリに作成されます。 次に、buildコマンドを実行します:node-gyp build
これで、コンパイル済みの
.nodeバインディングファイルができました! コンパイルされたバインディングファイルは、ビルドモードに応じてbuild/Debug/またはbuild/Release/で終わります。 この時点で、Node.js で.nodeファイルを要求し、テストを実行できます!Note: バインディングファイルの Debug ビルドを作成するには、
configure、build、rebuildコマンドのいずれかを実行中に--debug(か-d) スイッチを渡します。bind.gyp ファイル
binding.gypファイルにはモジュールを構築する構成が JSON 形式で記述されています。 このファイルは、package.jsonと一緒にパッケージのルートに置かれます。Node.js アドオンを構築するのに適した基本的な
gypファイルは、次のようになります:{ "targets": } ]}Further reading
Node.Js のためのいくつかの追加のリソースがあります。ネイティブ アドオンと
gyp設定ファイルの書き方:- “Going Native” a nodeschool.io tutorial
- “Hello World” node addon example
- gyp user documentation
- gyp input format reference
- “bind.Js” node.Js addon example
“Node “設定ファイル:Node.Js adon.js
- “Node.Jsの設定ファイルを書き込むには?gyp” files out in the wild wiki page
- また
Commands
node-gyp は、以下のコマンドに応答します。
| Command | Description |
|---|---|
help |
ヘルプ画面を表示 |
build |
make/msbuild.exeを起動し、Nativeなビルドを行う addon |
clean |
存在すれば build ディレクトリを削除 |
configure |
現在のプラットフォーム用にプロジェクトのビルドファイルを生成 |
rebuild |
cleanを実行します。 configure と build を一度にインストールする |
install |
Installs Node.NET Framework をインストールする。js のヘッダーファイルを指定されたバージョンにインストールします |
list |
現在インストールされている Node.js のヘッダーバージョン一覧 |
remove |
Node.js を削除します |
list |
Node.js を削除されたバージョン一覧 |
コマンドオプション
node-gyp は、以下のコマンドオプションを受け付けます。
| Command | Description | |
|---|---|---|
-j n, --jobs n |
Run make in parallelを実行します。 値maxは利用可能なすべてのCPUコアを使用します |
|
--target=v6.2.1 |
Node.js バージョン (デフォルトは process.version) |
|
--silly, --loglevel=silly |
Log all progress to console | |
--verbose.Log for the js version to build for the console |
||
, --loglevel=verbose |
Log most progress to console | |
--silent, --loglevel=silent |
Don’t log anything to console | |
debugを使用する場合。 --debug |
Make Debug build (デフォルトは Release) |
|
--release, --no-debug |
Make Release build | |
-C $dir, --directory=$dir |
別のディレクトリでコマンドを実行する | |
--make=$make |
makeコマンドをオーバーライドする(例.g. gmake) |
|
--thin=yes |
Enable thin static libraries | |
--arch=$arch |
Set target architecture (e.g.). ia32) | |
--tarball=$path |
Get headers from a local tarball | |
--devdir=$path |
SDKダウンロード・ディレクトリー (デフォルトはOSのキャッシュディレクトリ) | |
--ensure |
すでにヘッダーがある場合は再インストールしない | |
--dist-url=$url |
Header tarball をカスタム URL からダウンロードする | |
--proxy=$url |
Set HTTP(S) proxy for downloading header tarball | |
--noproxy=$urls |
Set urls to ignore proxies when downloading header tarball | |
--cafile=$cafile |
||
--nodedir=$path |
Set path to node source code | |
--python=$path |
Setパス to Python binary | |
--msvs_version=$version |
Set Visual Studio version (Windows only) | |
| Set Visual Studio Version (Windows only) | --solution=$solution |
Set Visual Studio Solution version (Windows only) |
Configuration
Environment variables
上記のコマンド オプションには npm_config_OPTION_NAME 形式を使用します(オプション名のダッシュは、アンダースコアに置き換えられる必要があります)。
例えば、devdir を /tmp/.gyp と等しくするには、次のようにします:
Unix ではこれを実行:
export npm_config_devdir=/tmp/.gyp
または Windows ではこれ:
set npm_config_devdir=c:\temp\.gyp
npm configuration
上記のいずれかのコマンドオプションを OPTION_NAME フォームに変換して使用します。
例えば、devdirを/tmp/.gypと同じにするには、次のように実行します:
npm config set devdir /tmp/.gyp
注意: npm経由で設定した設定は、を直接実行した時のみ使われ、 node-gyp を実行すると使えない。
License
node-gyp は MIT ライセンスで提供されています。 詳しくは LICENSEfile を参照されたい。
Leave a Reply