Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 opened this issue Feb 13, 2024 · 28 comments

Comments

@utkarsh621x
Copy link

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
Copy link

sverkoye commented Feb 13, 2024

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
Copy link
Author

I am using node version 20

@Emmo00
Copy link

Emmo00 commented Feb 13, 2024

Found this issue with a similar problem...
Automattic/mongoose#13971

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

@utkarsh621x
Copy link
Author

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
Copy link

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
Copy link

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
Copy link

ryanolson-aumni commented Feb 13, 2024

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
Copy link
Owner

motdotla commented Feb 13, 2024

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
Copy link
Owner

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
Copy link
Author

  1. Upgrade your node to 14 or higher

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

@motdotla
Copy link
Owner

effort happening here: #812

@uniqname
Copy link

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

@motdotla
Copy link
Owner

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
Copy link
Owner

motdotla commented Feb 13, 2024

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?

@motdotla motdotla changed the title Getting error while reinstalling the dotenv Getting error while reinstalling the dotenv (updating to 16.4.4 should fix. apologies for the bad patch release of 16.4.3 everyone) Feb 13, 2024
@utkarsh621x
Copy link
Author

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

@motdotla
Copy link
Owner

great!

@ryanolson-aumni
Copy link

@motdotla It was from 16.4.2 -> 16.4.3.

@ryanolson-aumni
Copy link

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
Copy link
Owner

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

@ryanolson-aumni
Copy link

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
Copy link

magikcypress commented Feb 16, 2024

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
Copy link
Owner

@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
Copy link
Owner

@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
Copy link

ryanolson-aumni commented Feb 16, 2024

@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
Copy link
Owner

here's everything that changed since then: 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
Copy link
Owner

motdotla commented Feb 17, 2024

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
Copy link
Owner

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

#814

@ryanolson-aumni
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants