写gulp流的时候报错

枫s的思念
  • 2.4k

在运行gulp的时候报错,错误如下:

[19:04:57] Finished 'default' after 7.38 μs
stream.js:74
      throw er; // Unhandled stream error in pipe.
      ^

Error: ENOENT: no such file or directory, stat 'D:\project\dist\js\index-6045b384e6.min.js'
    at Error (native)

npm ERR! Windows_NT 10.0.10586
npm ERR! argv "D:\\Program\\nodejs\\node.exe" "D:\\Program\\nodejs\\node_modules\\npm\\bin\\npm-cli.           js" "run" "build"
npm ERR! node v4.5.0
npm ERR! npm  v2.15.9
npm ERR! code ELIFECYCLE
npm ERR! project@1.0.0 build: `gulp --gulpfile gulpfile-build.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the project@1.0.0 build script 'gulp --gulpfile gulpfile-build.js'.
npm ERR! This is most likely a problem with the project package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     gulp --gulpfile gulpfile-build.js
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs project
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!
npm ERR!     npm owner ls project
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     D:\project\npm-debug.log

gulpfile.js配置如下:

/*
* 打包上线时候用到的功能
* */
const gulp = require('gulp');
const rename = require('gulp-rename');              //重新命名生成的css和js文件
const sass = require('gulp-sass');                  //scss编译
const uglify = require('gulp-uglify');              //压缩js
const clean = require('gulp-clean');                //清空文件夹里所有的文件
const rev = require('gulp-rev');                    //生成带有哈希值的文件名
const runSequence = require('run-sequence');              //
//const reCollector = require('gulp-rev-collector');  //gulp-rev的插件,用于在生成带哈希值的文件名后替换html中的引用
//每次打包时先清空原有的文件夹
gulp.task('clean',()=>{
    gulp.src(['dist','rev'],{read:false})                   //这里设置的dist表示删除dist文件夹及其下所有文件
        .pipe(clean());

});
//scss编译
gulp.task('css',['clean'],()=>{
    gulp.src('src/scss/*.scss')
        .pipe(sass({
            outputStyle:'compressed'                //编译并输出压缩过的文件
         }))
        .pipe(rename(function (path) {
            path.basename += '.min';
        }))
        .pipe(rev())                                //给css添加哈希值
        .pipe(gulp.dest('dist/css'))
        .pipe(rev.manifest())                       //给添加哈希值的文件添加到清单中
        .pipe(gulp.dest('rev/css'));
});
//压缩js
gulp.task('js',['clean'],()=>{
    gulp.src('src/js/*js')
        .pipe(uglify())
        .pipe(rename(function (path) {
            path.basename += '.min';
        }))
        .pipe(rev())                                //给js添加哈希值
        .pipe(gulp.dest('dist/js'))
        .pipe(rev.manifest())                       //给添加哈希值的文件添加到清单中
        .pipe(gulp.dest('rev/js'));
});
//移动html文件到dist文件夹
gulp.task('html',['clean'],()=>{
    gulp.src('src/*.html')
        .pipe(gulp.dest('dist'));
});
//进行打包上线
gulp.task('build',()=>{
    runSequence('clean',['css','js'],'html');
});
//设置为gulp的默认任务
gulp.task('default',['build']);
回复
阅读 4.6k
3 个回答
Alsolee
  • 1.2k
✓ 已被采纳

使run-sequence同步执行任务,另外在其他task中,gulp.src前面必须加上return

gulp.task('build',()=>{
    runSequence('clean',['css','js','html']);
});
//进行打包上线
gulp.task('default','build');

['clean','css','js','html']
这是并行任务,把 clean 改成 ['css','js','html']的前置任务

基本可以肯定是这里出错了

.pipe(rev())                                //给js添加哈希值
.pipe(rename(function (path) {
    path.basename += '.min';
}))

rev会自己生成一个用来map的json文件,做替换的时候,根据json文件来做的替换。
现在感觉是你rename的时候,没有找到里面json文件对应的js文件,你可以从这方面查查

宣传栏