Giter Club home page Giter Club logo

Comments (6)

evanshortiss avatar evanshortiss commented on May 26, 2024 2

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.

from env-var.

evanshortiss avatar evanshortiss commented on May 26, 2024 1

BTW @ted-dino, your explanation was perfectly good! Let me know if my answer is helpful.

from env-var.

evanshortiss avatar evanshortiss commented on May 26, 2024 1

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

from env-var.

ted-dino avatar ted-dino commented on May 26, 2024

worked like a charm. thank you @evanshortiss

from env-var.

eparizzi-clevertech avatar eparizzi-clevertech commented on May 26, 2024

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

Has this doc section been added somewhere already? I'd like to see a full working example for Next.js.

from env-var.

evanshortiss avatar evanshortiss commented on May 26, 2024

@eparizzi-clevertech I have some examples in the branch for the planned v8 release. Please take a look here and let me know if it's sufficient. Thanks!

from env-var.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.