curveball
curveball

Reputation: 4505

Installing gulp plugins locally vs globally

I am pretty new to Gulp and I've got a question: should I install gulp plugins (such as gulp-sass or gulp-imagemin) locally or globally? In examples around the web people mostly do it locally (with --save-dev option). As I understand, by doing so the modules are stored in the local node_modules folder, added in the local package.json as devDependencies and can be referenced to in the local gulpfile.js via require(). So, if I need to install the same modules for another project I work on, it may be accomplished by copying package.json to a new project's folder and typing in npm install in a command line tool (after getting to the project's folder). Fine.

But what if I, as a regular gulp user, don't have plans to upload and share my stuff on the npm space and not interested in maintaining devDependencies, can I in this case just install gulp plugins globally like npm install -g gulp-sass? Will they be found through a global path in my system? Is it an option on the whole, if I don't want to bother myself to copy package.json, run npm install every time I create a new project or have multiple copies of the same modules scattered around on my disk?

Upvotes: 0

Views: 1097

Answers (2)

UnSeulT
UnSeulT

Reputation: 73

A kind of "global install" for the typical case OP explained, could be to have a folder where you put your package.json with every dependencies you need for each of your projects, and run npm install from there. And put here also your gulpfiles for each project (you could named them as you want), like gulpfile-project1.js, conf-project2.js... whatever.

Of course adjust your paths in those files accordingly. And from this directory, run gulp using the -f flag:

gulp -f gulpfile-project1.js

So you end with this kind of scaffolding :

my_dev_directory
  - project1
    - dev
      - main.less
      - main.js
    - www
      - main.css
      - main.js
  - gulp
    - node_modules
    - package.json
    - package-lock.json
    - gulpfile-project1.js

In your gulpfiles, start to declare a const with the base path of your project to ease all path declarations, like:

const path_dev = '../project1/dev';
const path_www = '../project1/www';

This way you have only one node_modules directory installed on your disk and your projects directories contain only public files you want to upload, no dev files.

Of course one of the main drawback is that you share the same module version with all your projects, but it answered to OP's need and even if it's not recommended, not the "proper way to do", you are free to do as you want with your dev tools :)

Upvotes: 0

user3329290
user3329290

Reputation:

For plugins that you are using via gulp, what you have to do is install them locally to the project. Although you have to install gulp itself globally and locally, to run the file and then for the project to pick up the gulp based commands and functions.

The reason you should install the plugins via npm locally is so that it is specific to the project, for example if you then went to upload this project to your server or host on github then you would have to then go and have to globally install all of your packages again. If they are saved, they exist in your packages.json, this is so that when you go to run npm install to install all the packages for said project npm knows what to install.

If I can further clarify anything let me know.

Upvotes: 1

Related Questions