How to tell a native node module where the needed dlls are stored?

969 Views Asked by At

I am currently developing a native node module for the use in Node.Js and Electron that needs additional dlls to work (and whatever the equivalent on Linux and Mac would be). I'm not really into C++ development and i didn't find a solution to this question in the internet, so I could need some support from you :)

So it works all well when everything is stored in the same (working) directory, but this is not something you can work with in the long term. Especially when i pack everything into a module that can be loaded with NPM, the module with it's dlls is stored in a subfolder like "./node_modules/native-module/build/Release/adddon.node[or dll]".

I can easily include the module with require but it doesn't find any dlls anymore, because if I understand the C++ behaviour correctly it just looks in the PATH and the current working directory (which is wher Electron or Node.js is executed) for the dlls.

My first idea for a workaround was to just change the cwd to the "Release" folder where all the dlls are. But changing the cwd gets really messy when you like to use the hot reload functionality of webpack, which i use for development purposes. You always have to check if the cwd was already changed, it doesn't display the page correctly anymore, and i really think that's not the way to do.

So my question is what would be the correcy way to dynamically tell the node module where the dlls are stored? Thanks in advance.

PS: I'm using cmake-js for the build process if that's important.

1

There are 1 best solutions below

0
On

I "fixed" this problem by extending the Path environment variable at runtime using the node.js APIs. At least under Windows it's the default behaviour of native C++ programms to look in the Path and the CWD for Dlls.

Here is my Code, if anyone is interested:

let currentPath = process.env.Path; 
let dllDirectory = path.dirname( path.resolve(require.resolve('your-package-name/package.json'))) + `${path.sep}build${path.sep}Release` 
process.env.Path = currentPath + dllDirectory;