How can I make multiple projects share node_module

2019-01-05 01:49发布

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

标签: node.js npm
6条回答
\"骚年 ilove
2楼-- · 2019-01-05 01:59

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

查看更多
不美不萌又怎样
3楼-- · 2019-01-05 02:02

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. The junction is nothing but a short cut to your original node_modules folder. Create it inside your project folder where the actual node_modules would have been created if used npm install.

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.

查看更多
太酷不给撩
4楼-- · 2019-01-05 02:07

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.

查看更多
ゆ 、 Hurt°
5楼-- · 2019-01-05 02:11

Try pnpm instead of npm.

pnpm uses hard links and symlinks to save one version of a module only ever once on a disk.

Install with:

npm install -g pnpm

To update your existing installations (and sub-directories) use:

pnpm recursive install
查看更多
Bombasti
6楼-- · 2019-01-05 02:15

By looking at some articles it seems that Lerna is a good tool for managing multiple projects inside a single directory (monorepo). It supports modules sharing without duplicating the entire packages in every folder and commands to install them in multiple projects.

查看更多
该账号已被封号
7楼-- · 2019-01-05 02:20

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

查看更多
登录 后发表回答