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

autoconfig

v1.0.0-rc.1

Published

Simple environment configuration from a config file for Node.js

Downloads

8

Readme

autoconfig

enter image description here Version Downloads Licence

Overview

An npm module to simplify configuration in a Node.js environment using a configuration file. Much of this library was based on mkleehammer/autoconfig a Python configuration module using a config file. The biggest difference between the Python and Node.js versions is that Node.js version excepts an object instead of parameters. Node.js does not support default paramters without the use of harmony flags::

  var autoconfig = require('autoconfig');
  autoconfig.init();

With these simple lines before other imports, you can add variables to process.env which provides global access to the configuration.

After using Python autoconfig and needing a configuration thats as easy for Node.js, I decided to create this project for all of my Node.js projects.

(From mkleehammer/autoconfig) - the 12 Factor App recommends keeping configuration in environment variables...

A common solution, that can lead to problems, is to set environment variables or command line options to the currently running user's environment. The first problem is Windows vs macOS/Linux style of setting the environment varable SET NODE_ENV=development (or setx) vs export NODE_ENV=development. The other option is to set it directly in the run node command, but that is sometimes more confusing on Windows vs macOS/Linux SET NODE_ENV=development && node index.js vs NODE_ENV=development node index.js.

In the end, this usually ends up to using a config file on the computer or hard-coded (env = process.env.NODE_ENV ? process.env.NODE_ENV : 'development') that is checked-in with some sort of .dev .qa .prod in the filename and could easily be error prone. The computer "usually" never changes environments mid-development cycle so it makes sense to have a configruation that is loaded to the process.env taken from a file specific to this machine. Given the file's location, it can be machine-wide (think QA only server with multiple projects) or project specific, see below.

Simple Example

If an object is not passed, the library searches for a file named .config.json, starting from the current directory (process.cwd()) and searching upwards.

Using well-known JSON format, this makes it easy to get started instantly

  {
    "env": {
      "NODE_ENV": "development",
      "DATABASE_URL": "postgresql://localhost/test?client_encoding=utf8"
    },
    "other": {
      "DATABASE_URL": "postgresql://otherhose/test?client_encoding=utf8"
    }
  }

The two entries NODE_ENV and DATABASE_URL are added to process.env and are accessible anywhere in any Node.js file by simply by using process.env.NODE_ENV OR process.env.DATABASE_URL.

Environment Variables

If an "env" section exists, its items are always copied, but additional sections can be copied also by passing them via the env keyword. The value can be a section name or a list of section names - the object can contain env (environment name), filename, searchfrom (filepath or null to search upward)

autoconfig.init({env: 'other'});             # copy from env and other
autoconfig.init({env: ['other', 'another]}); # copy from env, other, and another

Even though env is always copied to process.env, other environments (if passed in) override env. This means that in the "Simple Example" above, other's "DATABASE_URL" will be used when using "process.env.DATABASE_URL" even though env contains the same configuration.

Locating The Config File

The default behavior is to look for a file named ".config.json", starting in the current working directory and searching upwards. If a file is not found, an exception is thrown.

The init function accepts two keywords to customize this:

filename Pass just a filename, such as "project.ini" to search for a different filename.

Pass an absolute path name, such as "/etc/project.ini", to disable searching and use the filename as given.

searchfrom An optional directory to begin the search from. If not provided, the default is the current working directory as provided by process.cwd().

To simplify configuration, a path to a file can also be passed and the search will begin in the same directory as the file. This is particularly handy for starting a search from the directory where the calling module is located

autoconfig.init({
    env: 'env',
    searchfrom: __dirname
});

This object value is ignored if filename is an absolute path since no search is performed.