Load .env environment variables when running npm task









up vote
1
down vote

favorite












Let's say we have a .env file with some variables specified:



AWS_PROFILE=hsz
ENVIRONMENT=development


There is also a simple npm task defined:




"name": "project",
"version": "0.0.1",
"scripts":
"deploy": "sls deploy"




But runnning npm run deploy ignores our .env definition.



It can be resolved with better-npm-run like:




"name": "project",
"version": "0.0.2",
"scripts":
"deploy": "bnr deploy"
,
"betterScripts":
"deploy": "sls deploy"
,
"devDependencies":
"better-npm-run": "^0.1.1",




but this looks like an overhead - especially when we have 10+ tasks.



Is there a better way to always load .env without proxying all tasks via better-npm-run?










share|improve this question





















  • any comments/feedback regarding my answer? @hsz
    – mihai
    Nov 23 at 18:01














up vote
1
down vote

favorite












Let's say we have a .env file with some variables specified:



AWS_PROFILE=hsz
ENVIRONMENT=development


There is also a simple npm task defined:




"name": "project",
"version": "0.0.1",
"scripts":
"deploy": "sls deploy"




But runnning npm run deploy ignores our .env definition.



It can be resolved with better-npm-run like:




"name": "project",
"version": "0.0.2",
"scripts":
"deploy": "bnr deploy"
,
"betterScripts":
"deploy": "sls deploy"
,
"devDependencies":
"better-npm-run": "^0.1.1",




but this looks like an overhead - especially when we have 10+ tasks.



Is there a better way to always load .env without proxying all tasks via better-npm-run?










share|improve this question





















  • any comments/feedback regarding my answer? @hsz
    – mihai
    Nov 23 at 18:01












up vote
1
down vote

favorite









up vote
1
down vote

favorite











Let's say we have a .env file with some variables specified:



AWS_PROFILE=hsz
ENVIRONMENT=development


There is also a simple npm task defined:




"name": "project",
"version": "0.0.1",
"scripts":
"deploy": "sls deploy"




But runnning npm run deploy ignores our .env definition.



It can be resolved with better-npm-run like:




"name": "project",
"version": "0.0.2",
"scripts":
"deploy": "bnr deploy"
,
"betterScripts":
"deploy": "sls deploy"
,
"devDependencies":
"better-npm-run": "^0.1.1",




but this looks like an overhead - especially when we have 10+ tasks.



Is there a better way to always load .env without proxying all tasks via better-npm-run?










share|improve this question













Let's say we have a .env file with some variables specified:



AWS_PROFILE=hsz
ENVIRONMENT=development


There is also a simple npm task defined:




"name": "project",
"version": "0.0.1",
"scripts":
"deploy": "sls deploy"




But runnning npm run deploy ignores our .env definition.



It can be resolved with better-npm-run like:




"name": "project",
"version": "0.0.2",
"scripts":
"deploy": "bnr deploy"
,
"betterScripts":
"deploy": "sls deploy"
,
"devDependencies":
"better-npm-run": "^0.1.1",




but this looks like an overhead - especially when we have 10+ tasks.



Is there a better way to always load .env without proxying all tasks via better-npm-run?







node.js npm environment-variables






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 6:48









hsz

104k48200269




104k48200269











  • any comments/feedback regarding my answer? @hsz
    – mihai
    Nov 23 at 18:01
















  • any comments/feedback regarding my answer? @hsz
    – mihai
    Nov 23 at 18:01















any comments/feedback regarding my answer? @hsz
– mihai
Nov 23 at 18:01




any comments/feedback regarding my answer? @hsz
– mihai
Nov 23 at 18:01












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










A bit ugly, but you could try something like this:



"scripts": 
"deploy": "export $(cat .env


This will export all environment variables from the .env file before running sls deploy.



There are some variations to this tehnique in this answer.



Not very clean but it avoids usage of an extra module.






