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

@constantiner/resolve-node-configs-hierarchy

v0.4.6

Published

Simple library to resolve configuration files hierarchy in Node projects for producing effective configuration from them

Downloads

194

Readme

resolve-node-configs-hierarchy

Build Status codecov

Simple library to resolve configuration files hierarchy in Node projects for producing effective configuration from them.

It is very convenient if you need to have local configuration for local environment and not place it in version control system. Or to have separate configurations for development or test or production environments to apply them automatically.

So the library allows to manage configurations for any environment and the same code base.

Note Don't forget to put local files to .gitignore.

Changelog

Versions changelog.

Documentation

See the full documentation here.

The utility was inspired by create-react-app and may contain some chunks of code from it.

Note To use in Node environment, not in browser.

The base idea is the following:

It will list the following files, starting from the bottom. The first value set (or those already defined in the environment) take precedence:

  • .env - The Original®
  • .env.development, .env.test, .env.production - Environment-specific settings.
  • .env.local - Local overrides. This file is loaded for all environments except test (you can include it with flag as the second parameter).
  • .env.development.local, .env.test.local (for test environment you can include it with flag as the second parameter), .env.production.local - Local overrides of environment-specific settings.

It uses process.env.NODE_ENV for setting environment.

For test environment it will not list .env.local and .env.test.local from this list by default since normally you expect tests to produce the same results for everyone. You can include them by passing the second parameter with true value.

It may use any relative path as the base path even with extension.

It contains the following methods:

getConfigFiles

Returns a list of absolute file paths of existing files in the order to apply from first to last (in order of precedence).

for example if you pass "configuration/log4js.json" it will produce the following list for development environment (if all of these files are exist in file system):

  • <project_path>/configuration/log4js.development.local.json
  • <project_path>/configuration/log4js.local.json
  • <project_path>/configuration/log4js.development.json
  • <project_path>/configuration/log4js.json

This utility is asynchronous and returns a promise resolving to file list. Use getConfigFilesSync for synchronous version.

This utility was inspired by create-react-app and may contain some chunks of code from it.

Note The bundle contains only ES6 modules version. Use Babel, Rollup, Webpack etc. to produce commonjs version.

getConfigFile

Returns the most relevant absolute file path of existing files in files hierarchy.

for example if you pass "configuration/log4js.json" it will return the following file path for development environment (if it exists in file system):

  • <project_path>/configuration/log4js.development.local.json

This utility is asynchronous and returns a promise resolving to absolute file path as String (or resolving to null). Use getConfigFileSync for synchronous version.

getConfigFilesSync

Synchronous version of getConfigFiles.

getConfigFileSync

Synchronous version of getConfigFile.

Installation

npm install @constantiner/resolve-node-configs-hierarchy

Usage

Import it first:

import { getConfigFiles } from "@constantiner/resolve-node-configs-hierarchy";

Then you can use it:

getConfigFiles("src/.env").then(files => {
	files.forEach(file => {
		dotenv.config({
			path: file
		})
	})
});

Or for getting the single most actual config file:

import { getConfigFile } from "@constantiner/resolve-node-configs-hierarchy";

getConfigFile("src/.env").then(filePath => {
	if (filePath) {
		const config = require(filePath);
	}
});

To include local files in test environment you may pass corresponding flag (it is false by default):

getConfigFiles("src/.env", true).then(files => {
	files.forEach(file => {
		dotenv.config({
			path: file
		})
	})
});

Or the same for getConfigFile:

getConfigFile("src/.env", true).then(filePath => {
	if (filePath) {
		const config = require(filePath);
	}
});

The same is for synchronous versions:

import { getConfigFilesSync } from "@constantiner/resolve-node-configs-hierarchy";

Then you can use it:

getConfigFilesSync("src/.env").forEach(file => {
	dotenv.config({
		path: file
	})
});

Or for getting the single most actual config file:

import { getConfigFileSync } from "@constantiner/resolve-node-configs-hierarchy";

const filePath = getConfigFileSync("src/.env");
if (filePath) {
	const config = require(filePath);
}