next-auth
next-auth copied to clipboard
Vercel development env crashes startup
Environment
System: OS: macOS 12.3.1 CPU: (10) arm64 Apple M1 Max Memory: 2.65 GB / 32.00 GB Shell: 5.8 - /bin/zsh Binaries: Node: 14.19.1 - ~/.asdf/installs/nodejs/14.19.1/bin/node npm: 8.6.0 - ~/.asdf/plugins/nodejs/shims/npm Browsers: Firefox: 98.0.2 Safari: 15.4 npmPackages: next: ^12.1.4 => 12.1.4 next-auth: ^4.3.1 => 4.3.1 react: ^17.0.2 => 17.0.2
Reproduction URL
https://github.com/reconbot/next-auth-example
Describe the issue
When you run vercel pull you get empty vercel env vars which crashes next on startup
# .env Created by Vercel CLI
VERCEL="1"
VERCEL_ENV="development"
VERCEL_URL=""
VERCEL_GIT_PROVIDER=""
VERCEL_GIT_REPO_SLUG=""
VERCEL_GIT_REPO_OWNER=""
VERCEL_GIT_REPO_ID=""
VERCEL_GIT_COMMIT_REF=""
VERCEL_GIT_COMMIT_SHA=""
VERCEL_GIT_COMMIT_MESSAGE=""
VERCEL_GIT_COMMIT_AUTHOR_LOGIN=""
VERCEL_GIT_COMMIT_AUTHOR_NAME=""
By process of elimitation the only problem line is
VERCEL_URL=""
which causes errors because ?? doesn't behave as if the env var is missing.
The error is
TypeError [ERR_INVALID_URL]: Invalid URL
at new NodeError (node:internal/errors:371:5)
at onParseError (node:internal/url:552:9)
at new URL (node:internal/url:628:5)
at parseUrl (/Users/wizard/src/next-auth-example/node_modules/next-auth/lib/parse-url.js:18:16)
at Object.<anonymous> (/Users/wizard/src/next-auth-example/node_modules/next-auth/react/index.js:70:34)
at Module._compile (node:internal/modules/cjs/loader:1103:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19) {
The error seems to be coming from parseUrl which might be from this call or this call. We use ?? a lot of places but if an env var is set but an empty string that doesn't do the desired behavior.
How to reproduce
Run in order
git clone https://github.com/reconbot/next-auth-example # cone the repo
cd next-auth-example
vercel # go ahead and create a project and deploy it
# double check that `System Environment Variables` is enabled
vercel pull
npm i
npm run dev
Expected behavior
It doesn't crash
exactly what i have this morning, my current work around is set the vercel url to the localhost ie: VERCEL_URL = "http://localhost:3000"
You can just remove it too.
So I'm not sure there is action on our side here. An empty string "" is obviously an invalid URL, as the error shows. :thinking:. It doesn't really make sense for us to not consider it as a configuration issue from the user.
As a user I have no control over vercel adding these empty string to my local env. This empty string vs undefined issue is old as time for every language and environment. Best to be friendly here imho.
So I did merge the fix, but I realized it won't be a real fix, because of the order of how we fall back on environment variables. If you check https://github.com/nextauthjs/next-auth/pull/4443/files, we already fell back to VERCEL_URL.
maybe this is something to be addressed in the docs (or via a better error message).
I, too, did a vercel env pull and because of the empty VERCEL_URL experienced this error. all I had to do was remove this line.
perhaps:
- [ ] add a reference to this in https://next-auth.js.org/errors
- [ ] ~update the message (especially for the
VERCEL_URL == ''case) to instruct users to remove the offending line :)~
I'm not sure I agree there's an offending line, one that's produced by the build and hosting tooling and not the user. (If they remove it, it will come back)
But handle the condition 💯
An empty string "" is obviously an invalid URL
For an arbitrary input, true. But VERCEL_URL has it's own semantics, which is that it's always set and, in development, it's an empty string (or empty if you don't pull env vars at all). Whether or not that's a valid url isn't something I think is relevant. If we're going to support VERCEL_URL, the semantics come from vercel, which means that an empty string should be valid. If that's unacceptable, then support for VERCEL_URL should be removed entirely, which doesn't seem like a good path forward.
An empty string "" is obviously an invalid URL
In addition to @saiichihashimoto's answer (which I agree with):
undefinedornullare 'obviously' invalid URLs too. Yet, we handle them with default URLs.- JavaScript itself considers empty strings closely related to
undefinedornull. Following this lead by considering using default URLs on empty URLs would be consistent.
I created the PR #9846, hoping you guys agree with this train of thought. It's a simple one liner which, if I don't miss anything, could help disproportionally.