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.

I don’t sure if this has been asked before, but can this package access environment variables on the client side? I tried to print the env variable to the console, but it said it was undefined, even though I could see what it printed in the terminal. I know I’m terrible at explaining, but maybe the image i’ve included may help.

bad-at-explaining

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
evanshortisscommented, Sep 12, 2022

This seems to be related with how Next.js handles process.env. It says here in the docs that they inline the value to the browser. They also explicitly state that “dynamic lookups will not be inlined”.

Based on my understanding, this means that if your code is:

const url = process.env.NEXT_PUBLIC_BASE_URL

Next.js compiles it to:

const url = 'http://localhost:3000'

You need to explicitly make a call to process.env.NEXT_PUBLIC_BASE_URL someplace for it to be replaced by their compilation steps.

Using this knowledge I found the following example works when using env-var:

import { from } from 'env-var'

// Pass a custom env object to env-var
const { get } = from({
  NEXT_PUBLIC_BASE_URL: process.env.NEXT_PUBLIC_BASE_URL
})

// Read variables using env-var API
const url = get('NEXT_PUBLIC_BASE_URL').asUrlString()

This is a little convoluted, but it’s a quick workaround I’ve found works with the basic npx create-next-app@latest template.

1reaction
evanshortisscommented, Sep 14, 2022

That’s great! I referenced this issue for the next release. Will add a doc section for this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Client-side - Wikipedia
Client -side refers to operations that are performed by the client in a client–server relationship in a computer network.
Read more >
What Is the Client Side? | Definition by Elementor
The Client-side is everything in a web application displayed or occurring on a client's (end user's) device, including what they see – images,...
Read more >
Client-side web APIs - Learn web development | MDN
When writing client-side JavaScript for web sites or applications, you will quickly encounter Application Programming Interfaces (APIs).
Read more >
Client-side vs. Server-side - Educative.io
Client -side means that the processing takes place on the user's computer. It requires browsers to run the scripts on the client machine...
Read more >
What is Client-side? - Definition from Techopedia
Client -side refers to a specific part of client/server architecture, which is a network structure distinguishing clients or computers ordering information ...
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