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

前端 未结 6 1142
野趣味
野趣味 2021-02-12 18:16

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 sp

相关标签:
6条回答
  • 2021-02-12 18:20

    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']);
    
    0 讨论(0)
  • 2021-02-12 18:29

    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"
    }
    
    0 讨论(0)
  • 2021-02-12 18:32

    Reiterating the answers above, but including the gulpfile location and a gulp command (in this case build. On Windows you can use the following:

    node node_modules\gulp\bin\gulp.js build --gulpfile "C:\my_project_location\gulpfile.js"

    0 讨论(0)
  • 2021-02-12 18:33

    I think the most direct answer is the following:

    node ./node_modules/gulp/bin/gulp.js
    

    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.

    0 讨论(0)
  • 2021-02-12 18:39

    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

    0 讨论(0)
  • 2021-02-12 18:44

    In your package.json add the following:

    "scripts": {
      "gulp": "node ./node_modules/gulp/bin/gulp.js <task-name>" 
    }
    

    and run from command line by typing npm run gulp.

    This removes the dependency on having gulp globally installed

    0 讨论(0)
提交回复
热议问题