SyntaxError: #39;import#39; and #39;export#39; may appear only with #39;sourceType: module#39; - Gulp(SyntaxError: import 和 export 可能只出现在 sourceType: module 中 - Gulp)
问题描述
考虑以下两个文件:
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 预设.
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 使用你安装的预设.
Next, you need to tell babelify to use the preset you installed.
return browserify({ ... })
.transform(babelify.configure({
presets: ["es2015"]
}))
...
来源
这篇关于SyntaxError: 'import' 和 'export' 可能只出现在 'sourceType: module' 中 - Gulp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:SyntaxError: 'import' 和 'export' 可能只出


- 如何显示带有换行符的文本标签? 2022-01-01
- 使用 iframe URL 的 jQuery UI 对话框 2022-01-01
- 从原点悬停时触发 translateY() 2022-01-01
- 为什么我的页面无法在 Github 上加载? 2022-01-01
- 如何向 ipc 渲染器发送添加回调 2022-01-01
- 在不使用循环的情况下查找数字数组中的一项 2022-01-01
- 为什么悬停在委托事件处理程序中不起作用? 2022-01-01
- 如何调试 CSS/Javascript 悬停问题 2022-01-01
- 我不能使用 json 使用 react 向我的 web api 发出 Post 请求 2022-01-01
- 是否可以将标志传递给 Gulp 以使其以不同的方式 2022-01-01