Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using gulp without global gulp //edit: and without linking to the bin js file

I have a gulpfile.js that runs through perfectly when typing gulp into the commandline.

All the gulp bash command really does is calling the specified js file in package.json >> bin >> gulp of the globally installed 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

Using gulp without the cli tool would make it possible to use gulp as part of other npm plugins very easily.

Note:
Requiring another gulpfile.js works from an original gulpfile.js when this original gulpfile.js has been started via the gulp cli tool.

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)

like image 751
philkunz Avatar asked Oct 08 '15 14:10

philkunz


People also ask

How do I install gulp locally and globally?

To install Gulp locally, navigate to your project directory and run npm install gulp . You can save it to your package. json dependencies by running npm install gulp --save-dev . Once you have Gulp installed locally, you can then proceed to create your gulpfile.

What is gulp JS for?

Gulp is a task runner that uses Node. js as a platform. Gulp purely uses the JavaScript code and helps to run front-end tasks and large-scale web applications. It builds system automated tasks like CSS and HTML minification, concatenating library files, and compiling the SASS files.

How do I run Gulpfile in Visual Studio?

Run a Gulp Task in Visual Studio CodeType "Run Task" and select it, which will bring up a list of tasks configured in Gulp. Choose the Gulp Task you want to run! Most of your Gulp Tasks will probably be automated using gulp watch, but manual Gulp Tasks can easily be run within Visual Studio Code.


3 Answers

In 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

Update: Without bin link

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)

I think this should work:

var gulp = require('gulp');

gulp.task('default', function() {
    console.log('do something');
});

gulp.start.apply(gulp, ['default']);
like image 138
Sander Visser Avatar answered Oct 06 '22 17:10

Sander Visser


When you install it locally (npm install --save-dev gulp) you can just run it by calling ./node_modules/.bin/gulp all

Or you can define an npm script, and run it with npm run gulp

by adding this to package.json

"scripts": {
  "gulp": "gulp"
}
like image 22
t.niese Avatar answered Oct 06 '22 17:10

t.niese


Meanwhile since npm >= 5.2.0. you can do it with 'npx':

npx gulp

It executes the binaries in ./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

like image 8
tweini Avatar answered Oct 06 '22 17:10

tweini