@ideadesignmedia/config.js
v1.0.3
Published
Side-effect-only helper that reads a `config.json` file in your current working directory and copies its contents into `process.env`.
Readme
@ideadesignmedia/config.js
Side-effect-only helper that reads a config.json file in your current working directory and copies its contents into process.env.
Installation
npm install @ideadesignmedia/config.jsPrepare config.json
Create a config.json alongside the script that will run your application. Two shapes are supported:
Object form
{ "API_BASE_URL": "https://api.example.com", "NODE_ENV": "development" }Array form
[ { "key": "API_BASE_URL", "value": "https://api.example.com" }, { "key": "NODE_ENV", "value": "development" } ]
Every key becomes an environment variable (values are stored as strings). Missing files cause the package to throw Error: Please add config.json on startup.
Usage
Import the package once near your entry point. No value is exported; the import is only for its side effects.
CommonJS
require('@ideadesignmedia/config.js'); console.log(process.env.API_BASE_URL);ES modules / TypeScript
import '@ideadesignmedia/config.js'; const apiBaseUrl = process.env.API_BASE_URL;
The module can be imported multiple times without issue. Subsequent imports simply reuse the environment variables already attached to process.env.
Tips
- Keep
config.jsonout of version control if it contains secrets; add it to.gitignore. - Prefer string values in your config file; Node.js stores environment variables as strings.
- Combine this loader with packages like
dotenvor deployment-specific configuration files as needed.
