Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Dot-notation transformer for dotenv. Transform .env variables into condensed JSON variables for use in nodejs projects.

License

NotificationsYou must be signed in to change notification settings

roydukkey/dotenv-dot

Repository files navigation

dotenv-dot

Dotenv-dot adds dot-notation variable transformation on top ofdotenv. If you find yourself needing to condense environment variables into JSON variables, then dotenv-dot is your tool.

Release VersionLicense

Install

npm install dotenv --savenpm install dotenv-dot --save

Usage

Dot-notation variables are those which contain a dot (.) in their name. Add dot-notation variables to the.env file like any other variable.

MAIL_CONFIG.service=gmailMAIL_CONFIG.auth.user=noreply@domain.comMAIL_CONFIG.auth.pass=pass1234

Once the dotenv-dot transformer is executed, these variables will be condensed into a new variable namedMAIL_CONFIG.

MAIL_CONFIG='{"service":"gmail","auth":{"user":"noreply@domain.com","pass":"pass1234"}}'

Transformprocess.env variables

As early as possible in your application, require dotenv and dotenv-dot. Then, execute dotenv-dot after dotenv.

constdotenv=require('dotenv');constdotenvDot=require('dotenv-dot').transform;constmyEnv=dotenv.config();constresults=dotenvDot();// only updates `process.env`

Transformprocess.env and the results ofdotenv.config()

If you want to updateprocess.env and the output of thedotenv.config(), include the output as a parameter on the dotenv-dot transformer.

constmyEnv=dotenv.config();constresults=dotenvDot(myEnv);// updates `process.env` and `myEnv`

Transform variables without affectingprocess.env

It is possible to use dotenv without adding variables toprocess.env. Dotenv-dot can also do the same.

constparsedOutput=dotenv.parse(`MAIL_CONFIG.service=gmailMAIL_CONFIG.auth.user=noreply@domain.comMAIL_CONFIG.auth.pass=pass1234`);constresults=dotenvDot(parsedOutput);

If you already have parsed output you may transform it without using dotenv.

constresults=dotenvDot({'MAIL_CONFIG.service':'gmail','MAIL_CONFIG.auth.user':'noreply@domain.com''MAIL_CONFIG.auth.pass':'pass1234'});

Options

debug

Default:false

You may turn on logging to help debug why certain keys or values are not being set as expected.

constmyEnv=dotenv.config();require('dotenv-dot').transform(myEnv,{debug:process.env.DEBUG});
ignoreProcessEnv

Default:false

You may want to ignoreprocess.env when transforming the output of thedotenv.config(). When this option is turned onprocess.env will not be consulted or altered.

constmyEnv=dotenv.config();require('dotenv-dot').transform(myEnv,{ignoreProcessEnv:true});

How do I use dotenv-dot withimport?

import*asdotenvfrom'dotenv';importdotenvDotfrom'dotenv-dot';

Or, if only intending to accessprocess.env, the two modules should be loading in this order using their auto-imports:

import'dotenv/config';import'dotenv-dot/transform';

More information about theimport syntax is available on thedotenv repository.

Note: You may set thedebug option using the dotenv debugcommand line or environment variable. TheignoreProcessEnv option is irrelevant when using the auto-imports.

How do I add arrays to the.env file?

Arrays are added by using numbers to indicate the value's index in the resulting array.

THINGS.0='Was eaten by his others'THINGS.1='Thing One'THINGS.3='Wayward Thing Two'

About

Dot-notation transformer for dotenv. Transform .env variables into condensed JSON variables for use in nodejs projects.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

[8]ページ先頭

©2009-2025 Movatter.jp