Setup app production and developent mode in node.js












0















In my node app, I want to set production and development in my config.js file.



For that I have all most set all thing but I'm still mssing some thing.



I want to get config data like database credential from config fiel based on my development mode. If I upload on live then app will use live cred. On other hand if I used local then it should be use local cred.



module.exports = function () {
console.log("Process env is ::: ", process.env.NODE_ENV);
if (process.env.NODE_ENV == 'production') {
return {
db : {
host:'localhost',
batabase:'dbname',
username:'',
password:''
}
}
} else {
return {
db : {
host:'localhost',
batabase:'dbname',
username:'',
password:''
}
}
}
};


Thanks in advanced. I have taken ref from this answer










share|improve this question




















  • 1





    What’s your question?

    – Mazki516
    Nov 22 '18 at 16:36











  • @Mazki516 I have update my question.

    – khushboo
    Nov 22 '18 at 16:38






  • 1





    @khushboo still I cannot filter out a specific question from this post

    – Kristianmitk
    Nov 22 '18 at 16:41











  • @Kristianmitk I want to set dynamic config values in app. For that I have set this code. So by just one value changed, whole app will use live config data.

    – khushboo
    Nov 22 '18 at 16:44











  • I understand that, but this still does not reveal a question. In other words: whats the reason for this post?

    – Kristianmitk
    Nov 22 '18 at 16:58
















0















In my node app, I want to set production and development in my config.js file.



For that I have all most set all thing but I'm still mssing some thing.



I want to get config data like database credential from config fiel based on my development mode. If I upload on live then app will use live cred. On other hand if I used local then it should be use local cred.



module.exports = function () {
console.log("Process env is ::: ", process.env.NODE_ENV);
if (process.env.NODE_ENV == 'production') {
return {
db : {
host:'localhost',
batabase:'dbname',
username:'',
password:''
}
}
} else {
return {
db : {
host:'localhost',
batabase:'dbname',
username:'',
password:''
}
}
}
};


Thanks in advanced. I have taken ref from this answer










share|improve this question




















  • 1





    What’s your question?

    – Mazki516
    Nov 22 '18 at 16:36











  • @Mazki516 I have update my question.

    – khushboo
    Nov 22 '18 at 16:38






  • 1





    @khushboo still I cannot filter out a specific question from this post

    – Kristianmitk
    Nov 22 '18 at 16:41











  • @Kristianmitk I want to set dynamic config values in app. For that I have set this code. So by just one value changed, whole app will use live config data.

    – khushboo
    Nov 22 '18 at 16:44











  • I understand that, but this still does not reveal a question. In other words: whats the reason for this post?

    – Kristianmitk
    Nov 22 '18 at 16:58














0












0








0








In my node app, I want to set production and development in my config.js file.



For that I have all most set all thing but I'm still mssing some thing.



I want to get config data like database credential from config fiel based on my development mode. If I upload on live then app will use live cred. On other hand if I used local then it should be use local cred.



module.exports = function () {
console.log("Process env is ::: ", process.env.NODE_ENV);
if (process.env.NODE_ENV == 'production') {
return {
db : {
host:'localhost',
batabase:'dbname',
username:'',
password:''
}
}
} else {
return {
db : {
host:'localhost',
batabase:'dbname',
username:'',
password:''
}
}
}
};


Thanks in advanced. I have taken ref from this answer










share|improve this question
















In my node app, I want to set production and development in my config.js file.



For that I have all most set all thing but I'm still mssing some thing.



I want to get config data like database credential from config fiel based on my development mode. If I upload on live then app will use live cred. On other hand if I used local then it should be use local cred.



module.exports = function () {
console.log("Process env is ::: ", process.env.NODE_ENV);
if (process.env.NODE_ENV == 'production') {
return {
db : {
host:'localhost',
batabase:'dbname',
username:'',
password:''
}
}
} else {
return {
db : {
host:'localhost',
batabase:'dbname',
username:'',
password:''
}
}
}
};


Thanks in advanced. I have taken ref from this answer







node.js






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 16:43







khushboo

















asked Nov 22 '18 at 16:34









khushbookhushboo

54114




