Home
Support Forums

ENOENT: no such file or directory, open '/opt/build/repo/.env'

Really not sure why my Netlify build is looking for a .env file.

``site: frenchplus.netlify.app

On account of some private access keys, I keep it out of my git repo, with .env in my .gitignore file.
And I have the dotenv package in my devDependencies.
I also think I’ve removed all trace of any references to .env.
(I’ve certainly searched the files a few times)
And I’ve set my environment variables in the Netlify Dashboard.
So, afaik, there’s no need for a .env file anyway.
But whether I do a netlify CLI build on my local machine, or via my github repo, I get the error message about the missing .env file.

If possible, I’d rather not have to build a .env file in Netlify.
(That seems a bit of an odd thing to have to do)

Meantime I’m working around the issue by using the Netlify CLI command to push to prod:
netlify deploy --prod

Any suggestions about how to track down why it persists in looking for the .env file?

Deploy Log
Loading

Correct.

The dotenv package looks for a .env file.

Could be a misunderstanding on my part.

I thought if I had dotenv in devDependencies in the package.json file, that meant it wouldn’t be required for production on Netlify?

A per dotenv - npm

Create a .env file in the root directory of your project. Add environment-specific variables on new lines in the form of NAME=VALUE . …

process.env now has the keys and values you defined in your .env file.

If you have set up environment variables in the Netlify UI, these are available via process.env

Possibly this is a fix.

On doing a file search, I discovered I had two environment variables for production, ELEVENTY_ENV=production and NODE_ENV=production.

That particular error message disappeared after I standardized all instances of that variable to NODE_ENV=production.

So possibly the build process may have been confused, and looking for NODE_ENV.

But not conclusively the solution, because a new error then stopped the build process, which hasn’t yet been resolved :roll_eyes:

Facing same issue last time and still searching for some proper solution.

www.rapidfs.com

Hi @Dickens,

Could you please elaborate?

Ahh…discovered my duplicate ENV=production wasn’t the problem, but a env.js file which was indeed looking for dotenv via require(“dotenv”).

I guess I went down a wrong path because the error message seemed to suggest dotenv couldn’t find a .env file, which puzzled me because I had my dotenv in devDependencies.