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

alc-env

v1.0.1

Published

A dependable, simple means of getting ENV variables

Downloads

4

Readme

alc-env

A dependable, simple means of getting ENV variables.

What

A very basic (but solid) means of getting ENV variables, with a twist: you can specify your ENV defaults inside your project's package.json file.

As I've come to use environment variables more in my JS & Node apps lately, I decided I didn't want to only rely on variables being passed via the CLI at runtime. That's why I wrote this simple reusable script to look for my ENVs wherever they're defined and whatever they're named.

Compatibility

| OS | Will it work? | | ------------------ | ------------- | | Mac OSX | Yes | | Linux | Yes | | Windows | Maybe..? | | Anything with Bash | Probably |

Only tested with Node 6.2+, but will probably work with 4 and possibly lower.

Usage with a front-end app is only recommended if you're using a plugin to support CommonJS or ES6 imports, or a build tool that bundles modules.

How

Install & add to your existing project:

npm i --save alc-env

Import:

// CommonJS
var env = require('alc-env')

// or ES6 import
import env from 'alc-env'

(Optionally) Set some default ENV values in your project's package.json:

"config": {
  "env_name": "default env value"
},

(Optionally) Pass some ENVs via the command line:

env_name="env value" npm start

Call your ENVs freely and safely:

// Get env variable or return a default value if it's not defined
var envA = env('env_name', 'default value');

// Get the first found env variable in the passed array, or return a default value
var envB = env(['env_name', 'fallback_env_name'], 'default value');

What about setting ENVs from inside JS files?

Sorry, that's not a planned feature.

I'm not a fan of setting any ENV variables within the source code of an app. Not only is it messy to do this, but also consider cases where those variables contain potentially sensitive data. Yes, sensitive data isn't necessarily going to fare better inside the package.json file, however this will be up to the user to manage.*

* In other words: if you're storing your sensitive data in your package file, or any other file in your app for that matter, absolutely do not upload this to your public Github or NPM account! This module isn't going to help you there.

Inspiration

Lots of amazing JS & Node modules and indeed their creators have inspired what I code today, but the main drive to write this little module was down to working a lot with a Vagrant-to-Docker migration lately and needing a reliable and predictable method of storing ENV variables both before and after migration. Since the package.json file was always going to be present, it made a lot of sense to use it as a storage for ENV defaults in any case when ENVs set from the CLI or from Dockerfiles weren't present.

There's other ENV "management" modules up on NPM and Github, but I couldn't find one that was in quite this category of providing a fallback to use the package.json properties.

Dependencies

Nothing inside the package.json is mandatory, I've only used unimportant devDependencies to standardise the versions of ESLint & its plugins that I use for code cleanliness. To keep a production build minimal in your app it's safe to use the --production flag with this module. I can't vouch for other dependencies your project has however, so use the production flag at your own risk.

Todo

  • Add support for custom named package.json parameters as secondary ENV source
  • Add support for multiple package.json source parameters
  • Write tests
  • Possibly introduce type-setting upon getting ENVs as with other ENV "getter" modules, but currently this isn't top priority