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

global-dotenv

v0.1.2

Published

Global-dotenv creates the shared persistent `global.env` file that is accessible from all running apps and allows them to read variables from it and append variables to it. It enables apps to "know about each other" and to share information between each o

Downloads

3

Readme

global-dotenv

Global-dotenv creates the shared persistent global.env file that is accessible from all running apps and allows them to read variables from it and append variables to it. It enables apps to "know about each other" and to share information between each other without building a message system.

Install

npm install global-dotenv

Usage

To read the global global.env file as JSON, require and parse global-dotenv:

const global_dotenv = require('global-dotenv');

var global_dotenv_as_json = global_dotenv.parseSync();
// { FOO: 'BAR', MAY: 'The 4th' }

global_dotenv_as_json['FOO'];
// 'BAR'

If the global file doesn't yet exist or is removed, an empty object {} is returned. As the file is shared by all running apps, and can be altered by them at any moment, it's recommended to parse it every time right before you want to read the values from it.

To append variables to the global global.env file, pass them into appendSync function in the form of key, value:

const global_dotenv = require('global-dotenv');

global_dotenv.appendSync('FOO', 'BAR');
global_dotenv.appendSync('NUMBER', 1024);

Both key and value will be converted into strings before appending. If typeof value is "object", it's automatically converted into a string with JSON.stringify():

const global_dotenv = require('global-dotenv');

global_dotenv.appendSync('OBJ', {foo: 'bar'});
// { OBJ: '{"foo":"bar"}' }

Config

The default path to the global file is $HOME/global.env (Like: /home/user/global.env) for Linux and %HOMEPATH%\global.env (Like: C:\Users\User\global.env) for Windows. You can change it by setting a custom path. Do this by passing an options object with a path key into parseSync and appendSync functions:

const global_dotenv = require('global-dotenv');

var global_dotenv_as_json = global_dotenv.parseSync({path: '/path/to/the/file'});

global_dotenv.appendSync('FOO', 'BAR', {path: '/path/to/the/file'});

If the file doesn't exist, it will be automatically created. By using a proper custom path every time you can have multiple global files if it makes sense to you and your apps. For example you can have several groups of apps that share different global files.

Accessibility of the global.env file

The whole point of this module is that the global file can be accessed by many apps. The default $HOME/global.env file is accessible by the apps that are running under the user who owns the $HOME directory, under root user or under any user with sudo command. If you want the global.env file to be accessed by apps that are run under different users, make sure those users have access to the global file. If the app that tries to use the global file is run under a user that has no acces to the global file, parseSync and appendSync functions will throw an exception. You'll need to try-catch it yourself.