How do I tell a native node.js extension where to

2019-07-28 01:01发布

问题:

I’m installing scrypt (https://www.npmjs.com/package/scrypt) from npm. The installation involves a node-gyp build step that builds a native node.js extension. When I then start my app, it fails with the following error:

node index.js 
module.js:568
  return process.dlopen(module, path._makeLong(filename));
                 ^

Error: /package/host/localhost/gcc-4/lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by /home/xxxx/xxxx/node_modules/scrypt/build/Release/scrypt.node)
    at Error (native)
    at Object.Module._extensions..node (module.js:568:18)
    at Module.load (module.js:456:32)
    at tryModuleLoad (module.js:415:12)
    at Function.Module._load (module.js:407:3)
    at Module.require (module.js:466:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/home/xxxx/xxxx/node_modules/scrypt/index.js:3:20)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:456:32)
    at tryModuleLoad (module.js:415:12)
    at Function.Module._load (module.js:407:3)
    at Module.require (module.js:466:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/home/xxxx/xxxx/node_modules/unfun-cms/lib/utils/password.js:3:16)

which is not so surprising, because

[xxxx@xxxx nodeapp]$ strings /package/host/localhost/gcc-4/lib64/libstdc++.so.6 | grep "GLIBCXX_3\.4\.21"
[xxxx@xxxx nodeapp]$

while

[xxxx@xxxx nodeapp]$ strings /package/host/localhost/gcc-5/lib64/libstdc++.so.6 | grep "GLIBCXX_3\.4\.21"
GLIBCXX_3.4.21
[xxxx@xxxx nodeapp]$

I have

export PATH=/package/host/localhost/gcc-5/bin:$PATH
export LD_LIBRARY_PATH=/package/host/localhost/gcc-5/lib64:$LD_LIBRARY_PATH

in my .bashrc, and gcc --version shows 5.2.0 correctly, so I assume these settings are alright.

So the question is, how do I tell scrypt the path to the correct libstdc++.so.6, namely the one at /package/host/localhost/gcc-5/lib64/libstdc++.so.6? (And why does it even get it wrong?)

EDIT:

This is a CentOS machine. I’m using node.js version 6.1.0, npm version 3.8.6, and I’m trying to install scrypt version 6.0.3, which is the latest version at the time of this writing.

EDIT 2:

I tried to statically link the library by editing node_modules/scrypt/binding.gyp, adding

'libraries': ['/package/host/localhost/gcc-5/lib64/libstdc++.so.6'],

to each of the four specified targets, and then running node-gyp rebuild.

I also tried changing this to

'libraries': ['-L/package/host/localhost/gcc-5/lib64', '-lstdc++'],

, which appears to be syntactically valid, too—but still, it tries to load the library from the wrong path at runtime.

回答1:

I stubled with a similar trouble and had resolve it with a trick uses dynamic load of libstdc++.so.6 through LD_PRELOAD:

LD_PRELOAD='/path/to/lib/libstdc++.so.6' /usr/bin/nodejs app.js