54114








  • 1





    What’s your question?

    – Mazki516
    Nov 22 '18 at 16:36











  • @Mazki516 I have update my question.

    – khushboo
    Nov 22 '18 at 16:38






  • 1





    @khushboo still I cannot filter out a specific question from this post

    – Kristianmitk
    Nov 22 '18 at 16:41











  • @Kristianmitk I want to set dynamic config values in app. For that I have set this code. So by just one value changed, whole app will use live config data.

    – khushboo
    Nov 22 '18 at 16:44











  • I understand that, but this still does not reveal a question. In other words: whats the reason for this post?

    – Kristianmitk
    Nov 22 '18 at 16:58














  • 1





    What’s your question?

    – Mazki516
    Nov 22 '18 at 16:36











  • @Mazki516 I have update my question.

    – khushboo
    Nov 22 '18 at 16:38






  • 1





    @khushboo still I cannot filter out a specific question from this post

    – Kristianmitk
    Nov 22 '18 at 16:41











  • @Kristianmitk I want to set dynamic config values in app. For that I have set this code. So by just one value changed, whole app will use live config data.

    – khushboo
    Nov 22 '18 at 16:44











  • I understand that, but this still does not reveal a question. In other words: whats the reason for this post?

    – Kristianmitk
    Nov 22 '18 at 16:58








1




1





What’s your question?

– Mazki516
Nov 22 '18 at 16:36





What’s your question?

– Mazki516
Nov 22 '18 at 16:36













@Mazki516 I have update my question.

– khushboo
Nov 22 '18 at 16:38





@Mazki516 I have update my question.

– khushboo
Nov 22 '18 at 16:38




1




1





@khushboo still I cannot filter out a specific question from this post

– Kristianmitk
Nov 22 '18 at 16:41





@khushboo still I cannot filter out a specific question from this post

– Kristianmitk
Nov 22 '18 at 16:41













@Kristianmitk I want to set dynamic config values in app. For that I have set this code. So by just one value changed, whole app will use live config data.

– khushboo
Nov 22 '18 at 16:44





@Kristianmitk I want to set dynamic config values in app. For that I have set this code. So by just one value changed, whole app will use live config data.

– khushboo
Nov 22 '18 at 16:44













I understand that, but this still does not reveal a question. In other words: whats the reason for this post?

– Kristianmitk
Nov 22 '18 at 16:58





I understand that, but this still does not reveal a question. In other words: whats the reason for this post?

– Kristianmitk
Nov 22 '18 at 16:58












2 Answers
2






active

oldest

votes


















1














Just try this way.



module.exports = (function () {
process.env.NODE_ENV='development';
if(process.env.NODE_ENV === 'production'){
// Config data of Live
}else{
//Config data of Local
}
})()


This works for me. :)






