Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass an optional flag to a dependency of my npm package?

Tags:

node.js

npm

I'm working on an npm package, let's call it foo, that has a few external dependencies. One such dependency, bar requires a build flag in order to work with my project. If I were to manually install the dependencies I would say:

npm install bar --bar-option=1 ... # other deps npm install foo node script_that_uses_foo.js

I would like the dependencies of foo to be installed automatically with npm install foo. So I have a section in my package.json file that looks like this: "dependencies" : { "bar": "file:../../bar-0.1.0.tgz", "baz": "*" }

This works fine, except that bar is installed without --bar-option=1. How can I tell npm to pass this argument to the install script of bar? I've looked through the npm documentation and haven't found what I'm looking for.

Thanks for your help.

like image 645
matth Avatar asked Jul 22 '16 00:07

matth


People also ask

How do I add optional dependency?

To declare an optional dependency, add the <optional/> tag to the declared dependency in the plugin pom. xml . If any of the classes in the unavailable optional dependencies are used in the plugin a NoClassDefFoundError will be thrown.

How install optional dependencies npm?

Execute npm install someDependency --save-optional to install a package as an optional dependency. The installed package will be put into optionalDependencies . When you want to avoid installing optional dependencies, you can execute npm ci --no-optional (e.g. on CI tools like GitLab CI).

What flag do you use to save devDependencies to your package json file?

To add dependencies and devDependencies to a package. json file from the command line, you can install them in the root directory of your package using the --save-prod flag for dependencies (the default behavior of npm install ) or the --save-dev flag for devDependencies.


1 Answers

I know this is really old, but I believe that in this case, you can

    npm install foo --bar-option=1

npm will pass bar-option through to all dependencies, e.g. "bar" when installing them.

alternatively, inside of foo's package.json, you could define a preinstall script that does "npm install bar --bar-option=1"

like image 65
Mark Roy Avatar answered Oct 05 '22 13:10

Mark Roy