How to automatic run gulp tasks via npm

0 votes

I usually run gulp via npm, e.g. in my package.json

"scripts": {
    "test": "gulp test",
    "minify": "gulp minify"
}

Then I can run command such as

npm run minify

Which is okay, but every time I've new tasks in my gulpfile, I need to add them to the package.json under the scripts section, is there any better way to do so?

I only install npm globally to my path so all other modules will not pollute my path, so I need to run them via npm scripts

Nov 30, 2020 in Node-js by kartik
• 37,520 points
3,146 views

1 answer to this question.

0 votes

Hello @kartik,

Here is the code:

  var file = fs.readFileSync(config.path || 'package.json', 'utf-8');
  var pkg = JSON.parse(file);
  var tasks = gulp.tasks;

  pkg.scripts = pkg.scripts || {};

  Object.keys(tasks).forEach(function (t) {
    pkg.scripts[t] = 'gulp '+tasks[t].name;
  });

So use: npm install --save-dev gulp-npm-script-sync

Stick this in your gulpfile:

var gulp = require('gulp');
var sync = require('gulp-npm-script-sync');

// your gulpfile contents

sync(gulp);

Every time you update your gulpfile with a new task it will update your package.json.

You can even throw it inside a gulp task:

gulp.task('sync', function () {
  sync(gulp);
}
answered Nov 30, 2020 by Niroj
• 82,840 points

Related Questions In Node-js

0 votes
1 answer

How to get the available tasks list in gulp?

Hello @kartik,  I got it use the gulp --tasks in ...READ MORE

answered Oct 13, 2020 in Node-js by Niroj
• 82,840 points
1,145 views
–1 vote
1 answer

How to uninstall npm modules in node js?

Hello @kartik, The command is simply: npm uninstall ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,840 points
3,214 views
0 votes
1 answer

How to install a previous exact version of a NPM package?

Hello @kartik, If you have to install an ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,840 points
2,819 views
0 votes
1 answer

How do I send command line arguments to npm script?

Hello @kartik, The syntax is as follows: npm run ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,840 points
11,043 views
+1 vote
1 answer

What is the relationship between angularjs Scope with controller/view?

Let us consider the below block: <div ng-controller="emp"> ...READ MORE

answered Jan 20, 2020 in Web Development by Niroj
• 82,840 points

edited Jan 21, 2020 by Niroj 1,191 views
0 votes
1 answer

How can we avoid my php form from hacking?

Hii @kartik, If you want to know php ...READ MORE

answered Feb 13, 2020 in PHP by Niroj
• 82,840 points
2,610 views
0 votes
1 answer

What is a Cookie? How to create Cookies With PHP?

A cookie is often used to identify ...READ MORE

answered Feb 13, 2020 in PHP by Niroj
• 82,840 points
3,821 views
0 votes
1 answer

How to shrink Navigation menu or bar on Scroll?

Hey, You can follow the steps below in ...READ MORE

answered Feb 19, 2020 in PHP by varun
3,820 views
0 votes
1 answer

How to run Gulp tasks sequentially one after the other?

Hello @kartik, By default, gulp runs tasks simultaneously, ...READ MORE

answered Jul 12, 2020 in Node-js by Niroj
• 82,840 points
1,520 views
0 votes
1 answer

How to run Gulp tasks sequentially one after the other?

Hello @kartik, By default, gulp runs tasks simultaneously, ...READ MORE

answered Nov 26, 2020 in Node-js by Niroj
• 82,840 points
1,842 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP