javascript 在没有全局 gulp 的情况下使用 gulp //edit: 并且没有链接到 bin js 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33018779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using gulp without global gulp //edit: and without linking to the bin js file
提问by philkunz
I have a gulpfile.js that runs through perfectly when typing gulp
into the commandline.
我有一个 gulpfile.js,在输入gulp
命令行时可以完美运行。
All the gulp
bash command really does is calling the specified js file in package.json >> bin >> gulp
of the globally installed gulp.
所有gulp
bash命令确实被要求在指定的js文件package.json >> bin >> gulp
的全球的装机量一饮而尽的。
Now I want to run the gulpfile without the globally installed gulp by simply typing node gulpfile.js
which fails obviously and already has been mentioned quite often, despite gulp being installed locally and required at the beginning of the gulpfile.js
现在我想在没有全局安装的 gulp 的情况下运行 gulpfile,只需键入node gulpfile.js
这显然失败并且已经经常提到的,尽管 gulp 是在本地安装的并且在开始时需要gulpfile.js
Using gulp without the cli tool would make it possible to use gulp as part of other npm plugins very easily.
在没有 cli 工具的情况下使用 gulp 可以很容易地将 gulp 用作其他 npm 插件的一部分。
Note:
Requiring another gulpfile.js works from an original gulpfile.js when this original gulpfile.js has been started via the gulp cli tool.
注意:
当原始 gulpfile.js 已通过 gulp cli 工具启动时,需要另一个 gulpfile.js 从原始 gulpfile.js 工作。
Question:
What is the best way of running/requiring gulp without the need for the global cli gulp tool (//edit: or linking to it locally)? e.g. being able to simply require it from another js file when gulp is only a locally installed dependency. (in other words starting gulp programmatically from inside JS without CLI intervention of any kind)
问题:
在不需要全局 cli gulp 工具(//edit: 或在本地链接到它)的情况下运行/需要 gulp 的最佳方法是什么?例如,当 gulp 只是本地安装的依赖项时,能够简单地从另一个 js 文件中要求它。(换句话说,从 JS 内部以编程方式启动 gulp,无需任何类型的 CLI 干预)
回答by Sander Visser
In package.json
在 package.json 中
"scripts": {
"gulp": "gulp"
},
And then this command npm run gulp
Also npm provides the ability to pass extra parameters to your commands.
This is only the case for npm >= 2.0
然后这个命令npm run gulp
npm 也提供了向你的命令传递额外参数的能力。这仅适用于 npm >= 2.0
Update: Without bin link
更新:没有 bin 链接
You can check the node_modules/.bin/gulp
or node_modules/gulp/bin/gulp.js
file to see how you can start gulp (Line 129 is interesting)
您可以检查node_modules/.bin/gulp
或node_modules/gulp/bin/gulp.js
文件以了解如何启动 gulp(第 129 行很有趣)
I think this should work:
我认为这应该有效:
var gulp = require('gulp');
gulp.task('default', function() {
console.log('do something');
});
gulp.start.apply(gulp, ['default']);
回答by t.niese
When you install it locally (npm install --save-dev gulp
) you can just run it by calling ./node_modules/.bin/gulp all
当您在本地安装它 ( npm install --save-dev gulp
) 时,您可以通过调用来运行它./node_modules/.bin/gulp all
Or you can define an npm
script, and run it with npm run gulp
或者你可以定义一个npm
脚本,然后运行它npm run gulp
by adding this to package.json
通过将其添加到 package.json
"scripts": {
"gulp": "gulp"
}
回答by tweini
Meanwhile since npm >= 5.2.0. you can do it with 'npx':
同时,由于 npm >= 5.2.0。你可以用'npx'来做到:
npx gulp
It executes the binaries in ./node_modules/.bin
它执行 ./node_modules/.bin 中的二进制文件
By the way, it has the ability to execute not installed tools which are only temporary installed, executed and then deleted.
顺便说一下,它有能力执行未安装的工具,这些工具只是临时安装、执行然后删除。
Checkout the post from Kat Marchán: Introducing npx: an npm package runner
查看 Kat Marchán 的帖子:Introducingnpx : an npm package runner
回答by Megha Bisht
In your package.json add the following:
在您的 package.json 中添加以下内容:
"scripts": {
"gulp": "node ./node_modules/gulp/bin/gulp.js <task-name>"
}
and run from command line by typing npm run gulp.
并通过键入 npm run gulp 从命令行运行。
This removes the dependency on having gulp globally installed
这消除了对全局安装 gulp 的依赖
回答by Doug Lampe
I think the most direct answer is the following:
我认为最直接的答案如下:
node ./node_modules/gulp/bin/gulp.js
Yes, adding to your npm scripts works, but this doesn't help in situations like automated builds where you are trying to run a gulp task for existing code from a repo and you can't easily change package.json.
是的,添加到您的 npm 脚本是有效的,但这在自动构建等情况下无济于事,在这种情况下,您尝试为存储库中的现有代码运行 gulp 任务,并且您无法轻松更改 package.json。
Generally speaking Sander Visser's answer is probably the "best", but this is the lowest level call that can be implemented anywhere.
一般来说,Sander Visser 的回答可能是“最好的”,但这是可以在任何地方实现的最低级别的调用。
回答by WheretheresaWill
Reiterating the answers above, but including the gulpfile location and a gulp command (in this case build
. On Windows you can use the following:
重申上面的答案,但包括 gulpfile 位置和 gulp 命令(在这种情况下build
。在 Windows 上,您可以使用以下内容:
node node_modules\gulp\bin\gulp.js build --gulpfile "C:\my_project_location\gulpfile.js"
node node_modules\gulp\bin\gulp.js build --gulpfile "C:\my_project_location\gulpfile.js"