Run A Binary File With Shared Object Dependencies On NodeJS / Express Server
I'm using NodeJS childprocess to run shell commands on NodeJS / Express server (localhost). Everything goes well with basic shell commands like 'ls' (from NodeJS documentation):
const { spawn } = require('child_process');
const ls = spawn('ls', ['-lh', '/usr']);
ls.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
ls.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
});
ls.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
Now, what I need to do is run a binary file that has shared object dependencies. The objects are located inside server folder 'public/libraries'. I've tried to modify the previous with
const runBinary = spawn('/path/to/binary/file', ['-someoption', 'public/libraries']);
The error message is 'error while loading shared libraries ... no such file or directory', meaning the shared objects are not found. I have no idea how to
1) tell 'runBinary' the location of the folder of shared objects, and then
2) run the binary successfully to create output in console.
I have all the necessary tools installed to run the binary in a Bash shell and have tested it works as it should.
Any ideas?
Answer
You can fix this by setting the environment variable LD_LIBRARY_PATH
to the absolute path of your public/libraries
directory.
Something like this:
spawn('/path/to/binary/file', ['-someoption'],
{env: {LD_LIBRARY_PATH: '/path/to/public/libraries'}})
Related Questions
- → Maximum call stack exceeded when instantiating class inside of a module
- → Browserify api: how to pass advanced option to script
- → Node.js Passing object from server.js to external modules?
- → gulp-rename makes copies, but does not replace
- → requiring RX.js in node.js
- → Remove an ObjectId from an array of objectId
- → Can not connect to Redis
- → React: How to publish page on server using React-starter-kit
- → Express - better pattern for passing data between middleware functions
- → Can't get plotly + node.js to stream data coming through POST requests
- → IsGenerator implementation
- → Async/Await not waiting
- → (Socket.io on nodejs) Updating div with mysql data stops without showing error