Babel generated code causes error exports is undefined

前端 未结 2 1976
忘了有多久
忘了有多久 2021-01-04 06:09

When this code (generated from babel) runs I get an error exports is undefined

Object.defineProperty(exports, \'__esModule\', {
<
2条回答
  •  囚心锁ツ
    2021-01-04 07:05

    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.

    Or you could choose a different module transformer.


    With 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"}
          ]
        }
    };
    

    running the webpack command will convert all *.js files reachable via the entry file with babel and bundle them together into bundle.js.

提交回复
热议问题