問題描述
2 個關于 gulp 的手冊說我需要先全局安裝 gulp(使用 -g 標志),然后再安裝一個當地時間.為什么我需要這個?
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.
npx
與 npm
5.2
捆綁在一起的實用程序解決了這個問題.使用它,您可以調用本地安裝的實用程序,如全局安裝的實用程序(但您必須以 npx
開頭命令).例如,如果你想調用一個本地安裝的 eslint
,你可以這樣做:
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
當在 package.json 的 script
字段中使用時,npm
會在 node_modules
中搜索工具以及全局安裝的模塊,因此本地安裝就足夠了.
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.
所以,如果您對(在您的 package.json 中)感到滿意:
So, if you are happy with (in your package.json):
"devDependencies": {
"gulp": "3.5.2"
}
"scripts": {
"test": "gulp test"
}
等等.并使用 npm run test
運行,那么您根本不需要全局安裝.
etc. and running with npm run test
then you shouldn't need the global install at all.
這兩種方法對于讓人們設置您的項目很有用,因為不需要 sudo
.這也意味著 gulp
將在 package.json 中的版本發生碰撞時更新,因此每個人在使用您的項目開發時都將使用相同版本的 gulp.
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.
gulp 在全局使用時似乎有一些不尋常的行為.當用作全局安裝時,gulp 會查找本地安裝的 gulp 以傳遞控制權.因此,gulp 全局安裝需要 gulp 本地安裝才能工作.上面的答案仍然有效.本地安裝總是優于全局安裝.
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?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!