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
Main directory should look like this
just open the file
Project 1/.angular-cli.json
change the schema
to
and don't forget to create
node_modules
empty folder inside your project directoryI 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 yournode_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 usednpm 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.
You absolutely can share a node_modules directory amongst projects.
From node's documentation:
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.Try pnpm instead of npm.
Install with:
To update your existing installations (and sub-directories) use:
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.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
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