简体   繁体   English

在本地与全局安装 gulp 插件

[英]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?我是 Gulp 的新手,我有一个问题:我应该在本地还是全局安装 gulp 插件(例如 gulp-sass 或 gulp-imagemin)? In examples around the web people mostly do it locally (with --save-dev option).在网络上的示例中,人们大多在本地进行(使用--save-dev选项)。 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() .据我了解,通过这样做,模块存储在本地node_modules文件夹中,作为 devDependencies 添加到本地package.json ,并且可以通过require()在本地 gulpfile.js 中引用。 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).因此,如果我需要为我正在处理的另一个项目安装相同的模块,可以通过将package.json复制到新项目的文件夹并在命令行工具中输入npm install来完成(进入项目文件夹后)。 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 ?但是如果我,作为一个普通的 gulp 用户,没有计划在 npm 空间上传和分享我的东西并且对维护 devDependencies 不感兴趣,在这种情况下我可以像npm install -g gulp-sass这样全局npm install -g gulp-sass插件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?如果我不想打扰自己复制package.json ,每次创建新项目时都运行npm install或者在我的磁盘上散布多个相同模块的副本,这是一个整体的选择吗?

For plugins that you are using via gulp, what you have to do is install them locally to the project.对于通过 gulp 使用的插件,您需要做的是将它们本地安装到项目中。 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.虽然你必须在全局和本地安装 gulp 本身,才能运行文件,然后让项目选择基于 gulp 的命令和函数。

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.您应该在本地通过npm安装插件的原因是它特定于项目,例如,如果您随后将此项目上传到您的服务器或托管在 github 上,那么您将不得不去并且必须全局安装所有你的包裹再次。 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.如果它们被保存,它们就存在于你的packages.json ,这样当你去运行npm install来安装该项目的所有包时,npm 知道要安装什么。

If I can further clarify anything let me know.如果我能进一步澄清任何事情,请告诉我。

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. OP 解释的典型案例的一种“全局安装”可能是有一个文件夹,您可以在其中放置 package.json 以及每个项目所需的每个依赖项,然后从那里运行npm install And put here also your gulpfiles for each project (you could named them as you want), like gulpfile-project1.js, conf-project2.js... whatever.并将每个项目的 gulpfiles 放在这里(您可以根据需要命名它们),例如 gulpfile-project1.js、conf-project2.js ......随便。

Of course adjust your paths in those files accordingly.当然,相应地调整您在这些文件中的路径。 And from this directory, run gulp using the -f flag:从这个目录,使用 -f 标志运行 gulp:

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:在您的 gulpfiles 中,开始使用项目的基本路径声明一个 const 以简化所有路径声明,例如:

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.这样你的磁盘上只安装了一个 node_modules 目录,你的项目目录只包含你想要上传的公共文件,没有开发文件。

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 :)当然,主要缺点之一是您与所有项目共享相同的模块版本,但它满足了 OP 的需求,即使不推荐这样做,也不是“正确的做法”,您可以随心所欲使用您的开发工具:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM