Rahul Desai
Rahul Desai

Reputation: 15501

How to dynamically install a particular version of a NPM package based on local Node.js version?

I did Google about this but no luck.

Basically, I need a particular version of a NPM package (A) to be installed if the local Node.js version is X, if not then install version B of that NPM package. This needs to be a part of build process, so its all dynamic.

If there was a way to have this config in package.json, then it would have been a straightforward solution for me.

How do I achieve this?

Upvotes: 0

Views: 1222

Answers (1)

Ethan Kawkji
Ethan Kawkji

Reputation: 227

You can achieve this by using Yarn as your dependency management tool in combination with the Selective Versions Resolutions feature.

More specifically in your case you'd use it as described in the "Mapping version specifications" format, based on that your package.json you would include something like the following, assuming that version X=1.0.3 and version B=2.0.0 in the following example:

"devDependencies": {
  "a": "1.0.3"
},
"resolutions": {
  "a@==1.0.3": "[email protected]"
}

Upvotes: 1

Related Questions