share|improve this answer































    0














    process.env refers to the Environment Variables exists at the time you start you nodejs app . (it's part of the os)



    When you deploy to cloud , usually it's handled for you already (process.env.NODE_ENV = production) .
    Some cloud providers even give you the option to control it via a GUI .



    But for local environment , you can use .dotenv package . (https://github.com/motdotla/dotenv)



    With this package you create a .env file at the top of your project ,



    and just write down NODE_ENV = local/staging/production



    Please note that you can always run in shell:



    export NODE_ENV=production


    (WATCH FOR WHITESPACES !)
    before you start you nodejs app which will also give you the effect
    of controlling process.env



    Using the config files in other files, just by requiring it .



    const config = require('path/to/config.js');


    then config.data.host will be changed depend on the NODE_ENV






    share|improve this answer


























    • I just want to use config data in otehr file as in my comment. config.data.database

      – khushboo
      Nov 22 '18 at 17:07











    • i've updated the answer

      – Mazki516
      Nov 22 '18 at 17:12











    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%2f53435103%2fsetup-app-production-and-developent-mode-in-node-js%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Just try this way.



    module.exports = (function () {
    process.env.NODE_ENV='development';
    if(process.env.NODE_ENV === 'production'){
    // Config data of Live
    }else{
    //Config data of Local
    }
    })()


    This works for me. :)






    share|improve this answer




























      1














      Just try this way.



      module.exports = (function () {
      process.env.NODE_ENV='development';
      if(process.env.NODE_ENV === 'production'){
      // Config data of Live
      }else{
      //Config data of Local
      }
      })()


      This works for me. :)






      share|improve this answer


























        1












        1








        1







        Just try this way.



        module.exports = (function () {
        process.env.NODE_ENV='development';
        if(process.env.NODE_ENV === 'production'){
        // Config data of Live
        }else{
        //Config data of Local
        }
        })()


        This works for me. :)






        share|improve this answer













        Just try this way.



        module.exports = (function () {
        process.env.NODE_ENV='development';
        if(process.env.NODE_ENV === 'production'){
        // Config data of Live
        }else{
        //Config data of Local
        }
        })()


        This works for me. :)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 4:38









        Sachin ShahSachin Shah

        1,7561516




        1,7561516

























            0














            process.env refers to the Environment Variables exists at the time you start you nodejs app . (it's part of the os)



            When you deploy to cloud , usually it's handled for you already (process.env.NODE_ENV = production) .
            Some cloud providers even give you the option to control it via a GUI .



            But for local environment , you can use .dotenv package . (https://github.com/motdotla/dotenv)



            With this package you create a .env file at the top of your project ,



            and just write down NODE_ENV = local/staging/production



            Please note that you can always run in shell:



            export NODE_ENV=production


            (WATCH FOR WHITESPACES !)
            before you start you nodejs app which will also give you the effect
            of controlling process.env



            Using the config files in other files, just by requiring it .



            const config = require('path/to/config.js');


            then config.data.host will be changed depend on the NODE_ENV






            share|improve this answer


























            • I just want to use config data in otehr file as in my comment. config.data.database

              – khushboo
              Nov 22 '18 at 17:07











            • i've updated the answer

              – Mazki516
              Nov 22 '18 at 17:12
















            0














            process.env refers to the Environment Variables exists at the time you start you nodejs app . (it's part of the os)



            When you deploy to cloud , usually it's handled for you already (process.env.NODE_ENV = production) .
            Some cloud providers even give you the option to control it via a GUI .



            But for local environment , you can use .dotenv package . (https://github.com/motdotla/dotenv)



            With this package you create a .env file at the top of your project ,



            and just write down NODE_ENV = local/staging/production



            Please note that you can always run in shell:



            export NODE_ENV=production


            (WATCH FOR WHITESPACES !)
            before you start you nodejs app which will also give you the effect
            of controlling process.env



            Using the config files in other files, just by requiring it .



            const config = require('path/to/config.js');


            then config.data.host will be changed depend on the NODE_ENV






            share|improve this answer


























            • I just want to use config data in otehr file as in my comment. config.data.database

              – khushboo
              Nov 22 '18 at 17:07











            • i've updated the answer

              – Mazki516
              Nov 22 '18 at 17:12














            0












            0








            0







            process.env refers to the Environment Variables exists at the time you start you nodejs app . (it's part of the os)



            When you deploy to cloud , usually it's handled for you already (process.env.NODE_ENV = production) .
            Some cloud providers even give you the option to control it via a GUI .



            But for local environment , you can use .dotenv package . (https://github.com/motdotla/dotenv)



            With this package you create a .env file at the top of your project ,



            and just write down NODE_ENV = local/staging/production



            Please note that you can always run in shell:



            export NODE_ENV=production


            (WATCH FOR WHITESPACES !)
            before you start you nodejs app which will also give you the effect
            of controlling process.env



            Using the config files in other files, just by requiring it .



            const config = require('path/to/config.js');


            then config.data.host will be changed depend on the NODE_ENV






            share|improve this answer















            process.env refers to the Environment Variables exists at the time you start you nodejs app . (it's part of the os)



            When you deploy to cloud , usually it's handled for you already (process.env.NODE_ENV = production) .
            Some cloud providers even give you the option to control it via a GUI .



            But for local environment , you can use .dotenv package . (https://github.com/motdotla/dotenv)



            With this package you create a .env file at the top of your project ,



            and just write down NODE_ENV = local/staging/production



            Please note that you can always run in shell:



            export NODE_ENV=production


            (WATCH FOR WHITESPACES !)
            before you start you nodejs app which will also give you the effect
            of controlling process.env



            Using the config files in other files, just by requiring it .



            const config = require('path/to/config.js');


            then config.data.host will be changed depend on the NODE_ENV







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 22 '18 at 17:11

























            answered Nov 22 '18 at 17:02









            Mazki516Mazki516

            6431517




            6431517













            • I just want to use config data in otehr file as in my comment. config.data.database

              – khushboo
              Nov 22 '18 at 17:07











            • i've updated the answer

              – Mazki516
              Nov 22 '18 at 17:12



















            • I just want to use config data in otehr file as in my comment. config.data.database

              – khushboo
              Nov 22 '18 at 17:07











            • i've updated the answer

              – Mazki516
              Nov 22 '18 at 17:12

















            I just want to use config data in otehr file as in my comment. config.data.database

            – khushboo
            Nov 22 '18 at 17:07





            I just want to use config data in otehr file as in my comment. config.data.database

            – khushboo
            Nov 22 '18 at 17:07













            i've updated the answer

            – Mazki516
            Nov 22 '18 at 17:12





            i've updated the answer

            – Mazki516
            Nov 22 '18 at 17:12


















            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%2f53435103%2fsetup-app-production-and-developent-mode-in-node-js%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







            這個網誌中的熱門文章

            Academy of Television Arts & Sciences

            L'Équipe

            1995 France bombings