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

nodejs-config

v1.0.1

Published

A Laravel inspired simple NodeJs configuration loader and manager which can load configuration values based on the development environment.

Downloads

383

Readme

nodejs-config

A Laravel inspired simple NodeJs configuration loader and manager which can load configuration values based on the development environment.

Build Status

##Installation##

The source is available for download from GitHub. Alternatively, you can install using Node Package Manager (npm):

npm install nodejs-config

##Setup##

All the configurations should be stored in your application within a folder named config. You may create multiple configuration files(json) and put group of configurations belongs to same category into a single file. For example you may keep applications general settings in config/app.json and database settings in config/database.json. See example configuration files at test folder.

You can setup a new configuration manager instance with following syntax:

var config = require('nodejs-config')(
   __dirname  // an absolute path to your applications `config` directory
);

During setup you may instruct the configuration manager how to determine which environment it is running in. The default environment for configuration manager is always production. However, you may pass an environments object to configuration manager during setup. The object passed to this method is used to determine the current environment. The Object should have following structure:

var config = require('nodejs-config')(
   __dirname,  // an absolute path to your applications 'config' directory
   {
      development: ['your-machine-name']
   }
);

You may add any number of environments and machine names to the object as needed. In this example, 'development' is the name of the environment and 'your-machine-name' is the hostname of your server. On Linux and Mac, you may determine your hostname using the hostname terminal command. If you need more flexible environment detection, you may pass a function to the configuration setup method, allowing you to implement environment detection however you wish:

var config = require('nodejs-config')(
   __dirname,  // an absolute path to your applications 'config' directory
   function()
   {
      return process.env.NODE_ENV;
   }
);

####Accessing A Configuration Value####

To access a configuration field timezoneOffset from the configuration file 'config/app.json' use following syntax:

config.get('app').timezoneOffset;

or alternatively you can get a nested configuration value out of the configuration group with dot style syntax as follows:

config.get('app.timezoneOffset');

You may also specify a default value to return if the configuration option does not exist:

config.get('app.timezoneOffset', 5.30);

####Setting A Configuration Value####

You may also set configuration values at run-time:

config.set('database.default', 'mongo');

Configuration values that are set at run-time are only set for life time of the configuration manager instance.

####Environment Specific Configuration####

It is often helpful to have different configuration values based on the environment the application is running in. For example, you may wish to use a different timezoneOffset on your local development machine than on the production server. It is easy to accomplish this using environment based configuration.

Simply create a folder within the config directory that matches your environment name, such as local. Next, create the configuration files you wish to override and specify the options for that environment. For example, to override the timezoneOffset for the local environment, you would create a app.json file in $path/config/local with the following content:

{
    "timezoneOffset": "6.30",
};

Notice that you do not have to specify every option that is in the base configuration file, but only the options you wish to override. The environment configuration files will "cascade" over the base files.

####Accessing The Current Configuration Environment####

You may access the current configuration environment via the environment method:

environment = config.environment();

You may also pass arguments to the environment method to check if the environment matches a given value:

if (config.environment('local'))
{
    // The environment is local
}
if (config.environment('local', 'staging'))
{
    // The environment is either local OR staging...
}