使用 gulp 而不使用全局 gulp//edit: 并且不链接到 bin js 文件

2022-01-12 00:00:00 node.js javascript npm gulp

我有一个 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 gulpnpm 还提供了将额外参数传递给命令的能力.这仅适用于 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/gulpnode_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']);

相关文章