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)
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 withnpm run gulp
by adding this to
package.json
In your package.json add the following:
and run from command line by typing npm run gulp.
This removes the dependency on having gulp globally installed
I think the most direct answer is the following:
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.
Generally speaking Sander Visser's answer is probably the "best", but this is the lowest level call that can be implemented anywhere.
In package.json
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.0Update: Without bin link
You can check the
node_modules/.bin/gulp
ornode_modules/gulp/bin/gulp.js
file to see how you can start gulp (Line 129 is interesting)I think this should work:
Meanwhile since npm >= 5.2.0. you can do it with 'npx':
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