Locally installed gulp not running in command line

2020-05-25 07:23发布

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

10条回答
该账号已被封号
2楼-- · 2020-05-25 08:05

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.

查看更多
Fickle 薄情
3楼-- · 2020-05-25 08:07

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.

查看更多
聊天终结者
4楼-- · 2020-05-25 08:12

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

查看更多
三岁会撩人
5楼-- · 2020-05-25 08:13

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
查看更多
放我归山
6楼-- · 2020-05-25 08:18

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.

查看更多
看我几分像从前
7楼-- · 2020-05-25 08:19

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

`npm bin`/gulp ...

This keeps you away from hard-coding any paths.

查看更多
登录 后发表回答