How can I make multiple projects share node_modules directory?

匿名 (未验证) 提交于 2019-12-03 02:59:02

问题:

Whenever I make projects, I have to download all dependencies of node modules. Without copying the node_modules, Is there anyway to share the central node_modules in multiple projects?

like the followings, I have to run many commands every time..

npm install gulp-usemin                                                                         npm install gulp-wrap npm install gulp-connect npm install gulp-watch npm install gulp-minify-css npm install gulp-uglify npm install gulp-concat npm install gulp-less npm install gulp-rename npm install gulp-minify-html 

回答1:

You absolutely can share a node_modules directory amongst projects.

From node's documentation:

If the module identifier passed to require() is not a native module, and does not begin with '/', '../', or './', then node starts at the parent directory of the current module, and adds /node_modules, and attempts to load the module from that location.

If it is not found there, then it moves to the parent directory, and so on, until the root of the file system is reached.

For example, if the file at '/home/ry/projects/foo.js' called require('bar.js'), then node would look in the following locations, in this order:

/home/ry/projects/node_modules/bar.js /home/ry/node_modules/bar.js /home/node_modules/bar.js /node_modules/bar.js

So just put a node_modules folder inside your projects directory and put in whatever modules you want. Just require them like normal. When node doesn't find a node_modules directory in your project folder, it will check the parent folder automatically. So make your directory structure like this:

-myProjects --node_modules --myproject1 ---sub-project --myproject2

So like this, even your sub-project's dependencies can draw on your main node_modules repository.

One drawback to doing it this way is you will have to build out your package.json file manually (unless someone knows a way to automate this with grunt or something). When you install your packages and add the --save arg to an npm install command it automatically appends it to the dependencies section or your package.json, which is convenient.



回答2:

I found a trick, just take a look at the Symbolic Links (symlinks) on Windows or Linux, it is working just like shortcuts but more powerful.

Simply you need to make a Junction for your node_modules folder anywhere you want.

To achieve this you need at least one node_modules real folder then make a Junction to it in the other projects.

On Windows, you can either use the Command Prompt, or use an application. Using the Command Prompt gives you a bit more control, using an application is easier I suggest Link Shell Extension.



回答3:

Main directory should look like this

node_modules Project 1 Project 2 Project 3 Project 4 

just open the file Project 1/.angular-cli.json

change the schema

"$schema": "./node_modules/@angular/cli/lib/config/schema.json", 

to

"$schema": "./../node_modules/@angular/cli/lib/config/schema.json" 

and don't forget to create node_modules empty folder inside your project directory



回答4:

Let's assume that having a single node_modules it should contain all the packages for all applications. thus your apps will also share most of the unique package.json entries (just the name should change)

my idea would be to have a single root and multiple src level as below

root\package.json root\node_modules root\\.. root\app1\src\\.. root\app2\src\\.. 

the only issue you might face would be having a backup of json (or tsconfig) for any app and restore them when you work on it or setup your startup scripts to serve any app



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!