How to create new app projects without downloading and installing npm modules again and again?

1.8k Views Asked by At

I've installed most of the NPM modules globally with -g

However, I see that whenever I create a new app project (example: angular project) using Yeoman, I see that the npm modules get downloaded again and get installed in the local node_modules folder.

I consider that this is extremely wasteful that the same modules gets downloaded and copied for each new project. The size of each new project is then around 160MB.

Is there a way to download only the modules locally in the new project that are not already available in the global npm node_modules folder? Say, anyway to automatically create symbolic links from the local node_modules folder to the globally installed npm modules?

Is this possible? How to set it up?

2

There are 2 best solutions below

0
On

There are workarounds, but this is intentional to avoid equivalent to DLL hell or specifically library versioning mismatch meltdown. Besides 160 MB is a tiny price to pay for this luxury. Discrete functioning bundles are much nicer to ship as a unit of goodness - trust me

1
On

This is actually by design. From the Node.js blog:

In general, the rule of thumb is:

1.If you’re installing something that you want to use in your program, using require('whatever'), then install it locally, at the root of your project.

2.If you’re installing something that you want to use in your shell, on the command line or something, install it globally, so that its binaries end up in your PATH environment variable.

it is important for you to install project dependencies locally, as you have no guarantee that what is installed globally on your machine may be available on the deployment machine. True, you may manage both machines, or they may in fact be the same machine, but that case is never guaranteed.

Also, it's not unusual for you to have projects which rely upon a specific version of an npm which you may have installed an update for globally, breaking the project that needed the older version. Isolation is a key to keeping projects functional.

if you REALLY need to only maintain one copy, you can use symlinks. npm link globalnpm

Install it globally, and then npm link coffee-script or npm link express (if you’re on a platform that supports symbolic links.) Then you only need to update the global copy to update all the symlinks as well.

Note that the symlink option is really only relevant for hand initialized projects; generators such as Yeoman will always use local, as they follow the grain.