Next.js pass NODE_ENV to client










0















I'm building a React SSR App, using Next.js.



I want to be able to access the NODE_ENV on the client side, as this will tell my app which API endpoints to use.



I'm struggling to find a decent approach for this. I'd like to define the NODE_ENV as a window variable when I first render the page on the server, and then in my helper function where I make the API call, I would check if the code is being called on the server or the client, and using the window or process.env variables as required.



Does anyone have a good solution for such a problem. It must be a common issue but I can't find any good solutions.










share|improve this question






















  • Have you tried using the DefinePlugin?

    – Tholle
    Nov 12 '18 at 17:09











  • You can include that to window.__INITIAL_STATE__ = $ );

    – Seongjun Kim
    Nov 12 '18 at 22:29















0















I'm building a React SSR App, using Next.js.



I want to be able to access the NODE_ENV on the client side, as this will tell my app which API endpoints to use.



I'm struggling to find a decent approach for this. I'd like to define the NODE_ENV as a window variable when I first render the page on the server, and then in my helper function where I make the API call, I would check if the code is being called on the server or the client, and using the window or process.env variables as required.



Does anyone have a good solution for such a problem. It must be a common issue but I can't find any good solutions.










share|improve this question






















  • Have you tried using the DefinePlugin?

    – Tholle
    Nov 12 '18 at 17:09











  • You can include that to window.__INITIAL_STATE__ = $ );

    – Seongjun Kim
    Nov 12 '18 at 22:29













0












0








0








I'm building a React SSR App, using Next.js.



I want to be able to access the NODE_ENV on the client side, as this will tell my app which API endpoints to use.



I'm struggling to find a decent approach for this. I'd like to define the NODE_ENV as a window variable when I first render the page on the server, and then in my helper function where I make the API call, I would check if the code is being called on the server or the client, and using the window or process.env variables as required.



Does anyone have a good solution for such a problem. It must be a common issue but I can't find any good solutions.










share|improve this question














I'm building a React SSR App, using Next.js.



I want to be able to access the NODE_ENV on the client side, as this will tell my app which API endpoints to use.



I'm struggling to find a decent approach for this. I'd like to define the NODE_ENV as a window variable when I first render the page on the server, and then in my helper function where I make the API call, I would check if the code is being called on the server or the client, and using the window or process.env variables as required.



Does anyone have a good solution for such a problem. It must be a common issue but I can't find any good solutions.







javascript node.js reactjs environment-variables next.js






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 '18 at 16:59









Jack WildJack Wild

69611324




69611324












  • Have you tried using the DefinePlugin?

    – Tholle
    Nov 12 '18 at 17:09











  • You can include that to window.__INITIAL_STATE__ = $ );

    – Seongjun Kim
    Nov 12 '18 at 22:29

















  • Have you tried using the DefinePlugin?

    – Tholle
    Nov 12 '18 at 17:09











  • You can include that to window.__INITIAL_STATE__ = $ );

    – Seongjun Kim
    Nov 12 '18 at 22:29
















Have you tried using the DefinePlugin?

– Tholle
Nov 12 '18 at 17:09





Have you tried using the DefinePlugin?

– Tholle
Nov 12 '18 at 17:09













You can include that to window.__INITIAL_STATE__ = $ );

– Seongjun Kim
Nov 12 '18 at 22:29





You can include that to window.__INITIAL_STATE__ = $ );

– Seongjun Kim
Nov 12 '18 at 22:29












1 Answer
1






active

oldest

votes


















1














1. You can include it in webpack configuration (using dotenv-webpack dependency):






require('dotenv').config()

const path = require('path')
const Dotenv = require('dotenv-webpack')

module.exports =
webpack: (config) =>





Reference: here




2. using babel plugin to import the variable towards the entire app:



env-config.js



const prod = process.env.NODE_ENV === 'production'

module.exports =
'process.env.BACKEND_URL': prod ? 'https://api.example.com' : 'https://localhost:8080'



.babelrc.js



const env = require('./env-config.js')

module.exports =
presets: ['next/babel'],
plugins: [['transform-define', env]]



index.js



export default () => (
<div>Loading data from process.env.BACKEND_URL </div>
)


Reference: here



3. Using next/config:



next.config.js



module.exports = 
publicRuntimeConfig:
API_URL: process.env.API_URL




index.js



import React from 'react'
import getConfig from 'next/config'

const publicRuntimeConfig = getConfig()
const API_URL = publicRuntimeConfig

export default class extends React.Component
static async getInitialProps ()
// fetch(`$API_URL/some-path`)
return


render ()
return <div>
The API_URL is API_URL
</div>




Reference: here






share|improve this answer

























  • Thanks, this is an extremely helpful answer!

    – Jack Wild
    Nov 14 '18 at 12:26










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53266814%2fnext-js-pass-node-env-to-client%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














1. You can include it in webpack configuration (using dotenv-webpack dependency):






require('dotenv').config()

const path = require('path')
const Dotenv = require('dotenv-webpack')

module.exports =
webpack: (config) =>





Reference: here




2. using babel plugin to import the variable towards the entire app:



env-config.js



const prod = process.env.NODE_ENV === 'production'

module.exports =
'process.env.BACKEND_URL': prod ? 'https://api.example.com' : 'https://localhost:8080'



.babelrc.js



