Next.js Discord

Discord Forum

Accessing env variable defined in next.config.js

Answered
GetPsyched posted this in #help-forum
Open in Discord
I have defined my next config as:
/** @type {import('next').NextConfig} */
module.exports = {
  reactStrictMode: true,
  experimental: { appDir: true },
  env: {
    breadboardURL: 'https://breadboard.up.railway.app',
  },
};

I am trying to use this env in a fetcher utility as:
export const fetcher = async (url: URL | RequestInfo) => {
  const response = await fetch(process.env.breadboardURL + url);
  const responseJson = await response.json();
  // snip
Answered by riský
just put a ! at the end of the name to make it quiet: process.env.NEXT_PUBLIC_BREADBOARD_URL!
View full answer

66 Replies

I have also tried using .env.local:
export const fetcher = async (url: URL | RequestInfo) => {
  const response = await fetch(process.env.NEXT_PUBLIC_BREADBOARD_URL + url);
  const responseJson = await response.json();
  // snip
The linter says process is an undefined variable
Cannot find name 'process'. Do you need to install type definitions for node? Try npm i --save-dev @types/node.
is NEXT_PUBLIC_BREADBOARD_URL defined in your .env file...
ahh you are getting TS errors
bruh. I didn't have node_modules in its entirety
well that would make ts angry...
yeah, me as well. wtf am I doing lmao
so install it?
yeah it's downloading shit
nothing in the cache because this is my first time developing web stuff after formatting my OS
@riský and you have installed said types?
is this because I only have .env.local and not .env.production?
because I have it defined in .env.local
just put a ! at the end of the name to make it quiet: process.env.NEXT_PUBLIC_BREADBOARD_URL!
Answer
but you should actualy validate that it exists...
:blob_thonkang:
I don't like the idea of making my linter shut up. Sounds like a recipe for disaster
like make a env.ts file that exports the envs after making sure that they exist (and corectly types them as existent)
oh that's the problem?
like this is a popular one: https://env.t3.gg/
@GetPsyched oh that's the problem?
yeah...
@GetPsyched I don't like the idea of making my linter shut up. Sounds like a recipe for disaster
it isn't making it shut up, just telling it that the variable is defined (should only use when you can fully know tho)
I see
Thanks
@GetPsyched Thanks
so it works now 🎉
Yeah, I didn't want to go as far as using a env.ts
i would say in the long term, it is a good investment (as it is a good place to see what is required to set / should set)
Fair enough
I'll do it later as a refactor
I'm just trying to push out basic functionality rn without sacrificing code quality too much
uh I'm still getting these errors in my console even though the URL is fine:
error - TypeError: Failed to parse URL from /clubs
    at Object.fetch (node:internal/deps/undici/undici:11576:11) {
  type: 'TypeError',
  page: '/clubs/Technobyte'
}
error - TypeError: Failed to parse URL from /clubs
    at Object.fetch (node:internal/deps/undici/undici:11576:11) {
  type: 'TypeError',
  page: '/clubs/Microbus'
}
you shouldn't be using relative urls for server fetch
I printed the URL it's fetching and it seems to be fine
o.O
@riský you shouldn't be using relative urls for server fetch
it's not relative though. I'm just passing it to my fetcher util
i just saw
// fetcher.ts
export const fetcher = async (url: URL | RequestInfo) => {
  console.log(process.env.NEXT_PUBLIC_BREADBOARD_URL! + url);
  const response = await fetch(process.env.NEXT_PUBLIC_BREADBOARD_URL! + url);
  const responseJson = await response.json();

  if (!response.ok) {
    const error = new Error('An error has occured while fetching data.');
    error.cause = responseJson.errors;
    throw error;
  }

  return responseJson.data;
};
and I call it like: const club: Club = await fetcher(``/clubs/${name}``);
does NEXT_PUBLIC_BREADBOARD_URL end in a /?
nope
@GetPsyched I printed the URL it's fetching and it seems to be fine
you can see the resolved URL here
Wait I think I might have removed the full length from somewhere that's not using the fetcher util
  const {
    data: courses,
    isLoading,
    error,
  }: { data: Course[] | undefined; isLoading: boolean; error: any } = useSWR(
    '/courses',
    fetcher
  );
Found it
nvm still getting the error
nextjs should really give me the traceback of the issue
Ah found it now: const clubsResponse = await fetch('/clubs');
hmm why am I not using my util here
you can throw an error and see the trace path maybe - actually it could get bundled, so all in one place
hmm
maybe not in dev mode
🤷‍♂️
@GetPsyched Ah found it now: `const clubsResponse = await fetch('/clubs');`
anyways, that fixed everything once and for all
🤞
Worked finally
nice
ok some other unrelated stuff broke
ooof, didn't realise I was using fetcher within useSWR
you seem to be having fun here :this_is_fine:
:fine:
I have come back to this project after months
So I'm quite rusty about what code does what and how
npm WARN config production Use --omit=dev instead.
uh?