dest.write在broswerify中不是gulp中的函数

3 javascript browserify gulp

我尝试使用gulp和browserify构建我的react项目。但是每次我尝试构建捆绑软件时,都会出现此错误:

[16:19:54] Using gulpfile /var/www/html/new-webclass/gulpfile.js
[16:19:54] Starting 'build'...
[16:19:54] 'build' errored after 36 ms
[16:19:54] TypeError: gulp.src(...).pipe(...).pipe is not a function
    at Gulp.<anonymous> (/var/www/html/new-webclass/gulpfile.js:80:6)
    at module.exports (/var/www/html/new-webclass/node_modules/orchestrator/lib/runTask.js:34:7)
    at Gulp.Orchestrator._runTask (/var/www/html/new-webclass/node_modules/orchestrator/index.js:273:3)
    at Gulp.Orchestrator._runStep (/var/www/html/new-webclass/node_modules/orchestrator/index.js:214:10)
    at Gulp.Orchestrator.start (/var/www/html/new-webclass/node_modules/orchestrator/index.js:134:8)
    at /usr/lib/node_modules/gulp-cli/lib/versioned/^3.7.0/index.js:46:20
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9)
    at Module.runMain (module.js:592:11)
    at run (bootstrap_node.js:394:7)
/var/www/html/new-webclass/node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_readable.js:623
    var written = dest.write(chunk);
                       ^

TypeError: dest.write is not a function
    at write (/var/www/html/new-webclass/node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_readable.js:623:24)
    at flow (/var/www/html/new-webclass/node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_readable.js:632:7)
    at DestroyableTransform.pipeOnReadable (/var/www/html/new-webclass/node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_readable.js:664:5)
    at emitNone (events.js:86:13)
    at DestroyableTransform.emit (events.js:185:7)
    at emitReadable_ (/var/www/html/new-webclass/node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_readable.js:448:10)
    at emitReadable (/var/www/html/new-webclass/node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_readable.js:444:5)
    at readableAddChunk (/var/www/html/new-webclass/node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_readable.js:187:9)
    at DestroyableTransform.Readable.push (/var/www/html/new-webclass/node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_readable.js:149:10)
    at DestroyableTransform.Transform.push (/var/www/html/new-webclass/node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_transform.js:145:32)
Run Code Online (Sandbox Code Playgroud)

TypeError: dest.write is not a function。好吧,这很奇怪。我找不到任何解决方案。

package.json(仅限devDependencies)

devDependencies": {
    "babel-preset-es2015": "^6.14.0",
    "babel-preset-react": "^6.11.1",
    "babel-preset-stage-0": "^6.5.0",
    "babelify": "^7.3.0",
    "bootstrap-sass": "^3.3.7",
    "browserify": "^13.1.0",
    "gulp": "^3.9.1",
    "gulp-autoprefixer": "^3.1.1",
    "gulp-pug": "^3.0.4",
    "gulp-sass": "^2.3.2",
    "react": "^15.3.1",
    "react-dom": "^15.3.1",
    "vinyl-buffer": "^1.0.0",
    "vinyl-source-stream": "^1.1.0"
  }
Run Code Online (Sandbox Code Playgroud)

波纹管是我的gulpfile要求部分。

const gulp = require('gulp');
const sass = require('gulp-sass');
const autoprefixer = require('gulp-autoprefixer');
const pug = require('gulp-pug');
const browserify = require('browserify');
const babelify = require('babelify');
const source = require('vinyl-source-stream');
const buffer = require('vinyl-buffer');
Run Code Online (Sandbox Code Playgroud)

这是我用于捆绑的任务。

gulp.task('build', function() {
  return gulp.src('./react/index.jsx')
    .pipe(browserify({
      extensions: ['.jsx'],
      debug: true,
      cache: {},
      packageCache: {},
      fullPaths: true
    }))
    .pipe(babelify.configure({ presets: ['es2015', 'react', 'stage-0'] }))
    .pipe(source('bundle.js'))
    .pipe(buffer())
    .pipe(gulp.dest('./public/react/'));
});
Run Code Online (Sandbox Code Playgroud)

还有另一种方式做捆绑吗?怎么了?

小智 5

我花了一段时间(实际上是整天)才弄清楚。整个配置是错误的。这是正确的:

gulp.task('build', function() {
  return browserify({
      extensions: ['.jsx', '.js'],
      debug: true,
      cache: {},
      packageCache: {},
      fullPaths: true,
      entries: './react/index.js',
    })
    .transform(babelify.configure({ 
        presets: ['es2015', 'react', 'stage-0'],
        ignore: /(bower_components)|(node_modules)/
    }))
    .bundle()
    .on("error", function (err) { console.log("Error : " + err.message); })
    .pipe(source('bundle.js'))
    .pipe(gulp.dest('./public/react'));
});
Run Code Online (Sandbox Code Playgroud)

Src:将React与ES6和Browserify结合使用by wecodetheweb.com