错误:插件/预设文件不允许导出对象,只能导出功能/ babel-preset-stage-0

时间:2019-01-17 18:19:18

标签: javascript webpack

我在使用webpack / babel时遇到了一些问题。首先我安装了webpack,webpack-cli,webpack-dev-server,babel-loader,babel-core,babel-preset-env,但似乎工作正常。但是当我安装babel-polyfill和babel-preset-stage-0(因为我想能够使用async并等待)时,我不断收到上述error.I一直在努力这几天,如果有人告诉我,我将非常感谢我需要安装哪些依赖项以及需要对文件进行哪些调整。目前,我只想能够使用模块并异步处理并等待香草JavaScript应用程序。

// ./src/app.js
async function getUsers() {
  const response = await fetch("https://jsonplaceholder.typicode.com/users");

  const data = await response.json();

  return data;
}

getUsers().then(data => console.log(data));

//Package json file
{
  "name": "new",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack",
    "start": "webpack-dev-server --output-public-path=/build/"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.2.2",
    "@babel/preset-env": "^7.2.3",
    "babel-loader": "^8.0.5",
    "babel-polyfill": "^6.26.0",
    "babel-preset-stage-0": "^6.24.1",
    "webpack": "^4.28.4",
    "webpack-cli": "^3.2.1",
    "webpack-dev-server": "^3.1.14"
  }
}
//webpack.config.js
const path = require("path");

module.exports = {
  mode: "production",
  entry: {
    app: ["babel-polyfill", "./src/app.js"]
  },
  output: {
    path: path.resolve(__dirname, "build"),
    filename: "app.bundle.js"
  },
  module: {
    rules: [
      {
        test: /\.m?js$/,
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: "babel-loader",
          options: {
            presets: ["@babel/preset-env", "stage-0"]
          }
        }
      }
    ]
  }
};
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
  </head>
  <body>
    <script src="./build/app.bundle.js"></script>
  </body>
</html>

2 个答案:

答案 0 :(得分:0)

检查您是否安装了多个Babel。

就我而言,我在项目的node_modules文件夹中有一个,而在全局文件夹中又有一个。

卸载我在~/node_modules的全局@babel在这里解决了此问题。

答案 1 :(得分:0)

我下载了Brad Traversy的babel_webpack_starter包,它为我解决了这个问题。