npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dots-env

v1.1.8

Published

A simple node program for executing commands using an environment from **any** env file.

Downloads

415

Readme

dots-env

A simple node program for executing commands using an environment from any env file.

💾 Install

npm install dots-env --save-dev or yarn add dots-env -D

⌨️ Basic Usage

Create your .env.development file as:

# This is a comment
ENV1=THANKS
ENV2=FOR ALL
ENV3=THE FISH

Package.json

{
  "scripts": {
    "start": "dots-env -c 'node index.js'"
  }
}

Terminal

./node_modules/.bin/dots-env -c "node index.js"

Examples

Ignoring .env

Change your .env section from .gitignore to:

# env files
.env
.env.*.local

Using env file

This package assume that your project have an .env.development file to use it. The dots-env will create an .env at the root of the application with the content of your .env.* and it will inject the content on process.env. So, we recommend that you add the .env into .gitignore because it will be reconstruct always that dots-env script run.

Using other env files

To use other env files, create your .env.staging or .env.production or .env.*, and pass the argument --env or just -e with the option:

dots-env -c 'node index.js' # use .env.development
dots-env -c 'node index.js' -e staging # use .env.staging
dots-env -c 'node index.js' -e production # use .env.production
dots-env -c 'node index.js' -e my_env_file # use .env.my_env_file

Using custom local env file

To use a local env file, just create it with .local on the end of filename, and pass the argument --local or just -l

dots-env -c 'node index.js' -l # use .env.development.local
dots-env -c 'node index.js' -e staging -l # use .env.staging.local
dots-env -c 'node index.js' -e production -l # use .env.production.local
dots-env -c 'node index.js' -e my_env_file -l # use .env.my_env_file.local

Using custom env file path

If yours .env files are into another directory, you can use the argument --envPath, or just -p to change this:

# - envs/
#   - .env.development
#   - .env.staging
#   - .env.production
#   - .env.my_env_file
# - index.js

dots-env -p './envs/' -c 'node index.js'  # use ./envs/.env.development
dots-env -p './envs/' -c 'node index.js' -e staging  # use /envs/.env.staging
dots-env -p './envs/' -c 'node index.js' -e production  # use /envs/.env.production
dots-env -p './envs/' -c 'node index.js' -e my_env_file  # use /envs/.env.my_env_file

Programmatically

You can use the dots-env on your node script like this:

// start.js
const { processEnv, execCommand } = require('dots-env')

processEnv({
  // local: false,
  // env: 'development',
  // destinationPath: '',
  envPath: './envs/'
}).then(() => {
  execCommand('yarn start')
})

If you do not pass some parameters to processEnv, you can set the values of this missing arguments by cli when executing your script. Example:

node start.js -e staging -l

processEnv

This script will do all magic and return a promise that will be resolved when .env file was created. This promise return an object with:

{
  envName, // environment name
  envFile, // .env[envName]
  originalEnvPath, // path of .env[envName]
  destinationEnvPath, // path of .env created by dots-env
  local, // boolean flag
  getEnvValues, // function that return an object with env values
  reloadEnvValues, // function to reload process.env
}

execCommand

This method are a promise that execute one shell script with the env selected.

Setting default configs

You can set custom configs to your project creating the dots-env.config.js file on root of your project and export your default configurations as a object. You are able to overwrite the configurations when you invoke CLI or call the processEnv method.

// dots-env.config.js
module.exports = {
  env: 'development',
  local: false,
  envPath: '',
  destinationPath: ''
}

📜 Help

Usage: _ [options] <command> [...args]

Arguments:
  -e, --env [envName]              Set the file `.env.[envName]` to use (default envName: development)
  -l, --local [boolean]            Set the `.local` file based on `--env`
  -p, --envPath [path]             Set the path of your `.env.*` files (default path: '.')
  -d, --destinationPath [path]     Set the path that `.env` will be created (default path: '.')
  -c, --command [command]          REQUIRED! Set the command that will execute with the selected env. 

📋 Contributing Guide

If you would like to write types, tests or improve this package, please sent your PR! :D