问题描述
当这段代码(由babel生成)运行时,我收到错误 export is undefined
When this code (generated from babel) runs I get an error exports is undefined
Object.defineProperty(exports, '__esModule', {
任何想法? / p>
any ideas?
推荐答案
您很有可能在支持CommonJS模块的环境中执行代码,您可以使用bundler,例如或
将您的模块捆绑到可以在不同的环境中运行。
You are most likely not executing the code in an environment that supports CommonJS modules. You could use a bundler, such as Browserify or webpackto bundle your modules into something that can be run in different environments.
或者您可以选择其他。
使用webpack
运行 npm install -g webpack; npm install -D babel-loader
然后用这个webpack配置:
Run npm install -g webpack; npm install -D babel-loader
. Then with this webpack configuration:
// webpack.config.js
module.exports = {
entry: "./path/to/entry/module.js",
output: {
path: __dirname,
filename: "bundle.js"
},
module: {
loaders: [
{ test: /\.js$/, exclude: /node_modules/, loader: "babel-loader"}
]
}
};
运行 webpack
命令将转换所有 *。js
文件可以通过带有babel的条目文件到达,并将它们捆绑在一起成为 bundle.js
。
running the webpack
command will convert all *.js
files reachable via the entry file with babel and bundle them together into bundle.js
.
这篇关于Babel生成的代码导致错误导出未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!