我对webpack/宝宝有一些问题。首先我安装了webpack,webpack-cli,webpack-dev-server,babel-loader,babel-core,babel-preset-env和它似乎是工作的fine.But当我安装babel-polyfill和babel-preset-stage-0(因为我想能够使用异步和等待),我不断得到前述错误。我已经与这个斗争了几天,如果有人告诉我,我需要安装哪些依赖和我需要在我的files.At进行哪些调整,这一点,我只想能够使用模块和异步,并等待香草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>
发布于 2019-01-31 10:53:50
检查是否有多个Babel安装。
在我的例子中,一个放在项目的node_modules文件夹中,另一个放在全局文件夹中。
在~/node_modules卸载 my global @babel解决了这个问题。
发布于 2019-02-02 03:53:17
我下载了Brad Traversy的babel_webpack_starter pack.It为我解决了这个问题。
https://stackoverflow.com/questions/54241997
复制相似问题