Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use two different versions of a npm module in single node.js project?

The question is pretty self explanatory. In a single project, I have multiple instances of express app. I want to create a new API that will use express v3 instead of v2 that I currently use across my project.

I have a single package.json file. Is it possible to have npm download two different versions of express ?

Thanks.

like image 823
Michael Avatar asked Feb 05 '14 01:02

Michael


People also ask

Can I have multiple versions of npm installed?

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 switch between npm versions?

You can downgrade the npm version by specifying a version in the related commands. If you want to downgrade npm to a specific version, you can use the following command: npm install -g npm@[version.

How do I use different node versions?

Switching among Node. 7; we can simply run either nvm use 12.22. 7 or nvm use 16.13. 0 to easily switch into either version we need. Note that since we only have one version that begins with 12, 14, or 16, we can switch versions with a simple nvm use 16 , nvm use 14 , or nvm use 12 command.


1 Answers

Technically, the answer to your question is no, given your constraints. npm is going to install the dependencies for a given package.json file in a single node_modules directory and thus the filesystem itself will prevent two versions of the same package existing within the same directory. However, there are plenty of workarounds. You could create a tiny shim module that simply depends on express v3 and exposes the express v3 module, then use that in the part of your application that needs v3.

You could also do what the node community culture suggests and split your application into small parts. This is the clear trend in node - smaller modules are better. Express also makes this pretty easy since it supports mounting a child application within a parent application.

You can also play some tricks with npm link and symlinks and what to make it convenient to work on code from two different modules at once.

like image 149
Peter Lyons Avatar answered Oct 19 '22 15:10

Peter Lyons