Javascript 在Gulp中,child_进程无法执行前一步生成的JS程序

Javascript 在Gulp中,child_进程无法执行前一步生成的JS程序,javascript,node.js,gulp,child-process,Javascript,Node.js,Gulp,Child Process,我写了这样一个大口大口大口的剧本 简而言之,此脚本执行以下操作: 清理(删除)JS程序 构建JS程序 执行JS程序 这个脚本有时(并非总是)失败,但为什么呢 var gulp = require('gulp'), babel = require('gulp-babel'), rename = require('gulp-rename'), concat = require('gulp-concat'); var runSequence = require('run-se

我写了这样一个大口大口大口的剧本

简而言之,此脚本执行以下操作:

  • 清理(删除)JS程序
  • 构建JS程序
  • 执行JS程序
  • 这个脚本有时(并非总是)失败,但为什么呢

    var gulp = require('gulp'),
        babel = require('gulp-babel'),
        rename = require('gulp-rename'),
        concat = require('gulp-concat');
    
    var runSequence = require('run-sequence'),
        del = require('del'),
        exec = require('child_process').exec;
    
    gulp.task('build', function () {
        gulp.src('*.es')
            .pipe(babel())
            .pipe(rename({extname: '.js'}))
            .pipe(gulp.dest('./dist'))
            .pipe(concat('app.js'))
            .pipe(gulp.dest('./dist'));
    });
    
    gulp.task('clean', function () {
        del(['dist']);
    });
    
    gulp.task('exec', function (cb) {
        exec('node dist/app.js', function (err, stdout, stderr) {
            console.log(stdout);
            console.log(stderr);
            cb(err);
        });
    });
    
    gulp.task('default', function (cb) {
        runSequence('clean', 'build', 'exec', cb);
    });
    
    错误消息如下:

    someone@somewhere:~/***/***% gulp
    [15:17:15] Using gulpfile ~/***/***/gulpfile.js
    [15:17:15] Starting 'default'...
    [15:17:15] Starting 'clean'...
    [15:17:15] Finished 'clean' after 4.28 ms
    [15:17:15] Starting 'build'...
    [15:17:15] Finished 'build' after 12 ms
    [15:17:15] Starting 'exec'...
    
    module.js:338
        throw err;
              ^
    Error: Cannot find module '/***/***/dist/app.js'
        at Function.Module._resolveFilename (module.js:336:15)
        at Function.Module._load (module.js:278:25)
        at Function.Module.runMain (module.js:501:10)
        at startup (node.js:129:16)
        at node.js:814:3
    
    [15:17:15] 'exec' errored after 119 ms
    [15:17:15] Error: Command failed: /bin/sh -c node dist/app.js
    module.js:338
        throw err;
              ^
    Error: Cannot find module '/***/***/dist/app.js'
        at Function.Module._resolveFilename (module.js:336:15)
        at Function.Module._load (module.js:278:25)
        at Function.Module.runMain (module.js:501:10)
        at startup (node.js:129:16)
        at node.js:814:3
    
        at ChildProcess.exithandler (child_process.js:751:12)
        at ChildProcess.emit (events.js:110:17)
        at maybeClose (child_process.js:1015:16)
        at Socket.<anonymous> (child_process.js:1183:11)
        at Socket.emit (events.js:107:17)
        at Pipe.close (net.js:485:12)
    [15:17:15] 'default' errored after 142 ms
    [15:17:15] Error: Command failed: /bin/sh -c node dist/app.js
    module.js:338
        throw err;
              ^
    Error: Cannot find module '/***/***/dist/app.js'
        at Function.Module._resolveFilename (module.js:336:15)
        at Function.Module._load (module.js:278:25)
        at Function.Module.runMain (module.js:501:10)
        at startup (node.js:129:16)
        at node.js:814:3
    
        at ChildProcess.exithandler (child_process.js:751:12)
        at ChildProcess.emit (events.js:110:17)
        at maybeClose (child_process.js:1015:16)
        at Socket.<anonymous> (child_process.js:1183:11)
        at Socket.emit (events.js:107:17)
        at Pipe.close (net.js:485:12)
    
    someone@somewhere:~/***/***%大口喝
    [15:17:15]使用gulpfile~/***/***/gulpfile.js
    [15:17:15]正在启动“默认”。。。
    [15:17:15]开始“清洁”。。。
    [15:17:15]在4.28毫秒后完成“清洁”
    [15:17:15]正在启动“构建”。。。
    [15:17:15]12毫秒后完成“构建”
    [15:17:15]正在启动“exec”。。。
    module.js:338
    犯错误;
    ^
    错误:找不到模块“/***/***/dist/app.js”
    在Function.Module.\u解析文件名(Module.js:336:15)
    在Function.Module.\u加载(Module.js:278:25)
    位于Function.Module.runMain(Module.js:501:10)
    启动时(node.js:129:16)
    在node.js:814:3
    [15:17:15]“exec”在119毫秒后出错
    [15:17:15]错误:命令失败:/bin/sh-c node dist/app.js
    module.js:338
    犯错误;
    ^
    错误:找不到模块“/***/***/dist/app.js”
    在Function.Module.\u解析文件名(Module.js:336:15)
    在Function.Module.\u加载(Module.js:278:25)
    位于Function.Module.runMain(Module.js:501:10)
    启动时(node.js:129:16)
    在node.js:814:3
    在ChildProcess.exithandler(child_process.js:751:12)
    在ChildProcess.emit(events.js:110:17)
    在maybeClose(child_process.js:1015:16)
    在插座上。(child_process.js:1183:11)
    在Socket.emit(events.js:107:17)
    在Pipe.close(net.js:485:12)
    [15:17:15]“默认”在142毫秒后出错
    [15:17:15]错误:命令失败:/bin/sh-c node dist/app.js
    module.js:338
    犯错误;
    ^
    错误:找不到模块“/***/***/dist/app.js”
    在Function.Module.\u解析文件名(Module.js:336:15)
    在Function.Module.\u加载(Module.js:278:25)
    位于Function.Module.runMain(Module.js:501:10)
    启动时(node.js:129:16)
    在node.js:814:3
    在ChildProcess.exithandler(child_process.js:751:12)
    在ChildProcess.emit(events.js:110:17)
    在maybeClose(child_process.js:1015:16)
    在插座上。(child_process.js:1183:11)
    在Socket.emit(events.js:107:17)
    在Pipe.close(net.js:485:12)
    

    奇怪的是,再次运行“gulp exec”将会成功。

    您需要返回任务中正在处理的流,以便gulp能够知道任务何时完成:

    var gulp = require('gulp'),
        babel = require('gulp-babel'),
        rename = require('gulp-rename'),
        concat = require('gulp-concat');
    
    var runSequence = require('run-sequence'),
        del = require('del'),
        exec = require('child_process').exec;
    
    gulp.task('build', function () {
        return gulp.src('*.es')
            .pipe(babel())
            .pipe(rename({extname: '.js'}))
            .pipe(gulp.dest('./dist'))
            .pipe(concat('app.js'))
            .pipe(gulp.dest('./dist'));
    });
    
    gulp.task('clean', function () {
        return del(['dist']);
    });
    
    gulp.task('exec', function (cb) {
        exec('node dist/app.js', function (err, stdout, stderr) {
            console.log(stdout);
            console.log(stderr);
            cb(err);
        });
    });
    
    gulp.task('default', function (cb) {
        runSequence('clean', 'build', 'exec', cb);
    });
    

    非常感谢你!你的建议完美地解决了上述问题。