I have an issue when trying to build my react app.
This works perfectly in a dev environement using npm start
however once I try to build it I have this error.
> [email protected] build
> react-scripts build
Creating an optimized production build...
Failed to compile.
Unexpected end of JSON input
I have added a console log to verify that my environment variables are correctly replaced in dev environment however those are not fetch correctly during the build.
My .env is at the root directory of my application and all my variable starts with REACT_APP_*
The code of the login page I am trying to build
import React, { useState } from "react";
import { signInWithEmailAndPassword } from "firebase/auth"; // Import signInWithEmailAndPassword
import { auth } from "./firebase"; // Import Firebase auth instance
function Login() {
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [error, setError] = useState("");
const handleSubmit = async (event) => {
event.preventDefault();
if (!email || !password) {
setError("Email and password are required.");
return;
}
try {
const userCredential = await signInWithEmailAndPassword(auth, email, password);
console.log("User signed in:", userCredential.user);
alert("Login successful!");
} catch (err) {
console.error("Error during login:", err.message);
setError("Login failed: " + err.message);
}
};
return (
<div>
<h1>Login</h1>
<form onSubmit={handleSubmit}>
<div>
<label>Email:</label>
<input
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
required
/>
</div>
<div>
<label>Password:</label>
<input
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
required
/>
</div>
{error && <p style={{ color: "red" }}>{error}</p>}
<button type="submit">Login</button>
</form>
</div>
);
}
export default Login;
Thanks
4
Answers
So I completely uninstall node that was previously installed using nvm and reinstall it using brew and it works thanks @Michael Shobowale
The error
Unexpected end of JSON input
during the build process,normally occurs with a issue in environment variables or Firebase configuration not being correctly set or accessible during the build.defined in your
.env
file.I fixed this issue on a Mac OS by upgrading the version of Node, I just ran the following command
So what’s your format in your
.env
file.Maybe you should go for dotenv doc to check your code.