question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Access environment variables in custom server

See original GitHub issue

Feature request

Is your feature request related to a problem? Please describe.

Environment variables are undefined in a custom server. For example in server.js

...
const app = next({dev});
console.log(process.env.MY_ENV_VARIABLE);
...

In next.config.js:

module.exports = {
  env: {
     MY_ENV_VARIABLE: 'hello',
  },
};

Output:

undefined

Describe the solution you’d like

I would like to be able to use environment variable in a custom server.

Describe alternatives you’ve considered

No

Additional context

No

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

22reactions
ferosscommented, Mar 4, 2021

In case it’s helpful to future Google searchers, you can hook into the existing Next.js .env.local, .env, etc. processing by including the following at the top of your custom server:

// Load environment variables from .env, .env.local, etc. This explicit call
// into `@next/env` allows using environment variables before next() is called.
// More info: https://nextjs.org/docs/basic-features/environment-variables
import { loadEnvConfig } from '@next/env'
loadEnvConfig('./', process.env.NODE_ENV !== 'production')
7reactions
rachelgouldcommented, Apr 30, 2020

I’m dealing with this same issue right now

Read more comments on GitHub >

github_iconTop Results From Across the Web

Access environment variables in custom server - vercel/next.js
I would like to be able to use environment variable in a custom server. Describe alternatives you've considered. No. Additional context. No.
Read more >
Basic Features: Environment Variables - Next.js
Learn to add and access environment variables in your Next.js application.
Read more >
Making environment variables accessible to server-side ...
Amplify Hosting supports adding environment variables to your application's builds by setting them in the project's configuration in the Amplify console.
Read more >
Working with Environment Variables in Node.js - Twilio
Environment variables are a great way to configure parts of your Node.js application. Learn how to work with them using helpful tools such ......
Read more >
Setting up environment variables on HTTP Server - IBM
Set up environment variables for CGI programs running in an HTTP Server instance using the IBM ... Click the Custom Environment Variables tab...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found