Comments (6)
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.
BTW @ted-dino, your explanation was perfectly good! Let me know if my answer is helpful.
from env-var.
That's great! I referenced this issue for the next release. Will add a doc section for this issue.
from env-var.
worked like a charm. thank you @evanshortiss
from env-var.
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.
@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)
- .asUrlString() attaches "/" to url HOT 4
- Typo in repo description 'sanatization' HOT 1
- Feature request: asRegExp() HOT 4
- Add 'example/README.md' HOT 4
- Add description() function HOT 3
- The number accessors are too permissive HOT 3
- asEnum should accept readonly T[] HOT 3
- add asMailString() HOT 4
- Configure default behavior / Support dotenv-defaults HOT 5
- Duplicate identifier causing broken build with typescript. HOT 3
- Duplicate identifier 'EnvVarError'. HOT 4
- Deno Support HOT 3
- TypeError: env.get is not a function HOT 3
- Don't default export `from(process.env)` for environments that don't have `process` HOT 6
- v8.0.0 HOT 1
- Custom validation extension/custom accessor before `as` check. HOT 2
- Required() in React HOT 1
- Allow asUrlString (and maybe other checks) to be optional HOT 2
- 7.4.0 is breaking create-react-app projects that worked in prior versions HOT 11
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from env-var.