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 🙏

© 2026 – Pkg Stats / Ryan Hefner

imagic-config

v1.0.1

Published

Load JSON/JS configuration files by APP_ENV environment variable

Downloads

195

Readme

imagic-config

Load JSON/JS configuration files by environment, merging defaults and validating required keys.

Install

npm install imagic-config

Quick Start

import { loadConfig } from 'imagic-config'

// Reads from ./config/development/ (or APP_ENV subdirectory)
const config = await loadConfig()

console.log(config.database.host)
console.log(config.server.port)

API

loadConfig(options?)Promise<object>

Reads all .json and .js files from {dir}/{env}/, merges them with defaults, and returns a single config object. Each file's lowercased name (without extension) becomes a key.

loadConfig(options?: {
    dir?:      string,
    env?:      string,
    defaults?: object,
    required?: string[],
}): Promise<{ [key: string]: any }>

Options

| Option | Type | Default | Description | |--------|------|---------|-------------| | dir | string | './config' | Base directory containing environment subdirectories | | env | string | process.env.APP_ENV \|\| 'development' | Environment name; determines which subdirectory is read | | defaults | object | {} | Base config merged before loading files; deep-cloned, never mutated | | required | string[] | [] | Keys that must be present in the result; throws if any are missing |

Returns

A plain object with one key per config file found in {dir}/{env}/. File content is deep-merged on top of defaults.

File → key mapping

| File | Key in result | |------|--------------| | database.json | result.database | | Redis.json | result.redis (lowercased) | | app.js (ES module) | result.app = module.default if present, else module |

Subdirectories inside the env directory are silently skipped. Only .json and .js files are processed.


Directory Structure

config/
  development/
    server.json
    database.json
    redis.json
  production/
    server.json
    database.json
    redis.json
  test/
    server.json

Set the environment via the APP_ENV variable or the env option:

APP_ENV=production node server.js

Error Handling

| Condition | Error | |-----------|-------| | Config directory does not exist and required is non-empty | Error | | A key listed in required is not present in the result | Error | | A .json file contains invalid JSON | Error with .cause set to the parse error |

When required is empty and the config directory is absent, loadConfig returns the defaults object without throwing.


Examples

Basic usage

import { loadConfig } from 'imagic-config'

const config = await loadConfig()

Custom directory and required keys

import { loadConfig } from 'imagic-config'

const config = await loadConfig({
    dir: './app/config',
    env: process.env.APP_ENV || 'production',
    defaults: {
        server: { port: 3000 },
    },
    required: ['database', 'server'],
})

console.log(config.server.port)    // from defaults if not overridden by file
console.log(config.database.host)  // from ./app/config/production/database.json

JS config file

// config/development/app.js
export default {
    debug: true,
    featureFlags: ['beta-ui'],
}
const config = await loadConfig()
console.log(config.app.debug)         // true
console.log(config.app.featureFlags)  // ['beta-ui']

See examples/basic.js for a runnable demonstration:

node examples/basic.js

License

MIT © iMagicKey