const env = require('./env-config.js')

module.exports =
presets: ['next/babel'],
plugins: [['transform-define', env]]



index.js



export default () => (
<div>Loading data from process.env.BACKEND_URL </div>
)


Reference: here



3. Using next/config:



next.config.js



module.exports = 
publicRuntimeConfig:
API_URL: process.env.API_URL




index.js



import React from 'react'
import getConfig from 'next/config'

const publicRuntimeConfig = getConfig()
const API_URL = publicRuntimeConfig

export default class extends React.Component
static async getInitialProps ()
// fetch(`$API_URL/some-path`)
return


render ()
return <div>
The API_URL is API_URL
</div>




Reference: here






share|improve this answer

























  • Thanks, this is an extremely helpful answer!

    – Jack Wild
    Nov 14 '18 at 12:26















1














1. You can include it in webpack configuration (using dotenv-webpack dependency):






require('dotenv').config()

const path = require('path')
const Dotenv = require('dotenv-webpack')

module.exports =
webpack: (config) =>





Reference: here




2. using babel plugin to import the variable towards the entire app:



env-config.js



const prod = process.env.NODE_ENV === 'production'

module.exports =
'process.env.BACKEND_URL': prod ? 'https://api.example.com' : 'https://localhost:8080'



.babelrc.js



const env = require('./env-config.js')

module.exports =
presets: ['next/babel'],
plugins: [['transform-define', env]]



index.js



export default () => (
<div>Loading data from process.env.BACKEND_URL </div>
)


Reference: here



3. Using next/config:



next.config.js



module.exports = 
publicRuntimeConfig:
API_URL: process.env.API_URL




index.js



import React from 'react'
import getConfig from 'next/config'

const publicRuntimeConfig = getConfig()
const API_URL = publicRuntimeConfig

export default class extends React.Component
static async getInitialProps ()
// fetch(`$API_URL/some-path`)
return


render ()
return <div>
The API_URL is API_URL
</div>




Reference: here






share|improve this answer

























  • Thanks, this is an extremely helpful answer!

    – Jack Wild
    Nov 14 '18 at 12:26













1












1








1







1. You can include it in webpack configuration (using dotenv-webpack dependency):






require('dotenv').config()

const path = require('path')
const Dotenv = require('dotenv-webpack')

module.exports =
webpack: (config) =>





Reference: here




2. using babel plugin to import the variable towards the entire app:



env-config.js



const prod = process.env.NODE_ENV === 'production'

module.exports =
'process.env.BACKEND_URL': prod ? 'https://api.example.com' : 'https://localhost:8080'



.babelrc.js



const env = require('./env-config.js')

module.exports =
presets: ['next/babel'],
plugins: [['transform-define', env]]



index.js



export default () => (
<div>Loading data from process.env.BACKEND_URL </div>
)


Reference: here



3. Using next/config:



next.config.js



module.exports = 
publicRuntimeConfig:
API_URL: process.env.API_URL




index.js



import React from 'react'
import getConfig from 'next/config'

const publicRuntimeConfig = getConfig()
const API_URL = publicRuntimeConfig

export default class extends React.Component
static async getInitialProps ()
// fetch(`$API_URL/some-path`)
return


render ()
return <div>
The API_URL is API_URL
</div>




Reference: here






share|improve this answer















1. You can include it in webpack configuration (using dotenv-webpack dependency):






require('dotenv').config()

const path = require('path')
const Dotenv = require('dotenv-webpack')

module.exports =
webpack: (config) =>





Reference: here




2. using babel plugin to import the variable towards the entire app:



env-config.js



const prod = process.env.NODE_ENV === 'production'

module.exports =
'process.env.BACKEND_URL': prod ? 'https://api.example.com' : 'https://localhost:8080'



.babelrc.js



const env = require('./env-config.js')

module.exports =
presets: ['next/babel'],
plugins: [['transform-define', env]]



index.js



export default () => (
<div>Loading data from process.env.BACKEND_URL </div>
)


Reference: here



3. Using next/config:



next.config.js



module.exports = 
publicRuntimeConfig:
API_URL: process.env.API_URL




index.js



import React from 'react'
import getConfig from 'next/config'

const publicRuntimeConfig = getConfig()
const API_URL = publicRuntimeConfig

export default class extends React.Component
static async getInitialProps ()
// fetch(`$API_URL/some-path`)
return


render ()
return <div>
The API_URL is API_URL
</div>




Reference: here






require('dotenv').config()

const path = require('path')
const Dotenv = require('dotenv-webpack')

module.exports =
webpack: (config) =>





require('dotenv').config()

const path = require('path')
const Dotenv = require('dotenv-webpack')

module.exports =
webpack: (config) =>






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 1:43

























answered Nov 13 '18 at 1:34









Darryl R. NorbertDarryl R. Norbert

6401315




6401315












  • Thanks, this is an extremely helpful answer!

    – Jack Wild
    Nov 14 '18 at 12:26

















  • Thanks, this is an extremely helpful answer!

    – Jack Wild
    Nov 14 '18 at 12:26
















Thanks, this is an extremely helpful answer!

– Jack Wild
Nov 14 '18 at 12:26





Thanks, this is an extremely helpful answer!

– Jack Wild
Nov 14 '18 at 12:26

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53266814%2fnext-js-pass-node-env-to-client%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo