Unleash awesomeness. Private packages, team management tools, and powerful integrations. Get started with npm Orgs »

vne

1.2.6 • Public • Published

vne

N

A clever .env

vne is not yet compatible with pnpm.

Installation

$ npm i vne -S

Usage

import env from "vne"; // const env = require("vne"); works too
 
console.log(env); // your .env file in object format

Example

Take this sample .env file:

tokens1="jumble-of-numbers-and-letters"
tokens2="letters-and-jumble-of-numbers"
 
portDatabase=55555
serviceApi="jumble-of-letters-and-numbers"
 
dev-api="http://localhost:3000"
prod-api="https://api.domain.tld"
 
dev-app="http://localhost:3001"
prod-app="https://app.domain.tld"
 
dev-marketing="http://localhost:3002"
prod-marketing="https://domain.tld"

vne takes the .env file and produces an object like this:

{
  tokens: [
    "jumble-of-numbers-and-letters",
    "letters-and-jumble-of-numbers"
  ],
  portDatabase: "55555",
  serviceApi: "jumble-of-letters-and-numbers",
  dev: {
    api: "http://localhost:3000",
    app: "http://localhost:3001",
    marketing: "http://localhost:3002"
  },
  prod: {
    api: "https://api.domain.tld",
    app: "https://app.domain.tld",
    marketing: "https://domain.tld"
  }
}

Variables with names differentiated by numbers are intelligently placed into an array for easy iteration. A use case would be a handful of tokens you want to have admin access to your API.

Other variables in your .env file with a - or . gets placed into a nested object for easy querying. That way, you will be able to do something like this (check the url parameter):

return new Promise((resolve, reject) => {
  request({
    body: {},
    json: true,
    method: "POST",
    url: process.env.NODE_ENV === "development" ? env.dev.api : env.prod.api // BOOM
  }).then(body => {
    if (!body) return reject(body);
    resolve(body);
  }).catch(error => {
    resolve(error);
  });
});

That one-liner checks to see if your app is running in a development environment or not (of course, this assumes that you are setting your environment when starting your app). Check out the scripts section of this package.json file for reference.

Anyhoo, if your app is running in development mode the parameter on the left side of the : will be called. If not (it's running in production mode), the parameter on the right side is called.

Easy-peasy!

install

npm i vne

Downloadsweekly downloads

6

version

1.2.6

license

BSD-3-Clause

repository

Gitgit

last publish

collaborators

  • avatar
Report a vulnerability