Babel生成的代码导致错误导出未定义 [英] Babel generated code causes error exports is undefined

查看:548
本文介绍了Babel生成的代码导致错误导出未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当这段代码(由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,例如 Browserify webpack
将您的模块捆绑到可以在不同的环境中运行。

You are most likely not executing the code in an environment that supports CommonJS modules. You could use a bundler, such as Browserify or webpack to 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生成的代码导致错误导出未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