Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

npm package.json OS specific dependency

Is there a way to specify OS specific dependencies in a npm package.json file?

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

like image 282
sandeepmistry Avatar asked Mar 02 '13 15:03

sandeepmistry


People also ask

How npm install specific dependencies?

For npm install specific version, use npm install [package-name]@[version-number]. Use npm view [package-name] version to know the specific latest version of a package available on the npm registry. Use npm list [package-name] to know the specific latest version of an installed package.

Does package json install dependencies?

By default, npm install will install all modules listed as dependencies in package. json . With the --production flag (or when the NODE_ENV environment variable is set to production ), npm will not install modules listed in devDependencies .

What are dependencies in package json?

The dependencies value is used to specify any other modules that a given module (represented by the package. json ) requires to work. When you run npm install from the root folder of a given module, it will install any modules listed in that dependencies object.


2 Answers

There's a possible good way of doing this, depending on your setup.

npm package.json supports an os key,

and also optionalDependencies

  • os can be used to specify which OS a module can be installed on.
  • optionalDependencies are module dependencies that if they cannot be installed, npm skips them and continues installing.

In this way you can have your module have an optional dependency for each OS, and only the one which works will be loaded/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 "optional". Making all versions of the dependency optional means that if your installation fails for a legitimate reason, it will silently skip installation and you will be missing a dependency you really need.

like image 71
TinyTimZamboni Avatar answered Sep 21 '22 08:09

TinyTimZamboni


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

If that doesn't work for you, you might be able to use an install script to get the result you're going for - https://docs.npmjs.com/misc/scripts

I haven't tested this but I think it would work:

Add something like this to your package.json:

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

And then add a install_dependencies.js file that checks the OS and runs the appropriate npm install ... commands.

like image 32
Nathan Friedly Avatar answered Sep 21 '22 08:09

Nathan Friedly