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

nextjs Failed to load env TypeError: Cannot read properties of undefined (reading 'split') #36691

Open
1 task done
chrishj59 opened this issue May 4, 2022 · 3 comments
Open
1 task done
Labels
Upstream Related to using Next.js with a third-party dependency. (e.g., React, UI/icon libraries, etc.).

Comments

@chrishj59
Copy link

chrishj59 commented May 4, 2022

Verify canary release

  • I verified that the issue exists in Next.js canary release

Provide environment information

Operating System:
Platform: darwin
Arch: x64
Version: Darwin Kernel Version 21.4.0: Fri Mar 18 00:45:05 PDT 2022; root:xnu-8020.101.4~15/RELEASE_X86_64
Binaries:
Node: 16.13.1
npm: 7.24.2
Yarn: 1.22.15
pnpm: 6.11.0
Relevant packages:
next: 12.1.7-canary.1
react: 17.0.2
react-dom: 17.0.2

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

Executing on local laptop

Describe the Bug

On starting next with the command npm run dev ("dev": "next") the error message is show:
error - Failed to load env from /Users/path/.env.development.local TypeError: Cannot read properties of undefined (reading 'split')

The simple .env file has
SH_API_BASEURL='http://localhost:3010/api/v1'
NEXT_PUBLIC_SH_API_BASEURL = 'http://localhost:3010/api/v1'
NEXTAUTH_URL=http://localhost:3000
SECRET_COOKIE_PASSWORD=someText
NEXTAUTH_SECRET=AY_PqQ/gmFH6L@:z{szxm.2;~xN:RfD;Rwb`%c/$,/$sometext
JWT_SIGNING_PRIVATE_KEY=&/$someText

If I remove the $ from the keys the .env file will load so the issue is when there is $ in the keys. As this was possible in 12.0.6 it should be possible in the current version.

Expected Behavior

Next 12.0.6 is able to load the .env file without an issue but the current fails to load the .env.
I expect 12.1.16 or 12.1.7-canary.1 to be able to parse the .env file with $ in the value

To Reproduce

To reproduce run npm i next

@chrishj59 chrishj59 added the bug Issue was opened via the bug report template. label May 4, 2022
@balazsorban44
Copy link
Member

balazsorban44 commented May 5, 2022

This seems to be an upstream issue, the current recommendation is to escape the $: motdotla/dotenv-expand#65 (comment)

dotenv-expand has been upgraded in #34765

I noticed this similar thread: #35818

@balazsorban44 balazsorban44 added Upstream Related to using Next.js with a third-party dependency. (e.g., React, UI/icon libraries, etc.). kind: bug and removed bug Issue was opened via the bug report template. labels May 5, 2022
@c0ntax
Copy link

c0ntax commented May 27, 2022

@balazsorban44 I'm not sure that this is 100% a duplicate of #37207 . I've encountered the same issue and the problem is that the dollar sign is the last char in the string. Even when you correctly escape it, it still throws the same error.

e.g

The following will throw the above error

VAR=something$
VAR=something\$

The following will not throw the error

VAR=something$anything
VAR=something\$anything

@balazsorban44
Copy link
Member

The current workaround is based on this comment: motdotla/dotenv-expand#65 (comment) which specifically mentions the escaping of $ character as an ending.

I verified this in both a Next.js and non-Next.js reproduction.

// index.js
import dotenv from "dotenv"
import dotenvExpand from "dotenv-expand"

const myEnv = dotenv.config()
dotenvExpand.expand(myEnv)

console.log(process.env)
# .env
VAR=something$ # throws the error
VAR=something\$ # does not throw the error

So since escaping the last $ works, #37207 seems to be caused by the same issue as this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Upstream Related to using Next.js with a third-party dependency. (e.g., React, UI/icon libraries, etc.).
Projects
None yet
Development

No branches or pull requests

4 participants