So in fact, this is a fairly common scenario, which has been considered several times.
There is a closed problem for npm and an open problem for yarn package managers.
The first solution was proposed by NPM author in this GH comment:
Publish a separate package under a different name. This will require a specific version inside.
{ "name": "express3", "version": "1.0.0", "description":"Express version 3", "dependencies": { "express":"3" } } // index.js module.exports = require('express')
In your case, you will publish my-sdk-v1 and my-sdk-v2 . And now you can easily install 2 versions of the package in one project without encountering conflicts.
const mySDKLegacy = require('my-sdk-v1'); const mySDKModern = require('my-sdk-v2');
The second way, for the most part similar to the one suggested, is to use git url:
{ "my-sdk-v1": "git://github.com/user/my-sdk#1.0.0", "my-sdk-v2": "git://github.com/user/my-sdk#2.0.0" }
Unlike the npm package, you can choose any name! The source of truth is git url.
Later npm-install-version popped up. Buuut, as you have already proven, its use is a bit limited. Since it spawns a child process to execute some commands and write to tmp directories. Not the most reliable way for the CLI.
To summarize: you still have options 1 and 2. I would dwell on the first one, since the name and tags of the github repository can change.
The second option with git url is better if you want to change the version so that it depends more often. Imagine that you want to publish a security patch for my-sdk-v1 legacy. It will be easier to link to the git url, then publish my-sdk-v1.1 to npm again and again.
Vlad Holubiev
source share