Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

npm including same dependency twice with different versions

Is it possible in package.json to include the same dependency twice, but with different versions:

"dependencies": {
    "projectX-v1.0.0": "ssh://[email protected]/xxx/projectX.git#v1.0.0",
    "projectX-v1.0.1": "ssh://[email protected]/xxx/projectX.git#v1.0.1"
}

The best I could come up with is to change the name (see above) of the included projectX to projectX-v1.0.0 and projectX-v1.0.1 in its releases/tags and package.json in order to end up with two separate folders in node_modules folder.

The reason why I need this is the following omni-repository structure:

/omni/
/omni/frontend
/omni/frontend/projectA
/omni/frontend/projectB
...
/omni/package.json

projectA would use projectX-v1.0.0 and projectB would use projectX-v1.0.1 (bundling with browserify)

Thanks!

like image 462
Emir Pasic Avatar asked Jul 11 '16 07:07

Emir Pasic


People also ask

Does npm install multiple versions of same package?

With npm or yarn, you can install a package under a custom alias. This enables you to install multiple versions of a package in the same project.

Can I have multiple versions of npm?

The good news is that you can have it done directly via NPM! What can be done in this case is an idea called "package alias". So you can have multiple versions of the same package running in your app and you can work on the upgrades mitigating possible issues.

How do I install two versions of the same package?

In order to start, let's first create a folder called multiple-package-versions-in-project and, inside of this folder, initialize a node project by running npm init -y . After that, we can install the old and latest versions of the package.

What happens when you run npm install twice?

Running npm install apart from the first time installs only the missing packages and those that need update. Save this answer. Show activity on this post. Running npm install after the first time only installs missing packages or updates existing ones.

What are bundled dependencies npm?

"bundledDependencies" are exactly what their name implies. Dependencies that should be inside your project. So the functionality is basically the same as normal dependencies. They will also be packed when running npm pack .


2 Answers

Not with vanilla npm, although the community has talked about it extensively. That said, Scott Hardy's npm-install-version package will let you install node modules to versioned, or custom, directories.

like image 186
Bill Brower Avatar answered Nov 14 '22 22:11

Bill Brower


NPM has said that they don't plan to implement this feature, but I've found a way around it. I forked the package repository on github and renamed it. Then I installed this new repo as a dependency in my project using:

npm i -S git+ssh://[email protected]:<org>/<renamed-project>.git

Now, I can upgrade the actual npm package and change all of the references to it in my project to the new, renamed-project git repo dependency. It's a lot of extra work, but I found it easier than switching to yarn or another package manager with package aliasing.

like image 24
aidangarza Avatar answered Nov 14 '22 23:11

aidangarza