简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM