nodejs / nodejs/node-gyp

node-gyp builds on wrong nodejs version

Open
#2,453 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
10.7k
Forks
1.9k
Avg merge
1d 4h
Merged PRs (30d)
5

Description

I have problems with building my own module on nodejs versions that differ from the system version.

  • Node Version: v12.22.2 or v16.4.1
  • Platform: Linux 5.10.42-1-MANJARO # 1 SMP PREEMPT Thu Jun 3 14:37:11 UTC 2021 x86_64 GNU/Linux
  • Compiler: gcc v11.1.0 (GCC)

I have two versions of nodejs in my nvm: v12 and v16. v16 is my main system version.
Here you can see I use v12:

$ nvm list
->     v12.22.2
        v16.4.1
         system
default -> 12 (-> v12.22.2)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v16.4.1) (default)
stable -> 16.4 (-> v16.4.1) (default)
$ node -v
v12.22.2

I want to make a node c++ addon for node 12.
Here you can see node-gyp uses args for 12th version:

$ node-gyp configure
gyp info it worked if it ends with ok
gyp info using node-gyp@8.1.0
gyp info using node@12.22.2 | linux | x64
gyp info find Python using Python version 3.9.5 found at "/usr/bin/python3"
gyp info spawn /usr/bin/python3
gyp info spawn args [
gyp info spawn args   '/home/unlomtrois/.nvm/versions/node/v12.22.2/lib/node_modules/node-gyp/gyp/gyp_main.py',
gyp info spawn args   'binding.gyp',
gyp info spawn args   '-f',
gyp info spawn args   'make',
gyp info spawn args   '-I',
gyp info spawn args   '/home/unlomtrois/development/node/node-cpp-module-test-task/build/config.gypi',
gyp info spawn args   '-I',
gyp info spawn args   '/home/unlomtrois/.nvm/versions/node/v12.22.2/lib/node_modules/node-gyp/addon.gypi',
gyp info spawn args   '-I',
gyp info spawn args   '/home/unlomtrois/.cache/node-gyp/12.22.2/include/node/common.gypi',
gyp info spawn args   '-Dlibrary=shared_library',
gyp info spawn args   '-Dvisibility=default',
gyp info spawn args   '-Dnode_root_dir=/home/unlomtrois/.cache/node-gyp/12.22.2',
gyp info spawn args   '-Dnode_gyp_dir=/home/unlomtrois/.nvm/versions/node/v12.22.2/lib/node_modules/node-gyp',
gyp info spawn args   '-Dnode_lib_file=/home/unlomtrois/.cache/node-gyp/12.22.2/<(target_arch)/node.lib',
gyp info spawn args   '-Dmodule_root_dir=/home/unlomtrois/development/node/node-cpp-module-test-task',
gyp info spawn args   '-Dnode_engine=v8',
gyp info spawn args   '--depth=.',
gyp info spawn args   '--no-parallel',
gyp info spawn args   '--generator-output',
gyp info spawn args   'build',
gyp info spawn args   '-Goutput_dir=.'
gyp info spawn args ]
gyp info ok
$ node-gyp build
gyp info it worked if it ends with ok
gyp info using node-gyp@8.1.0
gyp info using node@12.22.2 | linux | x64
gyp info spawn make
gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
make: вход в каталог «/home/unlomtrois/development/node/node-cpp-module-test-task/build»
  CXX(target) Release/obj.target/fibonacci/src/fibonacci.o
  SOLINK_MODULE(target) Release/obj.target/fibonacci.node
  COPY Release/fibonacci.node
make: выход из каталога «/home/unlomtrois/development/node/node-cpp-module-test-task/build»
gyp info ok

But after I configure and build my module, I cannot run it. It says It was compiled for 93th (16.x) version, and I cannot run it on node 12.

$ node build/Release/fibonacci.node 
internal/modules/cjs/loader.js:1057
  return process.dlopen(module, path.toNamespacedPath(filename));
                 ^

Error: The module '/home/unlomtrois/development/node/node-cpp-module-test-task/build/Release/fibonacci.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 93. This version of Node.js requires
NODE_MODULE_VERSION 72. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).
    at Object.Module._extensions..node (internal/modules/cjs/loader.js:1057:18)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
    at internal/main/run_main_module.js:17:47

When I swtich to my system node version, I can run and use this build.

I think node-gyp compiled my module using the system version (16.4.1) binaries and headers.
How to fix this behaviour?

If you ask me why I need that old nodejs version: these are the conditions of the test task. I can't use my system version to perform this task.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reported node-gyp configure and node-gyp build commands, then inspect binding.gyp, build/config.gypi, and the Node headers under .cache/node-gyp/12.22.2. Compare the version used during compilation with the runtime error's module versions, and verify the fix by rebuilding and loading build/Release/fibonacci.node under Node 12.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, nodejs, python
Domain
build-system, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.