9

I have a list of websites like this:

project1/site
project2/site
project4/site
...
projectN/site

Each site folder has inside node_modules folder, gruntfile.js file and a package.json file. So they look like this:

project1/site/gruntfile.js
project1/site/package.json
project1/site/node_modules/
...
projectN/site/gruntfile.js
projectN/site/package.json
projectN/site/node_modules/

Everything is working fine with my tasks, plugins and stuff.

I am wondering if I can work this out with just one node_modules folder on my root folder as my websites are using similar tasks.

project1/site/
project2/site/
...
projectN/site/
/node_modules/

Is this even possible? What do I need to do?

Miguel Garrido
  • 1,121
  • 11
  • 23
  • `require` automatically goes up directories until it finds a `node_modules` dir containing the module you want, so that would work. What's your plan for the `package.json` files? Do you still want them separate, or would you combine them? – Aaron Dufour Jan 28 '15 at 04:35
  • As @AaronDufour said, I would recommend either a parent "project" directory where you store everything. Unfortunately, there is no other way to share Node module dependencies across projects without using the global flag, which is [highly discouraged for project dependencies](http://blog.nodejs.org/2011/03/23/npm-1-0-global-vs-local-installation). – Jordan Kasper Jan 28 '15 at 13:56
  • There's no special plan for my `package.son` file. I am just using slightly different tasks on my `gruntfile.js` for each projects (Eg: One projects uses Sass, other uses Less and so on). This is because some projects are either old or other people have been working on them. But overall they share similar tasks. – Miguel Garrido Jan 28 '15 at 20:44

1 Answers1

2

You can use the -g flag with npm to install modules globally, as in: npm install -g <modulename> This will install all the modules so they are accessible to all your node projects.

And then you can create a link for each of the projects to the required modules using npm link, as explained in the link @jakerella referred to:

...there are some cases where you want to do both. Coffee-script and Express both are good examples of apps that have a command line interface, as well as a library. In those cases, you can do one of the following:

  1. 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.

See the npm documentation.

Community
  • 1
  • 1
Ozk
  • 181
  • 11
  • 3
    That would not be advised as then you have no way to tie the installed modules to the project. The `package.json` file cannot be used in combination with the global flag (`-g`). From the [npm blog](http://blog.nodejs.org/2011/03/23/npm-1-0-global-vs-local-installation): "Just like how global variables are kind of gross, but also necessary in some cases, global packages are important, but best avoided if not needed." – Jordan Kasper Jan 28 '15 at 13:54
  • @jakerella, the same npm blog page refers to a potential solution for the OP - to use `npm link` - updated my answer accordingly. Considering the requirement of the OP, this may not be beautiful, but definitely a working solution, even if you do not agree with it... – Ozk Jan 28 '15 at 22:24
  • 1
    Removed the -1 for the extra explanation, although I still don't agree with global installation as the solution. Thanks for taking the time to update your answer! – Jordan Kasper Jan 28 '15 at 23:02