Skip to content

Question: Is there a setting to allow fallback (default?) configuration if a config file does not exist? #382

@jagretz

Description

@jagretz

Hello, I wasn't able to find an answer to this question in other issues, so here it is:

I am wondering if there is a standard or recommended way to have the default configuration for an environment loaded with config.load or config.loadFile if a config file is not found? Using part of the example config.js from the docs to illustrate,

var convict = require('convict');

// Define a schema
var config = convict({
  env: {
    doc: 'The application environment.',
    format: ['production', 'development', 'test'],
    default: 'development',
    env: 'NODE_ENV'
  },
  ip: {
    doc: 'The IP address to bind.',
    format: 'ipaddress',
    default: '127.0.0.1',
    env: 'IP_ADDRESS',
  },
  port: {
    doc: 'The port to bind.',
    format: 'port',
    default: 8080,
    env: 'PORT',
    arg: 'port'
  }
});

// Load environment dependent configuration
var env = config.get('env');
config.loadFile('./config/' + env + '.json');

module.exports = config;

And say you have a development.json, test.json, and production.json.
Then you run your configuration with NODE_ENV=local. The script will fail with the error Cannot find module './config/local.json'.

Is there a way to have it use the default env setting development in this situation?

Quick aside. I know I can write my own "check if the file exists" function. I am wondering if there is already something built-in.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions