-3

I have at the top of /src/index.js

import dotenv from "dotenv";
dotenv.config({path:"./.env"});

And this in /cloudinary.js

cloudinary.config({ 
 cloud_name: process.env.CLOUD_NAME, 
 api_key: process.env.CLOUD_API_KEY, 
 api_secret: process.env.CLOUD_API_SECRET,
 secure: true
});

process.env.* is not accessible here. I can't use require(), not even sure it would work. I think the problem is of the directory struture like the calling of dotenv() in /src/index.js and using them in /cloudinary.js

Is there any way to fix this and what exactly is the problem.

One solution is to import it again in cloudinary.js but I don't want to.

asked Dec 15, 2025 at 12:18

2 Answers 2

2

Remember that imports are aggregated and executed before code execution happens in the same module, so if your index has an import for your cloudinary file, but "normal code" for running dotenv, you're loading your .env file after you're configuring cloudinary.

Instead, create a new file, load-env.js with only that dotenv instruction, then import that as the very first import in index.js. Now your dotenv config will run before anything else is allowed to happen, as part of the import resolution, and then by the time the cloudinary module import happens you're working with the correct process.env data.

However, the real fix (if you're using Node v20 or newer) is to not use dotenv at all. Environment file loading is built into Node itself, so just run node --env-file=.env src/index instead of node src/index and done. This will update process.env before any of your own code runs so there are no timing issues like the ones associated with using dotenv.

answered Dec 15, 2025 at 18:27
Sign up to request clarification or add additional context in comments.

2 Comments

Yo thanks man it worked. I updated my package.json from "scripts": { "dev": "nodemon src/index.js", "start": "node src/index.js" } to "scripts": { "dev": "nodemon --env-file=.env src/index.js", "start": "node --env-file=.env src/index.js" } And I don't know why people downvoted my question.
For nodemon you probably need nodemon src/index.js -- --env-file=.env because nodemon itself has no --env-file flag. Instead you use -- to pass arguments on to "the next tool". Also, nodemon was a pretty brute force solution to the problem of updating running code during development, so depending on what you're doing a lot of things already have watching built in these days.
-2

(i am guessing this is nodeJs application that is either running in your local enviroment or on the server)

Dont give the path in the dotenv loading line(under the import)
keep your .env file in the root of the project and it should load properly in to process.env

answered Dec 15, 2025 at 13:36

2 Comments

This indeed is a nodeJs application that I'm running locally. The .env is in the root as well as the cloudinary.js the index.js is in the src folder where I am loading this. I did what you asked me to do, remove the path from config, it doesn't work. I still have the same issue
The path location already shows it's in the root dir. And Node does not automatically load .env files, you either need a module for this (in older versions of Node) or the --env-file runtime flag (in current versions of Node).

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.