share|improve this answer



























    up vote
    0
    down vote













    Maintain and load all your environment specific configuration in project itself.



    dev.js



    module.exports = 
    "host":"dev.com"



    prod.js



    module.exports = 
    "host":"prod.com"



    config.js - main file that will resolve configuration based on process.env.ENV variable.



    const dev = require('./dev');
    const prod = require('./prod');
    let envObject = ;
    const env = process.env.ENV || "dev";
    switch(env)
    case 'prod':
    envObject = prod;
    break;
    default:
    envObject = dev;

    envObject['ENV'] = env;
    process.env = Object.assign(process.env,envObject); // Optional if you prefer to add them into process environment otherwise `require('./config')` where you need configuration.
    module.exports = envObject;


    index.js - node project root file call every time when project start



    const config = require('./config');
    console.log('config object => ',config.host);


    package.json




    "name": "project",
    "version": "0.0.2",
    "scripts":
    "deploy": "sls deploy"




    Running you node.js code
    Prod environment ENV=prod npm run deploy;
    Development environment - npm run deploy;



    Default environment is set to dev in ./config.js



    Using this simple practice you don't need any npm module to manage your environment configurations.






    share|improve this answer




















    • Thanks, but the point is to avoid prefixing npm run with any env variables; also in case of running sls deploy - external tool - no index.js is loaded at all.
      – hsz
      Nov 9 at 8:31










    • Or create a .npmrc file inside the project root folder and set variable there. docs.npmjs.com/files/npmrc#files
      – front_end_dev
      Nov 9 at 8:46










    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%2f53221045%2fload-env-environment-variables-when-running-npm-task%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








    up vote
    1
    down vote



    accepted










    A bit ugly, but you could try something like this:



    "scripts": 
    "deploy": "export $(cat .env


    This will export all environment variables from the .env file before running sls deploy.



    There are some variations to this tehnique in this answer.



    Not very clean but it avoids usage of an extra module.






    share|improve this answer
























      up vote
      1
      down vote



      accepted










      A bit ugly, but you could try something like this:



      "scripts": 
      "deploy": "export $(cat .env


      This will export all environment variables from the .env file before running sls deploy.



      There are some variations to this tehnique in this answer.



      Not very clean but it avoids usage of an extra module.






      share|improve this answer






















        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        A bit ugly, but you could try something like this:



        "scripts": 
        "deploy": "export $(cat .env


        This will export all environment variables from the .env file before running sls deploy.



        There are some variations to this tehnique in this answer.



        Not very clean but it avoids usage of an extra module.






        share|improve this answer












        A bit ugly, but you could try something like this:



        "scripts": 
        "deploy": "export $(cat .env


        This will export all environment variables from the .env file before running sls deploy.



        There are some variations to this tehnique in this answer.



        Not very clean but it avoids usage of an extra module.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 10:21









        mihai

        23.1k73968




        23.1k73968






















            up vote
            0
            down vote













            Maintain and load all your environment specific configuration in project itself.



            dev.js



            module.exports = 
            "host":"dev.com"



            prod.js



            module.exports = 
            "host":"prod.com"



            config.js - main file that will resolve configuration based on process.env.ENV variable.



            const dev = require('./dev');
            const prod = require('./prod');
            let envObject = ;
            const env = process.env.ENV || "dev";
            switch(env)
            case 'prod':
            envObject = prod;
            break;
            default:
            envObject = dev;

            envObject['ENV'] = env;
            process.env = Object.assign(process.env,envObject); // Optional if you prefer to add them into process environment otherwise `require('./config')` where you need configuration.
            module.exports = envObject;


            index.js - node project root file call every time when project start



            const config = require('./config');
            console.log('config object => ',config.host);


            package.json




            "name": "project",
            "version": "0.0.2",
            "scripts":
            "deploy": "sls deploy"




            Running you node.js code
            Prod environment ENV=prod npm run deploy;
            Development environment - npm run deploy;



            Default environment is set to dev in ./config.js



            Using this simple practice you don't need any npm module to manage your environment configurations.






            share|improve this answer




















            • Thanks, but the point is to avoid prefixing npm run with any env variables; also in case of running sls deploy - external tool - no index.js is loaded at all.
              – hsz
              Nov 9 at 8:31










            • Or create a .npmrc file inside the project root folder and set variable there. docs.npmjs.com/files/npmrc#files
              – front_end_dev
              Nov 9 at 8:46














            up vote
            0
            down vote













            Maintain and load all your environment specific configuration in project itself.



            dev.js



            module.exports = 
            "host":"dev.com"



            prod.js



            module.exports = 
            "host":"prod.com"



            config.js - main file that will resolve configuration based on process.env.ENV variable.



            const dev = require('./dev');
            const prod = require('./prod');
            let envObject = ;
            const env = process.env.ENV || "dev";
            switch(env)
            case 'prod':
            envObject = prod;
            break;
            default:
            envObject = dev;

            envObject['ENV'] = env;
            process.env = Object.assign(process.env,envObject); // Optional if you prefer to add them into process environment otherwise `require('./config')` where you need configuration.
            module.exports = envObject;


            index.js - node project root file call every time when project start



            const config = require('./config');
            console.log('config object => ',config.host);


            package.json




            "name": "project",
            "version": "0.0.2",
            "scripts":
            "deploy": "sls deploy"




            Running you node.js code
            Prod environment ENV=prod npm run deploy;
            Development environment - npm run deploy;



            Default environment is set to dev in ./config.js



            Using this simple practice you don't need any npm module to manage your environment configurations.






            share|improve this answer




















            • Thanks, but the point is to avoid prefixing npm run with any env variables; also in case of running sls deploy - external tool - no index.js is loaded at all.
              – hsz
              Nov 9 at 8:31










            • Or create a .npmrc file inside the project root folder and set variable there. docs.npmjs.com/files/npmrc#files
              – front_end_dev
              Nov 9 at 8:46












            up vote
            0
            down vote










            up vote
            0
            down vote









            Maintain and load all your environment specific configuration in project itself.



            dev.js



            module.exports = 
            "host":"dev.com"



            prod.js



            module.exports = 
            "host":"prod.com"



            config.js - main file that will resolve configuration based on process.env.ENV variable.



            const dev = require('./dev');
            const prod = require('./prod');
            let envObject = ;
            const env = process.env.ENV || "dev";
            switch(env)
            case 'prod':
            envObject = prod;
            break;
            default:
            envObject = dev;

            envObject['ENV'] = env;
            process.env = Object.assign(process.env,envObject); // Optional if you prefer to add them into process environment otherwise `require('./config')` where you need configuration.
            module.exports = envObject;


            index.js - node project root file call every time when project start



            const config = require('./config');
            console.log('config object => ',config.host);


            package.json




            "name": "project",
            "version": "0.0.2",
            "scripts":
            "deploy": "sls deploy"




            Running you node.js code
            Prod environment ENV=prod npm run deploy;
            Development environment - npm run deploy;



            Default environment is set to dev in ./config.js



            Using this simple practice you don't need any npm module to manage your environment configurations.






            share|improve this answer












            Maintain and load all your environment specific configuration in project itself.



            dev.js



            module.exports = 
            "host":"dev.com"



            prod.js



            module.exports = 
            "host":"prod.com"



            config.js - main file that will resolve configuration based on process.env.ENV variable.



            const dev = require('./dev');
            const prod = require('./prod');
            let envObject = ;
            const env = process.env.ENV || "dev";
            switch(env)
            case 'prod':
            envObject = prod;
            break;
            default:
            envObject = dev;

            envObject['ENV'] = env;
            process.env = Object.assign(process.env,envObject); // Optional if you prefer to add them into process environment otherwise `require('./config')` where you need configuration.
            module.exports = envObject;


            index.js - node project root file call every time when project start



            const config = require('./config');
            console.log('config object => ',config.host);


            package.json




            "name": "project",
            "version": "0.0.2",
            "scripts":
            "deploy": "sls deploy"




            Running you node.js code
            Prod environment ENV=prod npm run deploy;
            Development environment - npm run deploy;



            Default environment is set to dev in ./config.js



            Using this simple practice you don't need any npm module to manage your environment configurations.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 9 at 8:17









            front_end_dev

            1,3151511




            1,3151511











            • Thanks, but the point is to avoid prefixing npm run with any env variables; also in case of running sls deploy - external tool - no index.js is loaded at all.
              – hsz
              Nov 9 at 8:31










            • Or create a .npmrc file inside the project root folder and set variable there. docs.npmjs.com/files/npmrc#files
              – front_end_dev
              Nov 9 at 8:46
















            • Thanks, but the point is to avoid prefixing npm run with any env variables; also in case of running sls deploy - external tool - no index.js is loaded at all.
              – hsz
              Nov 9 at 8:31










            • Or create a .npmrc file inside the project root folder and set variable there. docs.npmjs.com/files/npmrc#files
              – front_end_dev
              Nov 9 at 8:46















            Thanks, but the point is to avoid prefixing npm run with any env variables; also in case of running sls deploy - external tool - no index.js is loaded at all.
            – hsz
            Nov 9 at 8:31




            Thanks, but the point is to avoid prefixing npm run with any env variables; also in case of running sls deploy - external tool - no index.js is loaded at all.
            – hsz
            Nov 9 at 8:31












            Or create a .npmrc file inside the project root folder and set variable there. docs.npmjs.com/files/npmrc#files
            – front_end_dev
            Nov 9 at 8:46




            Or create a .npmrc file inside the project root folder and set variable there. docs.npmjs.com/files/npmrc#files
            – front_end_dev
            Nov 9 at 8:46

















            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53221045%2fload-env-environment-variables-when-running-npm-task%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

            𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

            Edmonton

            Crossroads (UK TV series)