gpt4 book ai didi

javascript - 从 Babel 5 迁移到 6 时出错(ReferenceError : exports is not defined)

转载 作者:行者123 更新时间:2023-11-29 10:10:37 25 4
gpt4 key购买 nike

我正在使用 Gulp 和 Babel 将客户端 es6 代码编译为 es5。升级后出现此错误(在浏览器中):未捕获的 ReferenceError:导出未定义

此错误的原因是 Babel 将我的客户端脚本编译为 CommonJS 模块并在每个文件的开头添加了以下行:

Object.defineProperty(exports, "__esModule", { // <-- ReferenceError: exports is not defined
value: true
});

但是我没有在客户端使用任何 UMD/CommonJS 模块加载器,所以这段代码会导致错误。对于 Babel 5,为了避免这种情况,我在我的 gulpfile 中使用了选项 modules: 'ignore':

return gulp.src(src, {base: 'src'})
.pipe(babel({
modules: 'ignore' // <-- dropped from Babel 6
}))
.pipe(gulp.dest(dest));

因此它按原样编译了我的脚本,原始且清晰。但是这个选项从 Babel 6 中删除了,现在它会导致错误[ReferenceError: [BABEL] ..myscript.js: Unknown option: base.modules], 所以我不得不评论这一行。

在 Babel 6 中,是否有替代 modules: 'ignore' 的方法?

最佳答案

因为您使用的是 es2015 this set of plugins默认启用。请注意,babel-plugin-transform-es2015-modules-commonjs 就在那里。

如果您不想执行任何类型的模块转换,您需要明确列出您想要使用的插件,而不是使用 es2015

关于javascript - 从 Babel 5 迁移到 6 时出错(ReferenceError : exports is not defined),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34240430/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com