Basic Node and Express - Use the .env File

Tell us what’s happening:
Describe your issue in detail here.
Hi, this code worked on repl, but using it locally, something seems not to be work, is my .env file supposed to have a name? if so, what name? when requiring dotenv().config(), should it be saved to a variable? and am I supposed to app.use it?
Your code so far

app.get('/json', (req, res) => {
	if (process.env.MESSAGE_STYLE === 'uppercase') {
		res.json({ "message": "Hello json".toUpperCase() });
	} else {
		res.json({ "message": "Hello json" });

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36

Challenge: Basic Node and Express - Use the .env File

Link to the challenge:

When using replit you only need use the SECRETS tab for your environment variables and don’t need a .env file at all.
If you’re working locally however, you’ll need a .env file (named as such) and also install the dotenv package, which you then require as (usually in server.js):


dotenv is used to load variables from the .env file into process.env, so that they can be used in your application.

but it says here " At the top of your myApp.js file, import and load the variables with require('dotenv').config() " , are you saying that in saving the .env file, nothing to should be behind the “.”?

Yes, the .env file is named simply .env.
You require dotenv at the top of your main app code, wherever that is.
As you get onto projects which are more involved, your code will become more modular, so your primary code will likely be in server.js, with routing code, logic and other stuff in separate files, imported as modules into server.js.

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