【问题标题】:EEXIST error when trying to overwrite an existing file in Gulp?尝试覆盖 Gulp 中的现有文件时出现 EEXIST 错误?
【发布时间】:2017-03-26 02:54:11
【问题描述】:

我的 Gulpfile 中有一些任务我想运行,或者让它们的输出替换或更改现有文件。例如,我想运行wiredep 并让代码替换index.html 中的块(与源文件相同)所以基本上我有以下内容:

gulp.task('bower', () => {
  return gulp.src('app/index.html')
    .pipe(wiredep())
    .pipe(gulp.dest('app/index.html')) // Have wiredep operate on the source 
})

但这会产生EEXIST 错误。

同样,我想运行stylus 命令,并将输出通过管道传输到一个已经存在的文件(因为它之前已经运行过)。

除了每次运行del,我还有其他选择吗?看起来 Gulp 应该能够轻松覆盖现有文件,但我想不出一个简单的方法。

【问题讨论】:

  • Gulp 总是覆盖现有文件。 EEXIST 错误通常表明您没有正确处理某些异步操作。
  • @SvenSchoenung 这似乎是一个非常简单的任务,你看到我在这里做错了什么吗?

标签: gulp


【解决方案1】:

Gulp 有overwrite 选项

gulp.task('bower', () => {
  return gulp.src('input/*.js')
    .pipe(gulp.dest('output/',{overwrite:true})) 
})

另一个例子

const { src, dest } = require('gulp');

function copy() {
  return src('input/*.js')
    .pipe(dest('output/',{overwrite:true}));
}

完整文档https://gulpjs.com/docs/en/api/dest

【讨论】:

    【解决方案2】:

    会好的。

    gulp.task('bower', () => {
      return gulp.src('app/index.html')
        .pipe(wiredep())
        .pipe(gulp.dest('app')) // Have wiredep operate on the source 
    })
    

    【讨论】:

      【解决方案3】:

      gulp.dest() 需要一个目录。您正在向它传递一个文件名

      发生的情况是 gulp 尝试创建目录 app/index.html,但它无法创建目录,因为已经有一个具有该名称的文件。

      您只需将app/ 作为目标目录传递:

      gulp.task('bower', () => {
        return gulp.src('app/index.html')
          .pipe(wiredep())
          .pipe(gulp.dest('app/'));
      })
      

      【讨论】:

        【解决方案4】:

        您应该可以使用gulp.dest,其中有一个选项overwrite

        options.overwrite 类型:布尔值

        默认:真

        指定是否应覆盖具有相同路径的现有文件。

        【讨论】:

        • 好像默认为true,但不起作用?
        • @Startec 但是你在哪里打电话给gulp.dest
        • 我刚刚更新了问题。我在最后调用它并想对同一个文件进行操作
        • 我相信@SvenSchoenung 解决了这个问题,但是dest 需要一个目录而不是一个特定的文件名。
        猜你喜欢
        • 2016-04-26
        • 2020-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-28
        • 1970-01-01
        相关资源
        最近更新 更多