Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
181
rated 0 times [  188] [ 7]  / answers: 1 / hits: 42330  / 8 Years ago, thu, april 14, 2016, 12:00:00

How to run a npm script command from inside a gulp task?



package.json



scripts: 
{
tsc: tsc -w
}


gulpfile.js



gulp.task('compile:app', function(){
return gulp.src('src/**/*.ts')
.pipe(/*npm run tsc*/)
.pipe(gulp.dest('./dist'))
.pipe(connect.reload());
});


I want to do this because running npm run tsc does not give me any error but if I use gulp-typescript to compile .ts then I get bunch of errors.


More From » node.js

 Answers
18

You can get the equivalent using gulp-typescript



var gulp = require('gulp');
var ts = require('gulp-typescript');

gulp.task('default', function () {
var tsProject = ts.createProject('tsconfig.json');

var result = tsProject.src().pipe(ts(tsProject));

return result.js.pipe(gulp.dest('release'));
});

gulp.task('watch', ['default'], function() {
gulp.watch('src/*.ts', ['default']);
});


Then on your package.json



scripts: {
gulp: gulp,
gulp-watch: gulp watch
}


Then run



npm run gulp-watch






Alternatively using shell



var gulp = require('gulp');
var shell = require('gulp-shell');

gulp.task('default', function () {
return gulp.src('src/**/*.ts')
.pipe(shell('npm run tsc'))
.pipe(gulp.dest('./dist'))
.pipe(connect.reload());
});




gulp-shell has been blacklisted you can see why here



Another alternative would be setting up webpack.


[#62561] Tuesday, April 12, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
pranavrorys

Total Points: 466
Total Questions: 87
Total Answers: 115

Location: Barbados
Member since Sun, Nov 27, 2022
2 Years ago
pranavrorys questions
Fri, May 27, 22, 00:00, 2 Years ago
Thu, Oct 28, 21, 00:00, 3 Years ago
Sat, May 30, 20, 00:00, 4 Years ago
Fri, Dec 20, 19, 00:00, 5 Years ago
;