'import' and 'export' may only appear at the top level

后端 未结 15 1554
耶瑟儿~
耶瑟儿~ 2020-12-08 18:00

I\'m using webpack with vuejs. Webpack does its thing, but when I look at the outputted app.js file, it gives me this error.

\'import\' and \'export\

相关标签:
15条回答
  • 2020-12-08 18:49

    'import' and 'export' may only appear at the top level

    This means that webpack is bundling the non-transpiled ES6 code, which is why these import/export statements are being found. babel-loader must therefore not be transpiling what you expect.

    If you simply remove the include and exclude rules from its loader config, the default behavior of transpiling everything besides what's in node_modules will kick in. For some reason or another, the current rules are causing some/all files to be skipped.

    module.exports = {
      entry: './src/entry.js',
      output: {
        filename: './public/js/app.js'
      },
      devtool: 'source-map',
      plugins: [
        new ExtractTextPlugin('./public/css/style.css')
      ],
      module: {
        preLoaders: [{
          test: /\.js$/, 
          exclude: /node_modules/,
          loader: 'jshint-loader'
        }],
        loaders: [{
          test: /\.scss$/,
          loader: ExtractTextPlugin.extract(
            'style',
            'css!sass'
          ),
        },
        {
          test: /\.vue$/,
          loader: 'vue'
        },
        {
          test: /\.js$/,
          loader: 'babel-loader',
          query: {
            presets: ['es2015']
          }
        }]
      }
    };
    
    0 讨论(0)
  • 2020-12-08 18:51

    Look out for a double opening bracket syntax error as well {{ which can cause this error message to appear

    0 讨论(0)
  • 2020-12-08 18:52

    Using webpack 4.14.0 and babel-cli 6.26 I had to install this Babel plugin to fix the SyntaxError: 'import' and 'export' may only appear at the top level error: babel-plugin-syntax-dynamic-import

    It was linked through from the Webpack Code Splitting Docs.

    0 讨论(0)
  • 2020-12-08 18:54

    I got this error when I was missing a closing brace in a component method:

    const Whoops = props => {
      const wonk = () => {props.wonk();      // <- note missing } brace!
      return (
        <View onPress={wonk} />
      )
    }
    
    0 讨论(0)
  • 2020-12-08 18:54

    Maybe you're missing some plugins, try:

    npm i --save-dev babel-plugin-transform-vue-jsx
    
    npm i --save-dev babel-plugin-transform-runtime
    
    npm i --save-dev babel-plugin-syntax-dynamic-import
    
    • If using "Webpack.config.js":

    • If using ".babelrc", see answer in this link.
    0 讨论(0)
  • 2020-12-08 18:57

    I had the same issue using webpack4, i was missing the file .babelrc in the root folder:

    {
        "presets":["env", "react"],
        "plugins": [
            "syntax-dynamic-import"
        ]
    }
    

    From package.json :

    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.5",
    "babel-plugin-syntax-dynamic-import": "^6.18.0",
    "babel-polyfill": "^6.26.0",
    "babel-preset-env": "^1.7.0",
    "babel-preset-react": "^6.24.1",
    
    0 讨论(0)
提交回复
热议问题