简体   繁体   中英

Share node_modules between different projects

I'm developing various Angular 2 projects and I want to share node_modules folder between multiple projects. I would like to create a structure like this:

MainFolder
- Project1
- Project2
- package.json

so I would have just 1 package.json for all the projects. My answer: is it possible to do this? If possible, I have to lunch npm install with -g ? I can't understand how -g works. Can someone give me instructions how to proceed? Very thanks

I forgot to say that I build the projects with angular-cli.

The way I go around this for small/learning/test projects is with (I call it) "git projects". Basically I manage the various projects via git, and just "load" the project I want to work on. Of course this doesn't work if you want to have access to multiple projects at the same time.

I like to use a git client for this purpose because it's easier to visualize my existing "projects".

So my workflow is this...

  1. Create my main/base folder. This will contain the git repo, the single node_modules folder, and whatever else that should be common to all projects.
  2. I create the basic package.json file (using npm init ). No description, no nothing, just the basic skeleton package.json file. (However, if you know you will use certain packages in ALL of your projects, you can npm install them first, so they will be added to package.json as your "base" modules.)
  3. Now I check the bare package.json into the repo (and anything else that you may want to have in all of your projects, but usually it's just the package.json file). This will be the bare-bones starting branch for all projects.
  4. Once this is checked in, I create a branch off of this in the git repo. This will be "Project 1" - or whatever you want to call it. Then build up your project however you want, installing modules, checking in changes, etc, etc.
  5. When I want to start a new project, I simply check out the first bare-bones project (which is just the empty, or almost empty, package.json file) and do another branch off of it. This will be my 2nd project.

And so forth...

So the main thing is that every new "project" will be a new branch in the git repo, and to create a new project, just switch back to the original bare-bones one and do a new branch off of that.

Of course it is possible to create branches within a project, too. It's all about naming conventions. You could, for example, prefix a new project branch with "P_" or "PROJECT_", etc, so you can quickly tell in your git client which branches are projects. And of course use a different naming scheme if you just need a new branch within an existing project. That's basically how I go about it.

You may not like this workflow, but this way I don't need to install packages globally. When I do a backup, I can simply delete the single (possibly huge) node_modules folder. All project related modules can be reinstalled by simply checking out a branch for a particular project and run " npm install " on its package.json. Hope it makes sense.

Here is documentation on the various npm install arguments

In global mode (ie, with -g or --global appended to the command), it installs the current package context (ie, the current working directory) as a global package.

The -g install locations based on environment can be found here

One way you can achieve what you want is to have one solution for both projects and each project route uses it's own lazy loaded module .

Unless you have a specific business need to share resources, it's better to keep each project separate with it own resources and configuration.

-g Stands for global Installation, ie the packages you install will be available for all applications.

And why do you want to share node_modules and package.json file?

Keep them seperate for each seperate project. And if you need to share your project, you may share your package.json instead of sharing the node_modules folder.

Also to point out, if you manually install packages by listing their names, then you can use -g (global) flag, but if you do use only npm install then your packages won't be installed as global packages.

If it really is just for testing simple applications, could rename tha app folder in some way provide a solution. It assumes that all the dependencies are the same or at least a subset of the dependencies provided.

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