Gulp错误:ENOENT没有这样的文件或目录,即使它确实存在

时间:2016-07-19 22:58:28

标签: javascript node.js gulp browserify

我正在运行gulp任务时收到Unhandled rejection Error: ENOENT: no such file or directory, stat 'path/to/file' ,即使相关文件确实存在于该确切路径上。

这是gulp日志:

15:35:46] Requiring external module babel-register
[15:35:57] Using gulpfile ~/Path/To/Project/gulpfile.babel.js
[15:35:57] Starting 'clean'...
[15:35:57] Finished 'clean' after 5.08 ms
[15:35:57] Starting 'dev'...
[15:35:57] Starting 'ghost'...
[15:35:57] Finished 'ghost' after 7.44 ms
[15:35:57] Starting 'styles'...
[15:35:57] Starting 'pug'...
[15:35:57] Starting 'browserify'...
[15:35:57] Rebundle...
Unhandled rejection Error: ENOENT: no such file or directory path/to/node_modules/ghost/content/themes/submittedly'

文件确实存在,如下所示: file exists

我的开发工作流程包括启动ghost服务器,它可以在不使用编译器的情况下工作,例如browserify,但在我的情况下,我需要一个代码编译器。

我假设错误发生在“browserify”任务或“browserSync”任务中:以下是两者的代码:

Browserify:

'use strict';

import gulp         from 'gulp';
import gulpif       from 'gulp-if';
import gutil        from 'gulp-util';
import source       from 'vinyl-source-stream';
import streamify    from 'gulp-streamify';
import sourcemaps   from 'gulp-sourcemaps';
import rename       from 'gulp-rename';
import watchify     from 'watchify';
import browserify   from 'browserify';
import babelify     from 'babelify';
import uglify       from 'gulp-uglify';
import browserSync  from 'browser-sync';
import debowerify   from 'debowerify';
import handleErrors from '../util/handle-errors';
import config       from '../config';

// Based on: http://blog.avisi.nl/2014/04/25/how-to-keep-a-fast-build-with-browserify-and-reactjs/
function buildScript(file, watch) {

  let bundler = browserify({
    entries: ['./src/assets/js/' + file],
    debug: !global.isProd,
    cache: {},
    packageCache: {},
    fullPaths: global.isProd ? false : true
  });

  if ( watch ) {
    bundler = watchify(bundler);
    bundler.on('update', rebundle);
  }

  bundler.transform(babelify);
  bundler.transform(debowerify);

  function rebundle() {
    const stream = bundler.bundle();

    gutil.log('Rebundle...');

    return stream.on('error', handleErrors)
    .pipe(source(file))
    .pipe(gulpif(global.isProd, streamify(uglify())))
    .pipe(streamify(rename({
      basename: 'main'
    })))
    .pipe(gulpif(!global.isProd, sourcemaps.write('./')))
    .pipe(gulp.dest(config.dest.js))
    .pipe(gulpif(browserSync.active, browserSync.reload({ stream: true, once: true })));
  }

BrowserSync:

'use strict';

import url         from 'url';
import browserSync from 'browser-sync';
import gulp        from 'gulp';
import config      from '../config';

gulp.task('browserSync', () =>  {

  const DEFAULT_FILE = 'index.hbs';
  const ASSET_EXTENSION_REGEX = new RegExp(`\\b(?!\\?)\\.(${config.assetExtensions.join('|')})\\b(?!\\.)`, 'i');

  /*browserSync.use('logger', function () {
    return function (emitter) {
        emitter.on('init', function (data) {
            console.log('Server started... go to http://localhost:2368');
          });
      }
  });*/

  browserSync.init({
    server: {
      baseDir: config.dest.dir,
      middleware: function(req, res, next) {
        const fileHref = url.parse(req.url).href;

        if ( !ASSET_EXTENSION_REGEX.test(fileHref)) {
          req.url = '/' + DEFAULT_FILE;
        }

        return next();
      }
    },
    open: false,
    port: config.browserPort,
    ui: {
      port: config.UIPort
    },
    ghostMode: {
      links: false
    }
  });

});

这些是我为我的工作流程运行的任务:

'use strict';

import gulp        from 'gulp';
import runSequence from 'run-sequence';

gulp.task('dev', ['clean'], function(cb) {

  cb = cb || function() {};

  global.isProd = false;

  return runSequence(['ghost', 'styles', 'pug', 'browserify'], 'watch', cb);

});

这是我的工作流程设置: workflow

0 个答案:

没有答案