Search code examples
node.jsnode-gypnode.js-addon

How to build node addon, so that it can be imported only by name, and not path


When I build a node add-on by creating a standard binding.gyp and running node-gyp build, the addon.node library is created in the subdirectory build/{Release|Debug}/. When I write tests or other javascript code to use this module, I have to give explicit path to the library location

For example,

var addon = require('./build/Release/addon')

However, I would like to do this by only specifying the module name and let node's module search for the library

var addon = require('addon')

How can that be achieved?


Solution

  • There's a module for that if you're okay with another dependency.

    https://www.npmjs.com/package/bindings

    That will search all of the possible build output locations automatically, including debug and release directories.