为什么我们需要在全球和本地安装gulp? [英] Why do we need to install gulp globally and locally?

查看:131
本文介绍了为什么我们需要在全球和本地安装gulp?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关 g 的两本手册说,我需要首先在全球安装gulp(使用-g标志),然后再添加一个本地时间。为什么需要它?

解决方案

在全球安装工具时,用户可以将其用作任何地方的命令行工具,包括节点项目之外。全局安装的节点项目很糟糕,因为它们会使部署更加困难。



npm 5.2 +



npx 实用程序捆绑在 npm 5.2 解决了这个问题。有了它,您可以调用本地安装的实用程序,例如全局安装的实用程序(但您必须以 npx 开头执行命令)。例如,如果您想调用本地安装的 eslint ,您可以执行以下操作:

  npx eslint。 



npm < 5.2



当在package.json的脚本字段中使用时, npm code>搜索 node_modules 作为工具以及全局安装的模块,所以本地安装就足够了。



<所以,如果你满意(在你的package.json中):

 devDependencies:{
gulp:3.5.2
}
脚本:{
test:gulp test
}

等。并运行 npm run test ,那么您根本不需要全局安装。



这两种方法都很有用因为不需要 sudo ,所以可以让你的项目成为你的项目。这也意味着,当版本在package.json中碰撞时, gulp 会被更新,所以每个人在开发项目时都会使用相同版本的gulp。 p>

附录:



看起来gulp在全球使用时有一些不寻常的行为。当用作全局安装时,gulp会寻找一个本地安装的gulp来传递控制权。因此,全球安装需要一个吞咽本地安装才能工作。上面的答案仍然存在。本地安装总是优于全局安装。


2 manuals about gulp say that I need to install gulp first globally (with -g flag) and then one more time locally. Why do I need this?

解决方案

When installing a tool globally it's to be used by a user as a command line utility anywhere, including outside of node projects. Global installs for a node project are bad because they make deployment more difficult.

npm 5.2+

The npx utility bundled with npm 5.2 solves this problem. With it you can invoke locally installed utilities like globally installed utilities (but you must begin the command with npx). For example, if you want to invoke a locally installed eslint, you can do:

npx eslint .

npm < 5.2

When used in a script field of your package.json, npm searches node_modules for the tool as well as globally installed modules, so the local install is sufficient.

So, if you are happy with (in your package.json):

"devDependencies": {
    "gulp": "3.5.2"
}
"scripts": {
    "test": "gulp test"
}

etc. and running with npm run test then you shouldn't need the global install at all.

Both methods are useful for getting people set up with your project since sudo isn't needed. It also means that gulp will be updated when the version is bumped in the package.json, so everyone will be using the same version of gulp when developing with your project.

Addendum:

It appears that gulp has some unusual behaviour when used globally. When used as a global install, gulp looks for a locally installed gulp to pass control to. Therefore a gulp global install requires a gulp local install to work. The answer above still stands though. Local installs are always preferable to global installs.

这篇关于为什么我们需要在全球和本地安装gulp?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