Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
config-ninja
Advanced tools
A quick and easy way to read in config files from disk depending on the environment. By default Config Ninja uses the NODE_ENV
environment variable to determine whether we are running in production
mode, otherwise development
mode is assumed.
Once your config has been initialised Config-Ninja allows you to const config = require('config-ninja');
in any of your modules and get access to your config object.
Important: The production
config is always the default config. If you specify another environment, Config Ninja will copy the properties from that environment into the production config, overwriting any values that already exist. Nested properties will be respected. No changes are saved to disk.
Create a directory to hold your config files and create a production.config.json
file which will contain all your configuration. Then create a development.config.json
file which will hold config specific to your development environment. Then in your index.js
:
// Prepare the ninja on application load.
const config = require('config-ninja').init('/path/to/cfg/dir/');
// In another module we need config again...
const config = require('config-ninja');
// Use our config.
console.dir(config);
console.log('Nested Number:', config.nested.number); // See examples.
See example.js
for a working example.
You will need at least 2 config files, one for production
and one for development
. You may also want config files for other environments such as staging
. You can have as many files as you need.
/myConfig
/production.config.json
/staging.config.json
/development.config.json
/custom.config.json
By default production
and development
environment strings are understood. If you have additional environments you can override the environment string by passing in a second parameter env
:
const config = require('config-ninja').init('/path/to/cfg/dir/', 'staging');
Where env
matches the name of your config file e.g. staging.config.json
.
You can also specify some options when instantiating Config-Ninja. All options are optional.
const config = require('config-ninja').init('/path/to/cfg/dir/', 'development', { ... });
// Pass in null as the second parameter to default to the NODE_ENV variable.
const config = require('config-ninja').init('/path/to/cfg/dir/', null, { ... });
Property | Default | Description |
---|---|---|
configInFilename | true | Set false if you want to your config filenames to be in the format of development.json instead of the default development.config.json . |
additionalMergeFiles[] | Specify a list of other filenames to merge into your config, if the files don't exist they will just be ignored. Properties in additional files will overwrite properties with the same name in your config. | |
ignoreMissingAdditional | true | By default we don't throw an error if an additional config file is missing. |
All these property names are reserved by Config-Ninja and cannot be used in your config files:
The environment string for a given config variable is stored under config._env
, so you can simply do if (config._env === 'production') { ... }
. Warning: If any of your config files contain a property called _env
this will not work.
Simply call config.init()
again without any parameters and your config files will be read from disk. Warning: Your config files must not contain any of the reserved property names in order for this to work.
Call config.init(null, 'new-environment-string');
This will reload the config with the new environment set.
Call config.init(null, null, { additionalMergeFiles: [ ... ] });
This will reload the config with your new additional merge files.
No. That's beyond the scope of this module.
FAQs
Environmental JSON config.
We found that config-ninja demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.