I am new to webpack and am just trying to get it actually work. I haven’t found anyone else with my issue, but everytime I try to npm run build which runs webpack I get an error saying:
ERROR in ./src/index.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: C:UsersbradrOneDriveDesktopreposweather-appsrcindex.js: Unexpected character ‘�’. (1:0)
Index.js file
import { heythere } from "./generateJoke";
console.log(123);
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script src="main.js"></script>
</body>
</html>
First I thought maybe I have made a mistake in my code or in following the tutorial but I did it again the same error came up. Then I started googling and thought maybe I could change the webpack.config.js file.
webpack.config.js
const path = require("path");
module.exports = {
entry: "/src/index.js",
output: {
filename: "main.js",
path: path.resolve(__dirname, "dist"),
},
module: {
rules: [
{
test: /.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: ["@babel/preset-env"],
},
},
},
],
},
};
but nothing is working, every little tweak I make is the same error.
2
Answers
The issue was that my file was encoding its self in UTF-16 LE instead of the defualt UTF-8 that I had set, thus making the file unreadable by the transpiler.
Most of the times error like that means that your file has some bytes that are not displayed as visible characters in text editors:
https://www.w3.org/International/questions/qa-utf8-bom.en
You can check your file’s encoding in Encoding menu of Notepad++, for example.