Process.env not working

Im very new to Glitch, and I joined to make a puzzle thingy without paying a domain.
I want the answers of the puzzle hidden in the .env, however, answering the puzzle didn’t do anything.
To test if they weren’t working, I put this in my index.html file: <button onclick="alert(process.env.THEHPLEX)">hi</button>, and nothing happened, not even the alert. But when I replaced the process.env.THEHPLEX with "something", it worked fine.

Env vars can only be accessed on the server side - take a look at remixing the hello-express project for some basics on how to use node.js to serve files, and you can then use the env var on the server side.
I just reread this and it doesn’t really make much sense sorry

1 Like

http://env-test2223.glitch.me/
I remixed the hello-expess project, but .env still didn’t work.

The hello-express project also has this distinction between server side code and client side code. Web pages like an index.html are client side, while process.env in particular is only available on the server side.

So the .env variable has to be “called” in the server-side, such as in script.js? If so, I don’t know how to link the index.html to the script.js.
In the env test, I put the alert functions in the script.js, but it doesn’t work.

in hello-express, the server.js file runs on the server side. and in the public/script.js file runs on the client side.

but to clarify, this isn’t because they’re named some special names “server” and “script.” it’s a result of the various configuration around them. I’ve taken a look at your project, and it looks like you’ve made some unprincipled additions/reorganizations that aren’t based on an understanding of this configuration.

as of now, script.js in your project looks like it’s neither run on the server side nor on the client side.

2 Likes

extremely late response vecause i was somehow too lazyh to write this:
After looking a bit at server.js, it seems like it “transmits” data between it and index.html.
I tried to do the same to process.env, but it still didn’t work.

That sounds like it’s on the right track.

Maybe there’s some subtle issue with the exact way you last tried? The sizeable express library is all about various ways to transmit data from the server to the client. I’m sure there are some wrong ways among all that.

Let us know if you get stuck on this part.

1 Like

Because I’m making a Puzzle game, after a bit of thinking, I thought of this: the client would ask the server if the code is right, and then will check if it equals the env, it will transmit the result.

2 Likes

Short answer: process.env can only accessed from server(Backend) only, like when you’re running nodejs Server.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.