Npm specific package.json dependency

Can I specify specific OS dependencies in the npm package.json file?

For example, I would like to set 'dbus' ( https://npmjs.org/package/dbus ) as a dependency for my module if the user is running Linux. I would have another dependency for Mac and Windows.

+42
npm node-gyp
Mar 02 '13 at 15:23
source share
3 answers

This may be a good way to do this, depending on your setup.

npm package.json supports the os key as well as optionalDependencies

os can be used to indicate which OS the module can be installed on. optionalDependencies are module dependencies that, if they cannot be installed, npm skips them and continues the installation.

Thus, your module may have an optional dependency for each OS, and only the one that works / will be downloaded / installed. ^

EDIT: As @Sebastien mentions below, this approach is dangerous . For any given OS, at least one of your dependencies is “required” and the rest is “optional”. Providing all versions of the dependencies does not necessarily mean that if your installation fails for a good reason, it will silently skip the installation and you will not need the dependency that you really need.

+24
Sep 26 '14 at 23:14
source share

I think the short answer is no. I can think of a few workarounds - the simplest thing is to simply add everything to package.json regardless of the OS, and then require() correct at runtime.

If this does not work for you, you can use the installation script to get the result that you are going to use - https://docs.npmjs.com/misc/scripts

I have not tested this, but I think this will work:

Add something like this to your package.json package:

 ,"scripts": { "install": "node install_dependencies.js" } 

Then add the install_dependencies.js file that checks the OS and runs the corresponding npm install ... commands.

+5
Mar 27 '13 at 21:42
source share

There is also a bindings-shyp module : https://www.npmjs.org/package/bindings-shyp

Helper module for loading the module’s own .node file.

This is a helper module for authors of Node.js. add-ons built-in modules. This is basically a “Swiss army knife” requiring () your own module .node file.

During the history of Node's native add-ons, add-ons were compiled in different places, depending on which build tool and which version of Node was used. Worse, now the gyp build tool can create either the Release or Debug builds, each of which is embedded in different places.

This module checks all possible locations in which its own add-on will be built in and returns the first one that successfully loads.

+1
Sep 26 '14 at 23:37
source share



All Articles