dzek's mini blog

How to install multiple versions of an npm library

2025-12-13, in:

Yesterday I described how to globally install multiple versions of an npm library.

Today I will show you how to do the same, but in a local project. It’s a small trick that might come in handy when you need to test something against multiple versions of a library or gradually migrate from one version to another.

All you need to do is this:

{
  "dependencies": {
    "jquery": "*",
    "jquery-2": "npm:jquery@^2",
    "jquery-old": "npm:jquery@^1.0.0`"
  }
}

You will have the current version of jQuery available as jquery, version 2.x as jquery-2 and version 1.x as jquery-old.

import $ from 'jquery';
import $two from 'jquery-2';
import $old from 'jquery-old';

Bonus

This trick also allows you to change the name of the package when installing it. Imagine a fork of an anbandoned library on NPM you want to use instead of the original one, but for some reason you still need to import it using original name.

You can achieve it with this:

{
  "dependencies": {
    "jquery": "npm:jquery-fork-with-a-fix@^3.0.0"
  }
}