Search code examples
node.jsnpmyarnpkgdependency-management

Installing different versions of the same package with npm/yarn


I have two versions of a package e.g.

@mycompany/mylob v2.0.0

and

@mycompany/mylib v3.0.0

version 3.0.0 has breaking changes from version 2.0.0 but we do not have the capacity to upgrade everything to 3.0.0 as of yet.

Another developer has upgraded an internal package e.g. @mycompany/utils to use version 3.0.0 and that is installed into our codebase so now we are getting compatibility errors when running the build as @mycompany/utils requires version 3.0.0 but the rest of the code in that repository wants version 2.0.0.

Is there a way with yarn/npm that I can install @mycompany/mylib v3.0.0 for @mycompany/utils and have the rest of the code refer to v2.0.0?


Solution

  • You can use custom alias installs:

    npm i custom-name:@mycompany/[email protected]

    You can change custom-name to any valid package name you want to use.

    After that you can import the package with this alias name. e.g.:

    require("custom-name")/ import * from "custom-name"