0

I need to add a dependency in my package.json and load module, by taking user input in command prompt while running npm install.

Is it possible to do so.

  • we install the dependencies from the command prompt only. how you insall the dependencies? can you please explain more! – Ravi Singh Feb 20 '20 at 06:36
  • @RaviSingh I need to download some dependency conditionally. Like we have "Dependency A" and "Dependency B", I need to ask the user a question on CLI and accordingly dependency will get downloaded. – Ashish Kanojia Feb 25 '20 at 08:38
  • @AshishKanojia, did my answer help you or do you need an example script? – Kaspar Etter Feb 27 '20 at 11:20

1 Answers1

0

As you haven't provided much information about what exactly you try to accomplish, it's difficult to advise you or give a concrete example. Anyhow, in package.json, you can override the default behavior of npm scripts with the scripts field:

"scripts": {
    "install": "./scripts/install.sh",
}

As someone else has noted before, you cannot call npm install in your custom script, though, as this would lead to infinite recursion. Thus, rather provide a preinstall script, which npm runs before install.

For use cases other than adding dependencies based on user input, I recommend to check how to set the environment variables and configurations, which the user then could override with npm config set project-name:config-name config-value.

Kaspar Etter
  • 3,155
  • 1
  • 14
  • 21