'import' and 'export' may only appear at the top level 'import' and 'export' may only appear at the top level vue.js vue.js

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


I got this error when I was missing a closing bracket.

Simplified recreation:

const foo = () => {  return (    'bar'  );}; <== this bracket was missingexport default foo;


'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']      }    }]  }};


Make sure you have a .babelrc file that declares what Babel is supposed to be transpiling. I spent like 30 minutes trying to figure this exact error. After I copied a bunch of files over to a new folder and found out I didn't copy the .babelrc file because it was hidden.

{  "presets": "es2015"}

or something along those lines is what you are looking for inside your .babelrc file