27

I am new to nodejs and gulp stuff. I working on a nodejs project in which I have to run jslint on all the files. I am using gulp for this purpose.

My problem is that In order to run gulp on cli I don't want to install gulp globally and also does not want to update my path variable, So I have installed gulp and other node modules in my project locally using the package.json file

cd myproject

npm install

Since I don't want to install gulp globally and want to run the local gulp I have added script in my package.json file like this as given in this question

{
  "name": "",
  "version": "1.0.0",
  "main": "index.js",
  "private": true,
  "dependencies": {
    "async": "1.5.0"
  },
  "devDependencies": {
    "gulp": "^3.9.0",
    "gulp-jslint": "^0.2.2"
  },
  "scripts": {
      "gulp": "./node_modules/.bin/gulp"   // is this correct?
    }
}

Add added a gulpfile.js inside my myproject folder

var gulp = require('gulp'); 

// include plug-ins
var jslint = require('gulp-jslint');

// JS hint task
gulp.task('lint', function() {
  gulp.src('./common/srp/*.js')
    .pipe(jslint())
    .pipe(jslint.reporter('default'));
});

gulp.task("default", ["lint"]);

But now on my command line inside myproject folder, when I run gulp and gulp lint I get an error

user1-VirtualBox:~/myproject$ gulp lint

/usr/local/node-v0.10.26-linux-x64/bin/gulp No such file or directory

Its looking for gulp in the global node module.

Is there a way to make gulp run on cli without installing globally and updating PATH variable.

Any help will be appreciated Thanks

Community
  • 1
  • 1
user2378417
  • 283
  • 1
  • 3
  • 5

11 Answers11

38

You can find any executable installed by npm in node_modules/.bin. So you can run gulp locally using:

./node_modules/.bin/gulp

You can find more information at no command 'gulp' found - after installation

Community
  • 1
  • 1
snorberhuis
  • 3,108
  • 2
  • 22
  • 29
14

With your code you should be able to run command npm run gulp

Please try

One way to define script is

"scripts": {
      "gulp": "gulp"
    }

If in case you are not able to run gulp command in your project, run

npm link gulp

It will link your global install gulp with your local project. Then try

 gulp -v

If it is showing you the version then you are done. Now you can run any gulp command as you want.

Rahul Jha
  • 874
  • 1
  • 11
  • 28
5

Scripts defined in package.json are accessed through NPM, i.e. npm run-script gulp. I imagine you're trying to run plain old gulp, which should fail since you didn't install it globally.

The scripts section won't automatically create an alias, which I think is your mistake here. You could define one yourself or create a simple bash script if you don't want to type it every time.

thewildandy
  • 318
  • 2
  • 8
3

Try:

path_to_node path_to_gulp_js gulp_task

Example:

node\node.exe node_modules\gulp\bin\gulp.js build
iTake
  • 4,082
  • 3
  • 33
  • 26
2

Like @snorberhuis said. The only way for me to get gulp to work globally was to call gulp manually I am building in a Jenkins environment

Execute Windows Batch Command

cd your-app
npm install gulp

Execute Windows Batch Command

cd your-app\node_modules\.bin 
gulp
Daniel Orlan
  • 83
  • 1
  • 8
1

Just another alternative that will work locally but will give you global like feeling. Add to your shell config i.e. ~/.bash_profile the following export PATH=$PATH:./node_modules/.bin you have to source that file, execute rehash or just open a new shell and then gulp (and any other script inside that folder) shall be available as a global command.

Desislav Kamenov
  • 1,193
  • 6
  • 13
1

The way I did this after bashing my head every possible place is simply going to your Application and install npm dependencies like this:

1- E:\webra-jenkins\Code\trunk\WebRa.Web>npm install

Once npm installed then go this directory

2- [%Application_path%]\node_modules\.bin

And execute the gulp and give your file/task, like this:

3-[%Application_path%]\node_modules\.bin>gulp gulpfile --tasks

In my case as I saw the following lines... I got the inner happiness

18:06:36] Working directory changed to [%Application_path%]
[18:06:37] Tasks for [%Application_path%]\gulpfile.js

Now you can run your tasks 1 by one.

[%Application_path%]\node_modules\.bin>gulp pack-vendor-js
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
0

Check in your project node_modules/.bin folder and make sure gulp is in there. I had a case where it wasn't there that I never tracked down the reason for. If it isn't there, try re-installing gulp locally and see if it shows up. If that doesn't work and you get tired of the problem, the gulp-cli package will fix it for sure, but that shouldn't be something you have to do.

0

The simplest solution I know of is to use npm bin:

`npm bin`/gulp ...

This keeps you away from hard-coding any paths.

Valeriu Paloş
  • 3,459
  • 1
  • 20
  • 12
0

Nothing was working for me. I followed all instructions from everyone. No matter what I did I could not run the Gulp commands.

To fix this I opened the Node.js command prompt that comes installed automatically when you download and run node.js.

Once I was in this command prompt I could run the following commands:

npm install -g gulp
gulp -v

This is probably a matter of common knowledge but as someone starting out no one suggested to run the node.js command prompt and install gulp from there. Everything I read talked about regular powershell or command prompts with elevated permissions.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Jfranz
  • 1
  • 2
  • Please provide additional details in your answer. As it's currently written, it's hard to understand your solution. – Community Aug 26 '21 at 18:24
-5

Globally install gulp in C:\Users\%USERNAME% using this command

npm install –g gulp

You can install any other gulp methods you need to use.. Ex:

npm install -g gulp-concat
npm install -g gulp-uglify
npm install -g gulp-replace

Then at the directory you wish to use GULP. Open the command prompt (Shift + RightClick) then install locally and you'll be able to execute gulp.

npm install gulp

You can install any other gulp methods you need to use.. Ex:

npm install gulp-concat
npm install gulp-uglify
npm install gulp-replace
Mike G
  • 4,232
  • 9
  • 40
  • 66