問題描述
我有一個 gulpfile.js,當在命令行中輸入 gulp
時,它可以完美運行.
I have a gulpfile.js that runs through perfectly when typing gulp
into the commandline.
gulp
bash 命令真正做的就是調用 package.json >> 中指定的 js 文件.斌>>全局安裝的 gulp 的 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 的情況下運行 gulpfile,只需鍵入 node gulpfile.js
顯然失敗并且已經經常提到,盡管 gulp 是在本地安裝并且在開始時需要gulpfile.js
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
在沒有 cli 工具的情況下使用 gulp 可以很容易地將 gulp 用作其他 npm 插件的一部分.
Using gulp without the cli tool would make it possible to use gulp as part of other npm plugins very easily.
注意:
當原始 gulpfile.js 已通過 gulp cli 工具啟動時,需要另一個 gulpfile.js 從原始 gulpfile.js 工作.
Note:
Requiring another gulpfile.js works from an original gulpfile.js when this original gulpfile.js has been started via the gulp cli tool.
問題:
在不需要全局 cli gulp 工具(//或在本地鏈接到它)的情況下運行/需要 gulp 的最佳方式是什么?例如當 gulp 只是一個本地安裝的依賴項時,能夠簡單地從另一個 js 文件中要求它.(換句話說,從 JS 內部以編程方式啟動 gulp,無需任何 CLI 干預)
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)
推薦答案
在package.json中
In package.json
"scripts": {
"gulp": "gulp"
},
然后這個命令 npm run gulp
npm 還提供了將額外參數傳遞給命令的能力.這僅適用于 npm >= 2.0
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
更新:沒有 bin 鏈接
Update: Without bin link
您可以查看 node_modules/.bin/gulp
或 node_modules/gulp/bin/gulp.js
文件以了解如何啟動 gulp(第 129 行很有趣)
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)
我認為這應該可行:
var gulp = require('gulp');
gulp.task('default', function() {
console.log('do something');
});
gulp.start.apply(gulp, ['default']);
這篇關于使用 gulp 而不使用全局 gulp//edit: 并且不鏈接到 bin js 文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!