Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two versions of same npm package in Node application

I'm working on a CLI tool in NodeJS that uses another NodeJs package that we develop, which is an SDK.

The thing is, we just published a V2 version of that SDK, and we want to offer the CLI user a legacy mode, so they can use either the first or second version of our SDK, like so:

$ cli do-stuff #execute sdk v2 

Or

$ LEGACY_MODE='on' cli do-stuff #execute sdk v1 

My problem is that I did not found any clean way to use two versions of the same dependency in my CLI. I tried to use npm-install-version package. It works well on my local environment, but after publishing my cli and doing npm install -g my-cli, it doesn't work anymore, because it creates a node_modules folder in the current folder, instead of the /usr/local/lib/node_modules/my-cli folder. I also tried multidep, and I have kind of the same issue.

For now, my package.json do not contain at all my sdk, but I would like to have something like :

"dependencies": {   "my-sdk": "2.0.0"   "my-sdk-legacy": "1.0.0" } 

Or

"dependencies": {   "my-sdk": ["2.0.0", "1.0.0"] } 

I haven't found anything else yet. I'm thinking about publishing the first version of my sdk package with another name, like "my-sdk-legacy", but I would like to avoid that if possible.

Any solution for that ?

like image 570
Greg Avatar asked May 03 '17 21:05

Greg


People also ask

Can different versions of the same package be used in the same application?

When we install a package using the npm install package-name command, it will download the current stable version of the package inside node_modules folder and add it to package. json file. To install multiple versions of the same package, we need to use the package alias syntax which is supported from the npm v6.

How do I use two npm versions?

If you need to use a different version of npm for each project, there are a number of possible solutions. Probably the lightest-weight version is to use npx . A semi-common use-case for this can be projects that use lock-file v1 and another that uses lock-file v2. v2 was introduced in npm v7.

Can we have 2 versions of node installed?

As on the same machine, we can only install one version of the nodejs, so it's very painful to uninstall and install the new node version as per your project requirements. To overcome this problem, we can use the Node Version Manager (NVM).

How do I install a different version of a package?

Use npm list [package-name] to know the specific latest version of an installed package. Use npm install [package-name]@[version-number] to install an older version of a package. Prefix a version number with a caret (^) or a tilde (~) to specify to install the latest minor or patch version, respectively.


1 Answers

Based on my answer for a similar question:

As of npm v6.9.0, npm now supports package aliases. It implements the same syntax as Yarn uses:

npm install my-sdk-legacy@npm:my-sdk@1 npm install my-sdk 

This adds the following to package.json:

"dependencies": {   "my-sdk-legacy": "npm:my-sdk@^1.0.0",   "my-sdk": "2.0.0" } 

This seems to me the most elegant solution available, and is compatible with the Yarn solution proposed by @Aivus.

like image 195
Rens Baardman Avatar answered Sep 20 '22 19:09

Rens Baardman