'import'和'export'可能只出现在顶层

Jay*_*n H 40 webpack vue.js babeljs

我正在使用带有vuejs的webpack.Webpack做了它的事情,但是当我查看输出的app.js文件时,它给了我这个错误.

'import'和'export'可能只出现在顶层

我假设babel没有转换代码是个问题?因为我在查看应用程序时在浏览器中收到此信息.

意外的令牌导入

这是我的vuejs应用程序的entry.js:

/*jshint esversion: 6 */
import Vue from 'vue';
import App from './App.vue';
import VueRouter from 'vue-router';
Vue.use(VueRouter);
require('./css/style.scss');

// Export the vue router
export var router = new VueRouter({
  hashbang: false,
  root: '/'
  // history: true
});

// Set up routing and match routes to components
router.map({
  '/': {
    component: require('./components/home/Home.vue')
  }
});

// Redirect to the home route if any routes are unmatched
router.redirect({
  '*': '/'
});

// Start the app on the #app div
router.start(App, '#app');
Run Code Online (Sandbox Code Playgroud)

这是我的webpack.config.js:

var ExtractTextPlugin = require('extract-text-webpack-plugin');
var path = require('path');

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$/,
        exclude: /node_modules/,
        include: [
          path.resolve(__dirname, "src/services"),
        ],
        loader: 'babel-loader',
        query: {
          presets: ['es2015']
        }
    }]
  }
};
Run Code Online (Sandbox Code Playgroud)

这是我的packages.json文件:

{
  "name": "test-webpack",
  "version": "1.0.0",
  "description": "Myapp",
  "main": "entry.js",
  "scripts": {
    "watch": "webpack-dev-server  --host $IP --port $PORT  --hot --inline --config webpack.config.js",
    "dev": "webpack",
    "build": ""
  },
  "author": "Dev",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.9.1",
    "babel-loader": "^6.2.4",
    "babel-plugin-transform-class-properties": "^6.10.2",
    "babel-plugin-transform-runtime": "^6.9.0",
    "babel-preset-es2015": "^6.9.0",
    "babel-runtime": "^6.9.2",
    "css-loader": "^0.23.1",
    "extract-text-webpack-plugin": "^1.0.1",
    "jshint": "^2.9.2",
    "jshint-loader": "^0.8.3",
    "node-sass": "^3.8.0",
    "path": "^0.12.7",
    "sass-loader": "^3.2.0",
    "style-loader": "^0.13.1",
    "vue-hot-reload-api": "^1.3.2",
    "vue-html-loader": "^1.2.2",
    "vue-loader": "^8.5.2",
    "vue-style-loader": "^1.0.0",
    "webpack": "^1.13.1",
    "webpack-dev-server": "^1.14.1"
  },
  "dependencies": {
    "vue": "^1.0.25",
    "vue-router": "^0.7.13"
  }
}
Run Code Online (Sandbox Code Playgroud)

luk*_*aus 98

当我错过一个结束括号时,我收到了这个错误.

简化娱乐:

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

export default foo;
Run Code Online (Sandbox Code Playgroud)

  • 在90%的案例中,这个答案可能是正确的答案.检查你的开口结束括号! (18认同)
  • 谢谢,我快疯了。 (2认同)
  • 不敢相信这么微不足道的事情会让我如此头疼。 (2认同)

Jac*_*cob 13

'import'和'export'可能只出现在顶层

这意味着webpack捆绑了非转换的ES6代码,这就是找到这些import/ export语句的原因.babel-loader因此,必须不要发现你的期望.

如果您只是从其加载器配置中删除includeexclude规则,则除了内容之外的所有内容的默认行为node_modules都会启动.出于某种原因,当前规则导致跳过某些/所有文件.

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']
      }
    }]
  }
};
Run Code Online (Sandbox Code Playgroud)


Ace*_*Ace 10

确保你有一个.babelrc文件来声明Babel应该转换的内容.我花了30分钟试图找出这个确切的错误.在我将一堆文件复制到一个新文件夹后发现我没有复制.babelrc文件,因为它被隐藏了.

{
  "presets": "es2015"
}
Run Code Online (Sandbox Code Playgroud)

或者你在.babelrc文件中寻找的东西


Bas*_*ien 8

我在使用 webpack4 时遇到了同样的问题,我在根文件夹中丢失了 .babelrc 文件:

{
    "presets":["env", "react"],
    "plugins": [
        "syntax-dynamic-import"
    ]
}
Run Code Online (Sandbox Code Playgroud)

从 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",
Run Code Online (Sandbox Code Playgroud)


duh*_*ime 8

当我在组件方法中缺少右括号时出现此错误:

const Whoops = props => {
  const wonk = () => {props.wonk();      // <- note missing } brace!
  return (
    <View onPress={wonk} />
  )
}
Run Code Online (Sandbox Code Playgroud)


Eri*_*son 5

还要注意双左括号语法错误,{{这可能会导致出现此错误消息


Jon*_*ins 5

使用webpack 4.14.0和babel-cli 6.26,我必须安装此Babel插件来修复SyntaxError: 'import' and 'export' may only appear at the top level错误:babel-plugin-syntax-dynamic-import

它是通过Webpack代码拆分文档链接的