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

node-habitat

v1.0.1

Published

A small package for managing env variables

Downloads

6

Readme

node-habitat

node-habitat exists because of the difficulty with managing environment configs across various projects. For example, in the back-end you traditionally access process.env, for the front-end you can use wepack env, for universal you create some hybrid, and for mobile (react-native) its something custom.

node-habitat solves this by being a simple environment package that works across mobile, universal, the front-end, and the back-end.

Getting started

To get started you first need to install node-habitat into your project:

npm install node-habitat
or
yarn add node-habitat

Next create a .env.json file at the root of your project so that node-habitat can process your environment.

After creating the .env.json just import node-habitat into the initialization code of your project (app.js or index.js) and call the create() method:

import habitat from 'node-habitat';

habitat.create();

And that's it! To access your env just use the global namespace global.habitat.

For example:

const testEnv = global.habitat.TEST_ENV;

console.log(testEnv); // prints whatever TEST_ENV is inside your config file.

Using .env.json

You must place the .env.json file at the root of your project so habitat can find it.

The environment config file for node-habitat is just a simple .json file with a few custom features your can use.

First, the generic file looks like:

{
"env": "development",
"development": {
"MOCK_API": "www.api.dev.com"
},
"staging": {
"MOCK_API": "www.api.staging.com"
},
"production": {
"MOCK_API": "www.api.production.com"
}
}

The only required field here is the env: section - this tells habitat what environment switch to use. For example is the env is set to development then MOCK_API will equal www.api.dev.com. If, however, env is set to production then MOCK_API will equal www.api.production.com. Habitat will automatically setup the environment depending on the env otherwise it will return an empty object.

You can access the habitat throughout your entire project by using the global space global.habitat. For example running with the config above will output:

const apiUrl = global.habitat.MOCK_ENV;
console.log(apiURL); // prints out www.api.dev.com

Finally, you can access the current env by global.habitat.env.

Publishing Package

To publish new versions of habitat first add you newest changes and increment the verison number inside of package.json.

Once that is done, commit your changes to git and create a new tag. For example:

git add new_file.txt && git commit -m "Add new file"
git tag 1.0.0
git push origin 1.0.0
git push origin master

After the tag is added to git you need to publish the package to npm by running

npm publish

This will automatically create a new build of the code and push directly to the npm package repository.

Best Practices

As with any project it is ultimately up to the developer to decide what to do with their config files. With that being said, with habitat there are some best practices that will help keep your config safe.

First, make sure to not version the .env.json file to source control. Create a .env.example.json with your config variable names if you need to keep track of it.

Second, if you're creating a universal app be wary of importing habitat into your front-end code. Basically front-end code should always be considered exposed to the outside world - so if you're using secrets in your back-end and sharing the config to the front-end you may expose some API secrets. Essentially don't put sensitive information into your client config.

License

MIT