@richardpickett/config-js
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

Config JS

npm i @richardpickett/config-js
// config.js
import Config from "@richardpickett/config-js";
import packageJson from "../package.json" assert { type: "json" };

const skipDotEnv = true; // skip reading .env files

const config = new Config({
  jsonVariables: ["SOME_JSON_ENV_VAR", "ANOTHER_JSON_ENV_VAR"],
  packageJson,
  skipDotEnv,
  envJson: ["THIRD_JSON"],
});
export default config;

The above will (a) not read .env files, (b) take environment variables SOME_JSON_ENV_VAR and ANOTHER_JSON_ENV_VAR, JSON.parse() them and assign them as variables you can reach using config.SOME_JSON_ENV_VAR and ANOTHER_JSON_ENV_VAR, and (c) JSON.parse() THIRD_JSON in the same way, but also take each element of THIRD_JSON and assign them to process.env[element].

You can also store the config in your environment, requiring no code change when you add additional JSON variables, add expanded variables to process.env, or even use dotenv for local loading of the environment while turning it off in production (where you would supply the environment by IoC and not .env files in the container)

Here's the environment variable version of the above config options:

CONFIG_JS='{"jsonVariables":["SOME_JSON_ENV_VAR","ANOTHER_JSON_ENV_VAR"],"packageJson":{"name":"@richardpickett/config-js","version":"0.2.2"},"skipDotEnv":true,"envJson":["THIRD_JSON"]}'

Readme

Keywords

none

Package Sidebar

Install

npm i @richardpickett/config-js

Weekly Downloads

5

Version

1.0.1

License

MIT

Unpacked Size

69.5 kB

Total Files

8

Last publish

Collaborators

  • richard.pickett