getting error : TypeError: fs.readFileSync is not a function
when i tried to use get value from .env file, can anyone please help me why i am getting this error? here i have uploaded my code, can anyone please help me how to resolve this issue ? any help will be really appreciated.
const dotenv = require('dotenv').config();
const fs = require('fs');
const envConfig = dotenv.parse(fs.readFileSync('.env'))
for (const k in envConfig) {
process.env[k] = envConfig[k]
}
Firstly I want to remind that
fs
will not work in the browser. This is by design as to protect your filesystem from potential security threats.Did you use
create-react-app
when you were creating ReactJs project?If it is the case, the following solution will fix this issue.
For example :
This works -
This doesn't
You can read the reference link in here. https://create-react-app.dev/docs/adding-custom-environment-variables/