dotenv-up

1.0.32 • Public • Published

npm version NpmLicense

Installation

    yarn add dotenv-up

Reference

    require('dotenv-up')(3, true, 'sandbox/server.js');
    
    /**
     * Use like
     *     require('dotenv-up')(2); // 2 - default assigned to 'deep' parameter
     *     require('dotenv-up')(2, false); // second param is "debug" flag - to show on not show each step on the screen
     *     require('dotenv-up')(2, false, 'name of load'); // plain text to describe individual use of this library
     *     require('dotenv-up')({
                path            = process.cwd(),
                envfile         = '.env',
                override        = true, // override values in process.env
                deep            = 1,
                startfromlevel  = 0,
                justreturn      = false, // don't add found values to the process.env global object
                ... other dotenv options https://www.npmjs.com/package/dotenv
     *     });
     *
     * @param opt
     * @param debug (def: true)
     * @param name
     * @returns {} - object with all extracted variables
     */

Docker use

require('dotenv-up')({
    override    : false, // don't override existing parameters in process.env by those from .env file
    deep        : 3,
}, true, 'sandbox/server.js');

// instead of:

require('dotenv-up')(3, true, 'sandbox/server.js');
// because 'override' flag is by default true

Stacking config in given order from multiple files

const directly = require.main === module;

require('dotenv-up')({
    deep            : 2,
    startfromlevel  : 1,
}, !directly, 'index.server');

require('dotenv-up')({
    deep        : 1,
}, !directly, 'index.server');

if (directly) {

    console.log(JSON.stringify(process.env, null, 4));

    process.exit(0);
}

module.exports = () => {

    return {
        origin: "http://localhost:" + process.env.NODE_PORT,
        delayms: 0.2 * 1000,
    }
}

Todo

Tip

If you need to include other file that use internally again dotenv-up tool in override mode but you don't want those changes of process.env affect proce.env in you'r scope, do:

const ttt = {...process.env};
require('./lib/lib.js') // with its own require('dotenv-up')(1);
process.env = ttt;     

Readme

Keywords

Package Sidebar

Install

npm i dotenv-up

Weekly Downloads

2

Version

1.0.32

License

MIT

Unpacked Size

12.8 kB

Total Files

5

Last publish

Collaborators

  • stopsopa-owner