motdotla / dotenv

Loads environment variables from .env for nodejs projects.
https://www.dotenvx.com
BSD 2-Clause "Simplified" License
19.01k stars 853 forks source link

Getting error while reinstalling the dotenv (updating to `16.4.4` should fix. apologies for the bad patch release of `16.4.3` everyone) #810

Closed utkarsh621x closed 6 months ago

utkarsh621x commented 6 months ago

Just got an error after running npm i and then npm start on my project. Strange thing is that this error started apearing after only when i deleted my package-lock.json and node modules folder then reinstalling them via npm i

` D:\projects\Winkget proj\Partner\server\node_modules\dotenv\lib\main.js:210 if (options?.encoding) { ^

SyntaxError: Invalid or unexpected token at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)

Node.js v20.10.0 [nodemon] app crashed - waiting for file changes before starting... ` the file where the error os occuring is located in node modules folder & error is => Screenshot (341)

i have checked the same file from my old projects there is some changes that appeared after reinstalling the node modules this is the old file ==> image

this is my main js file where i have import dotenv package => image

sverkoye commented 6 months ago

Hi, getting the same error with latest 16.4.3 version. I am using Node.js version 12.16.3. It looks like the optional chaining operator (?.) is not supported in Node.js v12. Only starting from node v14 it is supported.

Your package.json does say this: "engines": { "node": ">=12" }, Seems to be in violation.

utkarsh621x commented 6 months ago

I am using node version 20

Emmo00 commented 6 months ago

Found this issue with a similar problem... https://github.com/Automattic/mongoose/issues/13971

Looks like the node dependency used might be a major cause.

utkarsh621x commented 6 months ago

I successfully resolved the error by removing the caret (^) from the dotenv version specifier in the package.json file. It appears that there were compatibility issues with the latest version being installed by the caret, so I switched to version 16.0.0, which is functioning as expected.

uniqname commented 6 months ago

Just as a data point, v16.4.3 caused a breakage for us as well. Will update with more findings as we discover them.

ryanolson-aumni commented 6 months ago

This was a major breaking change. Dependabot auto updated our project and this resulted in a hard crash on our client-side. We weren't expected that from a patch release.

ryanolson-aumni commented 6 months ago
Screenshot 2024-02-13 at 8 24 10 AM

It was failing on attempting to run existsSync on the client-side. (side note: this is in a Next.js app)

motdotla commented 6 months ago

this was a mistake with our patch release. so sorry, everyone. we are putting out a patch shortly. it appears isolated to node 12.

motdotla commented 6 months ago

until we get the patch out you have 2 options:

  1. Upgrade your node to 14 or higher
  2. Hardcode your dotenv version to v16.4.2 (the bug was introduced in yesterday's patch 16.4.3)
utkarsh621x commented 6 months ago
  1. Upgrade your node to 14 or higher

Sir i am already on node v20.10.0 but still facing the error

motdotla commented 6 months ago

effort happening here: https://github.com/motdotla/dotenv/pull/812

uniqname commented 6 months ago

Reiterating @utkarsh621x, we are on node v 20.10.0 when we got the error.

motdotla commented 6 months ago

released v16.4.4

please upgrade to 16.4.4 and report back.

@utkarsh621x let me know if that fixes it for you as well. that operator is supported for node 20 so i'm surprised you are receiving the error.

motdotla commented 6 months ago
Screenshot 2024-02-13 at 8 24 10 AM

It was failing on attempting to run existsSync on the client-side. (side note: this is in a Next.js app)

@ryanolson-aumni what version did you upgrade from with dependabot? was it from 16.4.2 to 16.4.3 or was it from a much earlier version?

utkarsh621x commented 6 months ago

The issue has been resolved by updating to version 16.4.4. I appreciate the prompt resolution.

motdotla commented 6 months ago

great!

ryanolson-aumni commented 6 months ago

@motdotla It was from 16.4.2 -> 16.4.3.

ryanolson-aumni commented 6 months ago

I updated to 16.4.4 and tested locally. I'm still seeing an issue (with the same error message "Unhandled Runtime Error TypeError: fs.existsSync is not a function")

It's failing on fs.existsSync in this block of code:

let optionPathsThatExist = []
  if (options && options.path) {
    if (!Array.isArray(options.path)) {
      if (fs.existsSync(options.path)) {
motdotla commented 6 months ago

@ryanolson-aumni are you using webpack? or is this back-end node code?

ryanolson-aumni commented 6 months ago

Using Next.js 14.1.0 with SWC (speedy web compiler). Then in our _app.jsx (for all pages), we've had:

import dotenv from 'dotenv';

dotenv.config({ path: '.env' });

const App = () => {
// not showing implementation
}

export default App;
magikcypress commented 6 months ago

the same error message "Unhandled Runtime Error TypeError: fs.existsSync is not a function"

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

or

require('dotenv').config({ path: '../../.env' });

Node : 20.11.0 Webpack : 5.90.2

motdotla commented 6 months ago

@ryanolson-aumni next.js has built-in support for .env files that uses dotenv under the hood: https://nextjs.org/docs/pages/building-your-application/configuring/environment-variables

importing dotenv in front-end code will not work because front-end can not read from a file. any use of dotenv for front-end code should be in the build step - not in actual front-end code.

or going forward we recommend using dotenvx for this as it's more straightfoward:

dotenvx run -- next build
dotenvx run -- next dev
motdotla commented 6 months ago

@magikcypress looks like you are experiencing the same issue - attempting to use dotenv inside runtime front-end code. front-end does not have a filesystem available to it. you can use https://github.com/mrsteele/dotenv-webpack.

ryanolson-aumni commented 6 months ago

@motdotla I don't entirely disagree with you. However, we've been using it this way for over a year now (in production) without any issues until 16.4.3. The _app.jsx page runs on the server-side for server-side rendering. Then once pushed to the frontend, can run things client side. Something that changed in 16.4.2->16.4.3 broke this. So I'd still like this to be resolved.

motdotla commented 6 months ago

here's everything that changed since then: https://github.com/motdotla/dotenv/compare/v16.4.2...v16.4.4

i'll try and take a look soon and see why this would result in a front-end fs issue when prior it somehow got away with it.

motdotla commented 6 months ago

at a quick glance, it looks like prior there was no fs.existsSync to trip up on and then the fs.readFileSync was under a try/catch (for good reason in case the .env file does not exist and using production-set envs).

for front-end that would result in dotenv.config() silently not working. https://github.com/motdotla/dotenv/blob/master/lib/main.js#L247

@ryanolson-aumni if you remove your historic code

import dotenv from 'dotenv';

dotenv.config({ path: '.env' });

does the app continue to function correctly? I anticipate it might because next.js is taking care of things.

motdotla commented 6 months ago

@ryanolson-aumni update to 16.4.5. i think it will fix this problem for you. i removed the fs.existsSync checks

https://github.com/motdotla/dotenv/pull/814

ryanolson-aumni commented 6 months ago

Thanks @motdotla. We'll test out removing the import and config call. Thanks for maintaining dotenv. 🎉