0

On a mac machine, this has no problem running but on a windows machine, it started breaking and for no apparent reason.
On the windows one, it returns the key with missing characters.

In my env file, I have the following (the auth key is a sample one).

AUTH_KEY=ec4$t2VmVoCxW1thpi0tcCW1FdeWla1y2UI16Kxj$u5hBm$FQDuBXJV/lYWo

And I am fetching the data in the following way:

process.env.AUTH_KEY;

I would appreciate it if you have any ideas.

3
  • Are you using create-react-app ? Commented Jan 2, 2020 at 14:08
  • @onuriltan yes. Commented Jan 2, 2020 at 14:10
  • @IvanVasilev did you tried the answer posted by onuriltan, It should work if you need to know why will update it as required Commented Jan 2, 2020 at 14:33

5 Answers 5

1

You should put REACT_APP_ at the beginning of the environment variables like

.env

REACT_APP_AUTH_KEY=ec4$t2VmVoCxW1thpi0tcCW1FdeWla1y2UI16Kxj$u5hBm$FQDuBXJV/lYWo

and use it in your components as

component.js

let key = process.env.REACT_APP_AUTH_KEY
Sign up to request clarification or add additional context in comments.

2 Comments

I am also using parcel to build/run the app, and the key is returned even without the key being prefixed with 'REACT_APP_). But it gets cut off after '\', it should be a Windows thing, because on the Mac machine it works and does not cut the string.
Hmm maybe if you are using different bundler, but accoring to docs you should put REACT_APP or REACT\APP at the begining of the env vars
0

Since you are using CRA(create-react-app), you need to add REACT_APP prefix for the env variables.

So in your case

Instead of

AUTH_KEY=ec4$t2VmVoCxW1thpi0tcCW1FdeWla1y2UI16Kxj$u5hBm$FQDuBXJV/lYWo

Need to update as

REACT_APP_AUTH_KEY=ec4$t2VmVoCxW1thpi0tcCW1FdeWla1y2UI16Kxj$u5hBm$FQDuBXJV/lYWo

Then in code you can access process.env.REACT_APP_AUTH_KEY;

Detailed Description

First of all, it must be clear that there is no such thing as environment variables inside the browser environment. Whichever solution we use nowadays is nothing but a fake abstraction.

But, then you might ask, what about .env files and REACT_APP prefixed environment variables which come straight from documentation? Even inside the source code, these are used as process.env just like we use environment variables inside Node.js.

In reality, the object process does not exist inside the browser environment, it’s Node-specific. CRA by default doesn’t do server-side rendering. It can’t inject environment variables during content serving (like Next.js does). During transpiling, Webpack process replaces all occurrences of process.env with a string value that was given.

8 Comments

I am also using parcel to build/run the app, and the key is returned even without the key being prefixed with 'REACT_APP_). But it gets cut off after '\', it should be a Windows thing, because on the Mac machine it works and does not cut the string.
Okay may be some escaping characters issue
Yeah, I think so, tried to find a solution but the solutions which I found did not work.
@IvanVasilev did you tried this one ? stackoverflow.com/questions/20635695/…
Just checked it out, my string does not contain '<, >, |, &, or ^,' and I tried echoing it in the cmd and it printed it out correctly, it was working before, it stopped working all of a sudden, which was the weird part, maybe a windows update, I have no idea. Thanks for the help though!
|
0

I also faced the same problem where my .env data are being cut off. I believe this may be caused by the word wrap/line break when you copy and paste it to the .env file. I managed to solve it copying the string to a notepad and manually breaking the word wrap. This works for me afterwards, however it can be rather tedious. I guess an easier way could be to set the variables in the .env file programmatically so as to prevent that bug. This link should help https://stackoverflow.com/a/59198410/14631246

However, I had to edit a little from that answer and implement a line break method to remove the line break.

fs.appendFileSync(
  "file_name",
  `ENV_NAME=${VALUE_TO_STORE.replace(/(\r\n|\n|\r)/gm, "")}`
);

Comments

0

You should use

AUTH_KEY=ec4$t2VmVoCxW1thpi0tcCW1FdeWla1y2UI16Kxj$u5hBm$FQDuBXJV/lYWo

instead of

AUTH_KEY=ec4$t2VmVoCxW1thpi0tcCW1FdeWla1y2UI16Kxj$u5hBm$FQDuBXJV/lYWo

You should type the \ character before each $ character.

Otherwise, it will detect it as a variable.

1 Comment

Those two lines are identical, aren't they?
0

You should use

AUTH_KEY=ec4\$t2VmVoCxW1thpi0tcCW1FdeWla1y2UI16Kxj\$u5hBm$FQDuBXJV/lYWo

instead of

AUTH_KEY=ec4$t2VmVoCxW1thpi0tcCW1FdeWla1y2UI16Kxj$u5hBm$FQDuBXJV/lYWo

You should type the \ character before each $ character.

Otherwise, it will mistake it for a variable.

1 Comment

The backslash character is not visible in the output of the answer, both code lines look the same, but the explanation below tells what to do. I suggested an edit to use code formatting so the backslashes become visible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.