react evironment variables .env return undefined











up vote
0
down vote

favorite












I am building a react app and i need to fetch data from my api, now i want to store the api url as an environment variable. I have my .env file, i have dotenv installed, here is my code process.env.API_URL is returning undefined.



import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import Home from '../src/components/Home'
import dotenv from 'dotenv'
import path from 'path'


class App extends Component {
render() {
console.log(process.env.API_URL)
return (
<div>
<Home/>
</div>
);
}
}

export default App;









share|improve this question






















  • Are you using Webpack build or create-react-app? Usually, environment variables are loaded as part of build process, usually a Node process. The code you are trying to run above runs in the browser. And .env is not packaged!
    – vijayst
    Nov 10 at 8:35















up vote
0
down vote

favorite












I am building a react app and i need to fetch data from my api, now i want to store the api url as an environment variable. I have my .env file, i have dotenv installed, here is my code process.env.API_URL is returning undefined.



import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import Home from '../src/components/Home'
import dotenv from 'dotenv'
import path from 'path'


class App extends Component {
render() {
console.log(process.env.API_URL)
return (
<div>
<Home/>
</div>
);
}
}

export default App;









share|improve this question






















  • Are you using Webpack build or create-react-app? Usually, environment variables are loaded as part of build process, usually a Node process. The code you are trying to run above runs in the browser. And .env is not packaged!
    – vijayst
    Nov 10 at 8:35













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am building a react app and i need to fetch data from my api, now i want to store the api url as an environment variable. I have my .env file, i have dotenv installed, here is my code process.env.API_URL is returning undefined.



import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import Home from '../src/components/Home'
import dotenv from 'dotenv'
import path from 'path'


class App extends Component {
render() {
console.log(process.env.API_URL)
return (
<div>
<Home/>
</div>
);
}
}

export default App;









share|improve this question













I am building a react app and i need to fetch data from my api, now i want to store the api url as an environment variable. I have my .env file, i have dotenv installed, here is my code process.env.API_URL is returning undefined.



import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import Home from '../src/components/Home'
import dotenv from 'dotenv'
import path from 'path'


class App extends Component {
render() {
console.log(process.env.API_URL)
return (
<div>
<Home/>
</div>
);
}
}

export default App;






reactjs undefined






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 8:31









David whyte

1




1












  • Are you using Webpack build or create-react-app? Usually, environment variables are loaded as part of build process, usually a Node process. The code you are trying to run above runs in the browser. And .env is not packaged!
    – vijayst
    Nov 10 at 8:35


















  • Are you using Webpack build or create-react-app? Usually, environment variables are loaded as part of build process, usually a Node process. The code you are trying to run above runs in the browser. And .env is not packaged!
    – vijayst
    Nov 10 at 8:35
















Are you using Webpack build or create-react-app? Usually, environment variables are loaded as part of build process, usually a Node process. The code you are trying to run above runs in the browser. And .env is not packaged!
– vijayst
Nov 10 at 8:35




Are you using Webpack build or create-react-app? Usually, environment variables are loaded as part of build process, usually a Node process. The code you are trying to run above runs in the browser. And .env is not packaged!
– vijayst
Nov 10 at 8:35












3 Answers
3






active

oldest

votes

















up vote
0
down vote













You will probably need to call dotenv.config() as suggested by the document



If you are using create-react-app, you don't need dotenv package. You will need to add REACT_APP_ prefix to the variable name in .env file. See the document here






share|improve this answer






























    up vote
    0
    down vote













    2 things to note here




    1. the variable should be prefixed with REACT_APP_

    2. You need to restart the server to reflect the changes.






    share|improve this answer




























      up vote
      0
      down vote













      Hey thanks guy what i did and worked was create a config.js file



       const dev={
      API_URL:"http://localhost:300"
      }

      const prod={
      API_URL:"llll"
      }
      const config=process.env.NODE_ENV=='development'?dev:prod
      export default config


      Then i import wherever maybe in a component and get my data.






      share|improve this answer





















        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',
        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%2f53237293%2freact-evironment-variables-env-return-undefined%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        0
        down vote













        You will probably need to call dotenv.config() as suggested by the document



        If you are using create-react-app, you don't need dotenv package. You will need to add REACT_APP_ prefix to the variable name in .env file. See the document here






        share|improve this answer



























          up vote
          0
          down vote













          You will probably need to call dotenv.config() as suggested by the document



          If you are using create-react-app, you don't need dotenv package. You will need to add REACT_APP_ prefix to the variable name in .env file. See the document here






          share|improve this answer

























            up vote
            0
            down vote










            up vote
            0
            down vote









            You will probably need to call dotenv.config() as suggested by the document



            If you are using create-react-app, you don't need dotenv package. You will need to add REACT_APP_ prefix to the variable name in .env file. See the document here






            share|improve this answer














            You will probably need to call dotenv.config() as suggested by the document



            If you are using create-react-app, you don't need dotenv package. You will need to add REACT_APP_ prefix to the variable name in .env file. See the document here







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 10 at 9:01

























            answered Nov 10 at 8:48









            tingxuanz

            365




            365
























                up vote
                0
                down vote













                2 things to note here




                1. the variable should be prefixed with REACT_APP_

                2. You need to restart the server to reflect the changes.






                share|improve this answer

























                  up vote
                  0
                  down vote













                  2 things to note here




                  1. the variable should be prefixed with REACT_APP_

                  2. You need to restart the server to reflect the changes.






                  share|improve this answer























                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    2 things to note here




                    1. the variable should be prefixed with REACT_APP_

                    2. You need to restart the server to reflect the changes.






                    share|improve this answer












                    2 things to note here




                    1. the variable should be prefixed with REACT_APP_

                    2. You need to restart the server to reflect the changes.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 10 at 9:11









                    kiranvj

                    12k23249




                    12k23249






















                        up vote
                        0
                        down vote













                        Hey thanks guy what i did and worked was create a config.js file



                         const dev={
                        API_URL:"http://localhost:300"
                        }

                        const prod={
                        API_URL:"llll"
                        }
                        const config=process.env.NODE_ENV=='development'?dev:prod
                        export default config


                        Then i import wherever maybe in a component and get my data.






                        share|improve this answer

























                          up vote
                          0
                          down vote













                          Hey thanks guy what i did and worked was create a config.js file



                           const dev={
                          API_URL:"http://localhost:300"
                          }

                          const prod={
                          API_URL:"llll"
                          }
                          const config=process.env.NODE_ENV=='development'?dev:prod
                          export default config


                          Then i import wherever maybe in a component and get my data.






                          share|improve this answer























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            Hey thanks guy what i did and worked was create a config.js file



                             const dev={
                            API_URL:"http://localhost:300"
                            }

                            const prod={
                            API_URL:"llll"
                            }
                            const config=process.env.NODE_ENV=='development'?dev:prod
                            export default config


                            Then i import wherever maybe in a component and get my data.






                            share|improve this answer












                            Hey thanks guy what i did and worked was create a config.js file



                             const dev={
                            API_URL:"http://localhost:300"
                            }

                            const prod={
                            API_URL:"llll"
                            }
                            const config=process.env.NODE_ENV=='development'?dev:prod
                            export default config


                            Then i import wherever maybe in a component and get my data.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 12 at 20:36









                            David whyte

                            1




                            1






























                                 

                                draft saved


                                draft discarded



















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237293%2freact-evironment-variables-env-return-undefined%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

                                Guess what letter conforming each word

                                Port of Spain

                                Run scheduled task as local user group (not BUILTIN)