本文介紹了SyntaxError: 'import' 和 'export' 可能只出現(xiàn)在 'sourceType: module' 中 - Gulp的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
限時送ChatGPT賬號..
考慮以下兩個文件:
app.js
import Game from './game/game';
import React from 'react';
import ReactDOM from 'react-dom';
export default (absPath) => {
let gameElement = document.getElementById("container");
if (gameElement !== null) {
ReactDOM.render(
<Game mainPath={absPath} />,
gameElement
);
}
}
index.js
import App from './src/app';
gulpfile.js
var gulp = require('gulp');
var source = require('vinyl-source-stream');
var browserify = require('browserify');
var babelify = require("babelify");
var watch = require('gulp-watch');
gulp.task('make:game', function(){
return browserify({
entries: [
'index.js'
]
})
.transform('babelify')
.bundle()
.pipe(source('index.js'))
.pipe(gulp.dest('app/'));
});
錯誤:
gulp make:game
[13:09:48] Using gulpfile ~/Documents/ice-cream/gulpfile.js
[13:09:48] Starting 'make:game'...
events.js:154
throw er; // Unhandled 'error' event
^
SyntaxError: 'import' and 'export' may appear only with 'sourceType: module'
這是什么錯誤?我做錯了什么?
推薦答案
舊版本的 Babel 提供了開箱即用的一切.較新的版本要求您安裝安裝所需的任何插件.首先,您需要安裝 ES2015 預(yù)設(shè).
Older versions of Babel came with everything out of the box. The newer version requires you install whichever plugins your setup needs. First, you'll need to install the ES2015 preset.
npm install babel-preset-es2015 --save-dev
接下來,你需要告訴 babelify 使用你安裝的預(yù)設(shè).
Next, you need to tell babelify to use the preset you installed.
return browserify({ ... })
.transform(babelify.configure({
presets: ["es2015"]
}))
...
來源
這篇關(guān)于SyntaxError: 'import' 和 'export' 可能只出現(xiàn)在 'sourceType: module' 中 - Gulp的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!