I’m using Parcel in a React project, and I’m running into frequent and inconsistent build issues.
The project works fine one moment, but after making a small change (like importing a hook or updating a file), Parcel suddenly throws errors such as:
Cannot find module ‘react’ – even though react is installed and present in node_modules
.env variables like process.env.BEARER_TOKEN return undefined, even though the exact same code was working before
Some examples:
I added useEffect from “react” and Parcel failed to recognize the react module, breaking the build
I used process.env.BEARER_TOKEN in my API call – initially it worked, then suddenly it broke and returned undefined; when I hardcoded the token, it worked again. But later, without any changes, process.env.BEARER_TOKEN started working fine again
Occasionally, Parcel fails to rebuild unless I:
Delete .parcel-cache
Delete and reinstall node_modules
My setup:
Parcel version: ^2.15.2
React version: 19.1.0
OS: Microsoft Windows [Version 10.0.26100.4351]
My .env file (at project root):
BEARER_TOKEN=yuJhbG… (no quotes or spaces) [this is just example token]
My API options in code:
export const API_OPTIONS = {
method: ‘GET’,
headers: {
accept: ‘application/json’,
Authorization: Bearer ${process.env.BEARER_TOKEN?.trim()},
},
};
What I’ve tried:
Restarting Parcel dev server (npx parcel index.html)
Deleting .parcel-cache
Reinstalling node_modules
Restarting my IDE (VS Code)
Confirmed .env is correctly formatted and at the root
Ensuring I’m not using stale cached tokens or module imports
My question:
Why is Parcel behaving so inconsistently with module resolution and .env variables?
Is this expected behavior? Is there a known issue with Parcel’s caching or environment variable injection?
How can I make my development environment more stable and predictable?
Any insights or best practices for working with Parcel in React projects would be appreciated!