5

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.

bigskull
  • 541
  • 2
  • 18
  • 28

4 Answers4

2

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.

pentool
  • 565
  • 1
  • 5
  • 13
0

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.

Richard Medeiros
  • 938
  • 9
  • 18
0

-g Stands for global Installation, i.e. 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.

Himanshu Mittal
  • 584
  • 1
  • 6
  • 21
  • 1
    I should share node_modules folder to avoid to have this folder in every project. If i put the same package.json in various project and run npm install will I have the same node_modules in every project? – bigskull Sep 28 '17 at 14:02
  • 1
    If you Run npm install in every project, then yes, identical node_modules in every project. However, there shall be a separate node_modules for every project, as if you start testing new things, you will have to maintain separate package.json for every project. And it is the ethical way – Himanshu Mittal Sep 28 '17 at 14:21
  • 1
    So the better way is to keep a package.json for each project and launch npm install in each folder, even if this will produce same node_modules folder. Is this right? – bigskull Sep 28 '17 at 14:45
  • Yes, right and ethical. And whenever you install any package, npm kind of saves a reference of it for itself; So you don't need internet for installing packages for every project, if you have net problems ;) – Himanshu Mittal Sep 28 '17 at 15:07
0

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.

jimscafe
  • 1,081
  • 2
  • 14
  • 24