Search code examples
node.jsnpmnode-gyp

Howto notify NPM of dependency on native C++ library?


Say I have a dynamic C++ library 'MyLib', including public headers, that will install to the platform specific default folders/paths. I also have a NPM package that builds a native node extension 'MyLib.node', using node-gyp, that wraps the functionality of 'MyLib'.

Is there maybe a way to gracefully notify NPM that this package is dependent on the 'MyLib' library, using package.json, instead of letting it fail miserably during build? Or do I have to go via another route, like making it a full-blown platform specific install package (which I hope not, because I like cross-platforminess so much)


Solution

  • In short: no, NPM itself doesn't have such a feature.

    There are various methods to deal with native library requirements:

    • Use something like node-pre-gyp to download precompiled binaries, with fallback to compilation if there is no binary available for the platform in question. This method is used by, for instance, sqlite3 and canvas.
    • Ship with the C/C++ code as part of the package, and compile during installation. This method is used by, for instance, leveldown and libjpeg.
    • There are also packages that require libraries to be installed separately, for instance qrcodeine. Installing this package will just fail with a compilation error when one of its prerequisite libraries isn't installed.

    A drawback of the last method, aside from just failing with an error, is that it makes versioning difficult. If MyLib is being actively developed and changes regularly, you may run into the problem that a Node module that depends on it could be out of date with respect to the (globally installed) version of the library.