npm installで出るnode-gypのビルドエラーの直し方
npm install の途中で gyp ERR! not ok が出て止まったら、C++ で書かれたパッケージをビルドするための道具(Python、make、C/C++ コンパイラー)が足りていません。Ubuntu なら apt-get install python3 make g++ で3つを入れると、同じ npm install が通ります。
Docker の Ubuntu や Debian のイメージなど、開発用の道具を入れていない Linux で npm install すると出るエラーです。私は検証環境で道具を1つずつ足しながらエラーを起こし、どの道具がないとどのメッセージになるかと、入れると消えることを確かめました。
検証環境は、Docker の ubuntu:24.04(Ubuntu 24.04.5 LTS)に Node.js 22.23.5(npm 10.9.9、同梱の node-gyp 11.5.0)の公式バイナリだけを入れたコンテナです。Python、make、コンパイラーは入っていません。パッケージは、C の XML パーサー libexpat を C++ のアドオンで Node.js から使えるようにする node-expat 2.4.1 を使いました。Docker 公式の node:22-bookworm-slim(Debian)では、エラーが出ることだけを確かめ、直す手順は試していません。Windows、macOS、Alpine Linux、ほかのバージョンの Node.js、yarn や pnpm も試していません。
エラーの見分け方
node-gyp は、Python → make → コンパイラーの順に道具を使います。足りない道具によって、出るメッセージが変わりました。
| 足りないもの | 探す行 | 入れるもの(Ubuntu) |
|---|---|---|
| Python | Could not find any Python installation to use |
python3 |
| make | Error: not found: make |
make |
| C/C++ コンパイラー | make: cc: No such file or directory または make: g++: No such file or directory |
g++ |
どの場合も、最後は gyp ERR! not ok で終わり、npm install の終了コードは1でした。最後の行だけを見ても原因は分かりません。表の「探す行」を、出力の中から探します。
npm install で node-gyp が動く理由
node-expat の package.json には、"install": "node-gyp rebuild" が書かれています。npm install はこの install スクリプトを実行し、パッケージに入っている C++ のソース(node-expat.cc)を、その場でビルドします。
node-gyp の README の Unix で必要なものには、次の3つが挙げられています。
- サポートされているバージョンの Python
make- GCC などの C/C++ コンパイラー
このうち1つでもないと、ビルドが途中で止まります。Python は、node-gyp が binding.gyp から Makefile を作るのに使います。
Python がないとき
道具が何もない状態で npm install node-expat@2.4.1 を実行すると、次のように出て止まりました(終了コードは1)。パスは例示用の値に、System の行のカーネルのバージョンは 6.x に置き換えています。
$ npm install node-expat@2.4.1
npm error code 1
npm error path /path/to/app/node_modules/node-expat
npm error command failed
npm error command sh -c node-gyp rebuild
npm error gyp info it worked if it ends with ok
npm error gyp info using node-gyp@11.5.0
npm error gyp info using node@22.23.5 | linux | x64
npm error gyp ERR! find Python
npm error gyp ERR! find Python Python is not set from command line or npm configuration
npm error gyp ERR! find Python Python is not set from environment variable PYTHON
npm error gyp ERR! find Python checking if "python3" can be used
npm error gyp ERR! find Python - executable path is ""
npm error gyp ERR! find Python - "" could not be run
npm error gyp ERR! find Python checking if "python" can be used
npm error gyp ERR! find Python - executable path is ""
npm error gyp ERR! find Python - "" could not be run
npm error gyp ERR! find Python
npm error gyp ERR! find Python **********************************************************
npm error gyp ERR! find Python You need to install the latest version of Python.
npm error gyp ERR! find Python Node-gyp should be able to find and use Python. If not,
npm error gyp ERR! find Python you can try one of the following options:
npm error gyp ERR! find Python - Use the switch --python="/path/to/pythonexecutable"
npm error gyp ERR! find Python (accepted by both node-gyp and npm)
npm error gyp ERR! find Python - Set the environment variable PYTHON
npm error gyp ERR! find Python - Set the npm configuration variable python:
npm error gyp ERR! find Python npm config set python "/path/to/pythonexecutable"
npm error gyp ERR! find Python For more information consult the documentation at:
npm error gyp ERR! find Python https://github.com/nodejs/node-gyp#installation
npm error gyp ERR! find Python **********************************************************
npm error gyp ERR! find Python
npm error gyp ERR! configure error
npm error gyp ERR! stack Error: Could not find any Python installation to use
npm error gyp ERR! stack at PythonFinder.fail (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/find-python.js:306:11)
npm error gyp ERR! stack at PythonFinder.findPython (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/find-python.js:164:17)
npm error gyp ERR! stack at process.processTicksAndRejections (node:internal/process/task_queues:103:5)
npm error gyp ERR! stack at async configure (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:27:18)
npm error gyp ERR! stack at async run (/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js:81:18)
npm error gyp ERR! System Linux 6.x
npm error gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
npm error gyp ERR! cwd /path/to/app/node_modules/node-expat
npm error gyp ERR! node -v v22.23.5
npm error gyp ERR! node-gyp -v v11.5.0
npm error gyp ERR! not ok
npm error A complete log of this run can be found in: ~/.npm/_logs/2026-11-20T00_27_40_440Z-debug-0.log
find Python の行は、node-gyp が Python を探した順番です。コマンドラインと npm の設定、環境変数 PYTHON を見たあと、python3 と python を探し、どちらも見つかりませんでした。原因を示すのは Error: Could not find any Python installation to use の行です。
失敗した npm install は、何も残しませんでした。node_modules はできず、package.json に dependencies も足されていません。
Python は入っているのに別の場所にある場合は、メッセージの中にあるとおり、--python や環境変数 PYTHON で場所を教えられます。node-gyp の README の Python の指定のしかたにも、同じ方法が書かれています。
make がないとき
同じコンテナに python3 だけを入れて(入ったのは Python 3.12.3)、もう一度 npm install node-expat@2.4.1 を実行しました。Python は見つかり、Node.js のヘッダーファイルもダウンロードされましたが、今度は make がないところで止まりました(終了コードは1)。
$ npm install node-expat@2.4.1
npm error code 1
npm error path /path/to/app/node_modules/node-expat
npm error command failed
npm error command sh -c node-gyp rebuild
npm error gyp info it worked if it ends with ok
npm error gyp info using node-gyp@11.5.0
npm error gyp info using node@22.23.5 | linux | x64
npm error gyp info find Python using Python version 3.12.3 found at "/usr/bin/python3"
npm error gyp http GET https://nodejs.org/download/release/v22.23.5/node-v22.23.5-headers.tar.gz
npm error gyp http 200 https://nodejs.org/download/release/v22.23.5/node-v22.23.5-headers.tar.gz
npm error gyp http GET https://nodejs.org/download/release/v22.23.5/SHASUMS256.txt
npm error gyp http 200 https://nodejs.org/download/release/v22.23.5/SHASUMS256.txt
npm error gyp info spawn /usr/bin/python3
npm error gyp info spawn args [
npm error gyp info spawn args '/usr/local/lib/node_modules/npm/node_modules/node-gyp/gyp/gyp_main.py',
npm error gyp info spawn args 'binding.gyp',
npm error gyp info spawn args '-f',
npm error gyp info spawn args 'make',
npm error gyp info spawn args '-I',
npm error gyp info spawn args '/path/to/app/node_modules/node-expat/build/config.gypi',
npm error gyp info spawn args '-I',
npm error gyp info spawn args '/usr/local/lib/node_modules/npm/node_modules/node-gyp/addon.gypi',
npm error gyp info spawn args '-I',
npm error gyp info spawn args '~/.cache/node-gyp/22.23.5/include/node/common.gypi',
npm error gyp info spawn args '-Dlibrary=shared_library',
npm error gyp info spawn args '-Dvisibility=default',
npm error gyp info spawn args '-Dnode_root_dir=~/.cache/node-gyp/22.23.5',
npm error gyp info spawn args '-Dnode_gyp_dir=/usr/local/lib/node_modules/npm/node_modules/node-gyp',
npm error gyp info spawn args '-Dnode_lib_file=~/.cache/node-gyp/22.23.5/<(target_arch)/node.lib',
npm error gyp info spawn args '-Dmodule_root_dir=/path/to/app/node_modules/node-expat',
npm error gyp info spawn args '-Dnode_engine=v8',
npm error gyp info spawn args '--depth=.',
npm error gyp info spawn args '--no-parallel',
npm error gyp info spawn args '--generator-output',
npm error gyp info spawn args 'build',
npm error gyp info spawn args '-Goutput_dir=.'
npm error gyp info spawn args ]
npm error gyp ERR! build error
npm error gyp ERR! stack Error: not found: make
npm error gyp ERR! stack at getNotFoundError (/usr/local/lib/node_modules/npm/node_modules/which/lib/index.js:16:17)
npm error gyp ERR! stack at which (/usr/local/lib/node_modules/npm/node_modules/which/lib/index.js:77:9)
npm error gyp ERR! stack at async doWhich (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:120:22)
npm error gyp ERR! stack at async loadConfigGypi (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:78:7)
npm error gyp ERR! stack at async build (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:36:3)
npm error gyp ERR! stack at async run (/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js:81:18)
npm error gyp ERR! System Linux 6.x
npm error gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
npm error gyp ERR! cwd /path/to/app/node_modules/node-expat
npm error gyp ERR! node -v v22.23.5
npm error gyp ERR! node-gyp -v v11.5.0
npm error gyp ERR! not ok
npm error A complete log of this run can be found in: ~/.npm/_logs/2026-11-20T00_28_05_276Z-debug-0.log
gyp info spawn /usr/bin/python3 までは進んでいます。Python で Makefile を作ったあと、それを動かす make が見つからず、Error: not found: make になりました。
コンパイラーがないとき
続けて make を入れると(GNU Make 4.3)、エラーの中身がまた変わりました(終了コードは1)。
$ npm install node-expat@2.4.1
npm error code 1
npm error path /path/to/app/node_modules/node-expat
npm error command failed
npm error command sh -c node-gyp rebuild
npm error make: Entering directory '/path/to/app/node_modules/node-expat/build'
npm error CC(target) Release/obj.target/expat/deps/libexpat/lib/xmlparse.o
npm error make: Leaving directory '/path/to/app/node_modules/node-expat/build'
npm error gyp info it worked if it ends with ok
npm error gyp info using node-gyp@11.5.0
npm error gyp info using node@22.23.5 | linux | x64
npm error gyp info find Python using Python version 3.12.3 found at "/usr/bin/python3"
npm error gyp info spawn /usr/bin/python3
npm error gyp info spawn args [
npm error gyp info spawn args '/usr/local/lib/node_modules/npm/node_modules/node-gyp/gyp/gyp_main.py',
npm error gyp info spawn args 'binding.gyp',
npm error gyp info spawn args '-f',
npm error gyp info spawn args 'make',
npm error gyp info spawn args '-I',
npm error gyp info spawn args '/path/to/app/node_modules/node-expat/build/config.gypi',
npm error gyp info spawn args '-I',
npm error gyp info spawn args '/usr/local/lib/node_modules/npm/node_modules/node-gyp/addon.gypi',
npm error gyp info spawn args '-I',
npm error gyp info spawn args '~/.cache/node-gyp/22.23.5/include/node/common.gypi',
npm error gyp info spawn args '-Dlibrary=shared_library',
npm error gyp info spawn args '-Dvisibility=default',
npm error gyp info spawn args '-Dnode_root_dir=~/.cache/node-gyp/22.23.5',
npm error gyp info spawn args '-Dnode_gyp_dir=/usr/local/lib/node_modules/npm/node_modules/node-gyp',
npm error gyp info spawn args '-Dnode_lib_file=~/.cache/node-gyp/22.23.5/<(target_arch)/node.lib',
npm error gyp info spawn args '-Dmodule_root_dir=/path/to/app/node_modules/node-expat',
npm error gyp info spawn args '-Dnode_engine=v8',
npm error gyp info spawn args '--depth=.',
npm error gyp info spawn args '--no-parallel',
npm error gyp info spawn args '--generator-output',
npm error gyp info spawn args 'build',
npm error gyp info spawn args '-Goutput_dir=.'
npm error gyp info spawn args ]
npm error gyp info spawn make
npm error gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
npm error make: cc: No such file or directory
npm error make: *** [deps/libexpat/expat.target.mk:119: Release/obj.target/expat/deps/libexpat/lib/xmlparse.o] Error 127
npm error gyp ERR! build error
npm error gyp ERR! stack Error: `make` failed with exit code: 2
npm error gyp ERR! stack at ChildProcess.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:219:23)
npm error gyp ERR! System Linux 6.x
npm error gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
npm error gyp ERR! cwd /path/to/app/node_modules/node-expat
npm error gyp ERR! node -v v22.23.5
npm error gyp ERR! node-gyp -v v11.5.0
npm error gyp ERR! not ok
npm error A complete log of this run can be found in: ~/.npm/_logs/2026-11-20T00_28_10_987Z-debug-0.log
gyp ERR! の行には `make` failed with exit code: 2 としか出ていません。本当の原因は、その少し上の make: cc: No such file or directory です。make が C のファイルをコンパイルして xmlparse.o を作ろうとしたところで、C コンパイラーの cc が見つかりませんでした。make: で始まる行は、出力の上のほう(make: Entering directory の行)にも出ています。
node-expat は、C で書かれたライブラリ(libexpat)を一緒にビルドするので、先に cc で止まりました。別のコンテナで、C コンパイラーの gcc だけを入れて g++ を入れなかったときは、libexpat のコンパイルは進み、C++ のファイル(node-expat.cc)のところで make: g++: No such file or directory になりました。
python3、make、g++ を入れると消える
続けて g++ を入れると(g++ 13.3.0)、同じ npm install node-expat@2.4.1 がエラーなしで終わりました(終了コードは0)。
$ npm install node-expat@2.4.1
added 4 packages, and audited 5 packages in 5s
found 0 vulnerabilities
ビルドでできた node_modules/node-expat/build/Release/node_expat.node は、x86-64 の Linux 用のファイルでした。node -e で読み込み、XML を読ませると、要素名と属性、テキストを取り出せました。
$ node -e 'const expat = require("node-expat"); const p = new expat.Parser("UTF-8"); p.on("startElement", (name, attrs) => console.log("start:", name, attrs)); p.on("text", t => console.log("text:", t)); console.log("parse:", p.write("<greeting lang=\"ja\">こんにちは</greeting>"));'
start: greeting { lang: 'ja' }
text: こんにちは
parse: true
1つずつ入れなくても、3つをまとめて入れれば足ります。新しいコンテナで、npm install が失敗するのを確かめてから、次のコマンドで入れました。コンテナの中では root で実行したので、sudo は付けていません。
apt-get update
apt-get install -y python3 make g++
これで npm install node-expat@2.4.1 は通り、package.json の dependencies に "node-expat": "^2.4.1" が入りました。g++ を入れると、C コンパイラーの cc(gcc)も一緒に入ります。次の2つの入れ方でも、同じように通りました。
apt-get install -y build-essential python3(make、gcc、g++をまとめて入れるパッケージ)apt-get install -y --no-install-recommends python3 make g++(推奨パッケージを入れない指定を付けたもの)
Docker 公式の node:22-bookworm-slim イメージにも Python、make、コンパイラーは入っておらず、同じ Could not find any Python installation to use で止まりました。
–ignore-scripts で入れても動かない
npm install --ignore-scripts を付けると、install スクリプトが動かないので、道具がなくてもエラーなしで終わりました。ただし、ビルドされていないので、読み込むと失敗しました(パスは例示用の値に置き換えています)。
$ node -e 'require("node-expat")'
/path/to/app/node_modules/bindings/bindings.js:121
throw e;
^
Error: /path/to/app/node_modules/node-expat/build/Release/node_expat.node: invalid ELF header
at Object..node (node:internal/modules/cjs/loader:1939:18)
at Module.load (node:internal/modules/cjs/loader:1505:32)
at Function._load (node:internal/modules/cjs/loader:1309:12)
at wrapModuleLoad (node:internal/modules/cjs/loader:254:19)
at Module.require (node:internal/modules/cjs/loader:1527:12)
at require (node:internal/modules/helpers:147:16)
at bindings (/path/to/app/node_modules/bindings/bindings.js:112:48)
at Object.<anonymous> (/path/to/app/node_modules/node-expat/lib/node-expat.js:4:34)
at Module._compile (node:internal/modules/cjs/loader:1781:14)
at Object..js (node:internal/modules/cjs/loader:1913:10) {
code: 'ERR_DLOPEN_FAILED'
}
Node.js v22.23.5
node-expat 2.4.1 の npm のパッケージには、もともと build/Release/node_expat.node が入っていました。検証環境で file コマンドで調べると、macOS(arm64)用のファイルでした。Linux ではこれを読み込めず、invalid ELF header になります。ビルドの道具を入れて npm install すると、このファイルが Linux 用にビルドし直されました。--ignore-scripts は、エラーを消す方法ではありません。




