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

bug: process.env.PORT is undefined initially #56874

Closed
1 task done
juliusmarminge opened this issue Oct 16, 2023 · 3 comments
Closed
1 task done

bug: process.env.PORT is undefined initially #56874

juliusmarminge opened this issue Oct 16, 2023 · 3 comments
Labels
bug Issue was opened via the bug report template. locked

Comments

@juliusmarminge
Copy link
Contributor

juliusmarminge commented Oct 16, 2023

Link to the code that reproduces this issue

https://github.com/juliusmarminge/port

To Reproduce

pnpm i
pnpm dev

will log undefined in console before the actual port 3000 is logged.

with that server running, starting a new dev server in a different terminal will log undefined
before trying the 3001 port and eventually logging that.

lastly, running the dev server with the --port flag set, results in the same behavior, we first get undefined log before 3002:

CleanShot 2023-10-16 at 09 33 03@2x

Current vs. Expected behavior

I want to verify that a URL is correct before starting the server, and want to match the ports:

import { env } from "./src/env.mjs";

const nextAuthPort = new URL(env.NEXTAUTH_URL).port;
if (env.NODE_ENV === "development" && nextAuthPort !== String(env.PORT)) {
  throw new Error(
    [
      `❗ NEXTAUTH_URL (${env.NEXTAUTH_URL}) doesn't match with PORT (${env.PORT}).`,
      `Either update your the URL in your .env file or make sure to run your dev server on port ${nextAuthPort}.`,
    ].join(" "),
  );
}

/** @type {import("next").NextConfig} */
const config = {};

export default config;

Unfortunately, this doesn't work if the port variable is initially undefined. Is there any way you could load this before loading next config, so that the variable isn't undefined to start with?

Verify canary release

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

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.0.0: Fri Sep 15 14:41:43 PDT 2023; root:xnu-10002.1.13~1/RELEASE_ARM64_T6000
Binaries:
  Node: 18.16.0
  npm: 9.5.1
  Yarn: 1.22.19
  pnpm: 8.6.2
Relevant Packages:
  next: 13.5.5-canary.17
  eslint-config-next: N/A
  react: 18.0.0
  react-dom: 18.0.0
  typescript: 5.0.2
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Not sure

Additional context

t3-oss/create-t3-app#1596

@juliusmarminge juliusmarminge added the bug Issue was opened via the bug report template. label Oct 16, 2023
@juliusmarminge juliusmarminge changed the title bug: process.env.PORT is undefined bug: process.env.PORT is undefined initially Oct 16, 2023
@balazsorban44
Copy link
Member

The Next.js config is loaded in different phases, and is likely not the correct place to check this. This has always been the same behavior. (Checked back til v11).

Sidenote, NextAuth.js can infer the host+port so NEXTAUTH_URL isn't even necessary to be set anymore.

@juliusmarminge
Copy link
Contributor Author

The Next.js config is loaded in different phases, and is likely not the correct place to check this. This has always been the same behavior. (Checked back til v11).

Hmm that's a bummer...

Sidenote, NextAuth.js can infer the host+port so NEXTAUTH_URL isn't even necessary to be set anymore.

That doesn't really matter though as the goal was to warn the user that they are doing something unexpected. If I have localhost:3000 set in the oauth app callback it doesn't matter if nextauth can infer that since it's going to cause a 400 request to the auth provider no matter what. We think it's better to be explicit and error early to provide the best developer experience.


Where would be the best place to check this if the Next.js config isn't?

Copy link
Contributor

github-actions bot commented Nov 1, 2023

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot added the locked label Nov 1, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 1, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. locked
Projects
None yet
Development

No branches or pull requests

2 participants