Socket
Socket
Sign inDemoInstall

lazy-universal-dotenv

Package Overview
Dependencies
3
Maintainers
3
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    lazy-universal-dotenv

Robust Environment Configuration for Universal Applications.


Version published
Weekly downloads
5.5M
increased by3.33%
Maintainers
3
Install size
107 kB
Created
Weekly downloads
 

Package description

What is lazy-universal-dotenv?

The lazy-universal-dotenv npm package is a utility for managing environment variables in a universal (isomorphic) JavaScript application. It allows developers to define environment variables in a .env file and then access them in both client-side and server-side code. This package is particularly useful for applications built with React and server-side rendering, where environment variables need to be shared between the server and the client.

What are lazy-universal-dotenv's main functionalities?

Loading environment variables

This feature allows you to load environment variables from your .env files. The `getEnvironment` function reads the variables and makes them available in your application.

const { getEnvironment } = require('lazy-universal-dotenv');
const { environment } = getEnvironment();
console.log(environment);

Accessing public environment variables

This feature provides access to environment variables that are specifically marked as public (i.e., those prefixed with `PUBLIC_`). This is useful for exposing certain variables to the client-side of a universal application without exposing server-side secrets.

const { getEnvironment } = require('lazy-universal-dotenv');
const { publicPath } = getEnvironment();
console.log(publicPath);

Other packages similar to lazy-universal-dotenv

Readme

Source

Lazy Universal DotEnv

Lazy Universal DotEnv - A Robust Environment Configuration for Universal Applications.

Features

  • Supports loading .env files with overriding between different NODE_ENV settings and BUILD_TARGET configurations.
  • Supports variable expansion between different settings.
  • Allows local overrides using files which use a ".local" postfix.

All Strings

It is important to remember that all environment variables are always stored as strings. Even numbers and booleans. The casting to other types must therefore take place in the application code. See also: https://github.com/motdotla/dotenv/issues/51

Variables

  • NODE_ENV: Typically either production, development or test
  • BUILD_TARGET: Either client or server

File Priority

Files are being loaded in this order. Values which are already set are never overwritten. Command line environment settings e.g. via cross-env always win.

  • .env.${BUILD_TARGET}.${NODE_ENV}.local
  • .env.${BUILD_TARGET}.${NODE_ENV}
  • .env.${BUILD_TARGET}.local
  • .env.${BUILD_TARGET}
  • .env.${NODE_ENV}.local
  • .env.${NODE_ENV}
  • .env.local
  • .env

Note: local files without NODE_ENV are not respected when running in NODE_ENV=test.

Basic Usage

import { getEnvironment } from "lazy-dotenv-universal";

const environment = getEnvironment({ nodeEnv, buildTarget });

const { raw, stringified, webpack } = environment;

After this you can access all environment settings you have defined in one of your .env files.

A .env file:

MY_END=awesome

Webpack config:

import { getEnvironment } from "lazy-dotenv-universal";

export default {
  // ... 
  plugins: [
    new webpack.DefinePlugin(getEnvironment().webpack),
  ],
  // ...
}

Code being bundled by webpack:

console.log(process.env.MY_ENV); // -> "awesome"

Serialization

  • raw: Just a plain JS object containing all app settings
  • stringified: Plain object but with JSON stringified values
  • webpack: For usage with Webpacks Define Plugin

License

Apache License Version 2.0, January 2004

Copyright 2018
Sebastian Software GmbH

Keywords

FAQs

Last updated on 30 Jan 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc