Ecmascript 6 ES6编译控制台错误
我有以下吞咽文件:Ecmascript 6 ES6编译控制台错误,ecmascript-6,gulp,Ecmascript 6,Gulp,我有以下吞咽文件: 'use strict' const gulp = require('gulp'); const babel = require('gulp-babel'); const concat = require('gulp-concat'); // Create an es6 task gulp.task('es6', () => { // Return gulp.src with the src set to our src folder // we
'use strict'
const gulp = require('gulp');
const babel = require('gulp-babel');
const concat = require('gulp-concat');
// Create an es6 task
gulp.task('es6', () => {
// Return gulp.src with the src set to our src folder
// we return here do that we indicate to gulp that this task is async
return gulp.src('src/*.js')
// We pipe the source into babel
.pipe(babel({
// We need to tell babel to use the babel-preset-es2015
presets: ['es2015']
}))
// We then pipe that into gulp.dest to set a final destination
// In this case a build folder
.pipe(concat('app.js'))
.pipe(gulp.dest('build'));
});
gulp.task('default', ['es6'],() => {
gulp.watch('src/*.js',['es6'])
});
当我运行gulp时,它会正确编译,没有错误,但是在预览HTML时,我会在控制台中遇到此错误:
Uncaught ReferenceError: exports is not defined
关于这一点:
Object.defineProperty(exports, "__esModule", {
value: true
});
如何解决这个问题
已更新
我已经更新了我的gulp文件,但它还没有完全工作:
'use strict'
const gulp = require('gulp');
const babel = require('gulp-babel');
const gutil = require('gulp-util');
const source = require('vinyl-source-stream');
const browserify = require('browserify');
// Create an es6 task
gulp.task('es6', () => {
// Return gulp.src with the src set to our src folder
// we return here do that we indicate to gulp that this task is async
// return gulp.src('src/*.js')
// // We pipe the source into babel
// .pipe(babel({
// // We need to tell babel to use the babel-preset-es2015
// presets: ['es2015']
// }))
// // We then pipe that into gulp.dest to set a final destination
// // In this case a build folder
// .pipe(concat('src/*.js'))
// .pipe(gulp.dest('build'));
return browserify('src/app.js')
.bundle()
.pipe(source('bulid/app.js'))
.pipe(babel({
// We need to tell babel to use the babel-preset-es2015
presets: ['es2015']
}))
.pipe(gulp.dest('build'));
});
gulp.task('default', ['es6'],() => {
gulp.watch('src/*.js',['es6'])
});
这是babel的问题,您只是试图在浏览器中无准备地运行CommonJS代码(从ES6导出传输)。CommonJS不在浏览器上运行,您需要使用一个工具为浏览器打包它,例如Webpack或Browserify。请参阅我的更新答案。在发生传输后,您需要Webpack/Browserify。我已更新,但出现以下错误:events.js:160 throw er;//未处理的“错误”事件^error:不支持流式处理