John Sanabria
John Sanabria

Reputation: 37

Centralized shared gulp dependencies for multiple projects?

Is it possible to share gulp dependencies for multiple projects instead of having them in every project folder (as it is for default)?

This is what I'm aiming for (every client project would have the same structure):

/shared
node_modules
shared_scss
shared_js
gulpfile.js

/client-project-1
client_scss
client_js
gulpfile.js

/client-project-2
/client-project-3
/client-project-n

Upvotes: 0

Views: 344

Answers (1)

eush77
eush77

Reputation: 4088

Sharing dependencies in Node.js is easy: given a module id that requires a lookup, the require.resolve algorithm searches in node_modules directories and walks up the directory tree doing that until module is resolved:

If the module identifier passed to require() is not a native module, and does not begin with '/', '../', or './', then Node.js 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.

Back to your question, you can basically put your projects into one directory with a shared node_modules subdirectory and every require call from any of your client project directories will load the dependency from that shared node_modules (if it exists and not overridden in the client directory).

shared
├── node_modules
│   └── ...
├── shared_js
├── shared_scss
├── gulpfile.js
|
├── client-project-1
│   ├── client_js
│   ├── client_scss
│   └── gulpfile.js
|
├── client-project-2
├── client-project-3
└── client-project-n

If you also symlink shared_js into shared/node_modules/shared_js, client projects will be able to require them with require('shared_js/...').

Alternatively, you can install shared dependencies globally into $HOME/.node_modules or $NODE_PATH, but this is discouraged.

Upvotes: 1

Related Questions