loopback icon indicating copy to clipboard operation
loopback copied to clipboard

Environment-specific configuration server/config.{env}.json cannot load properly

Open mike-aungsan opened this issue 10 years ago • 11 comments

@bajtos @crandmck

I have 6 config files

server/config.json server/config.test.json server/config.production.json

server/config.local.js server/config.test.js server/config.production.js

config.{env}.js is used for errorHandler

NODE_ENV="test" node . NODE_ENV=test node .

It does not load the right config.

Another question is, can I use config.js for errorHandling rather then config.local.js shown in the doc.

server/datasources.{env}.json/js work and load the right file tho.

mike-aungsan avatar Feb 03 '16 19:02 mike-aungsan

It does not load the right config.

Please provide more details.

Ideally, create a sample application we can use to reproduce your problem, per our wiki instructions.

Possibly related: https://github.com/strongloop/loopback-boot/pull/71

bajtos avatar Feb 04 '16 09:02 bajtos

I've seen this happen due to a configuration issue, it's been discussed reguarding the explorer component in component-config.json. config.json is loaded first, then properties are merged in from the other env specific file. So be careful what you're trying to change, you can't replace an object with a boolean for example (like the explorer issue)

noderat avatar Feb 05 '16 11:02 noderat

@bajtos @noderat

Let say I have 2 config

config.json

{
    "restApiRoot": "/api",
    "host": "0.0.0.0",
    "port": 80,
    "blur": false
 }

config.test.json

{
    "restApiRoot": "/api",
    "host": "0.0.0.0",
    "port": 80,
    "test": true
}

If I start the server, NODE_ENV="test" node . app.get('test') should be true. app.get('blur') should be undefined

On the other hand, if the server is run, node . app.get('test') should be undefined

Either way, only config.json is loaded

mike-aungsan avatar Feb 11 '16 00:02 mike-aungsan

Is it possible to create additional properties inside a config.{env}.json/js file ? I'm not so sure

Overdrivr avatar Feb 17 '16 16:02 Overdrivr

@Overdrivr Yes additional properties can be added as shown in @mike-aungsan's example

noderat avatar Feb 18 '16 03:02 noderat

@noderat Yes, they can be added, but are you definitively sure they are taken into account ? Is it not mandatory to override a property that already exist in the default configuration ?

I have seen a stack overflow question where a database was defined only in alternate config, and that caused issues and was not taken into account in alternative config. I can try to find the post back if you want.

Overdrivr avatar Feb 18 '16 09:02 Overdrivr

I am currently adding additional properties in every single configuration .json within server for *.local.json, *.staging.json and *.production.json without any problems.

The biggest problem people run in to is trying to replace an object with a boolean or something similar. Because of this I heavily use the "enabled" booleans available in each configuration file rather than just setting an entire property to false.

noderat avatar Feb 18 '16 09:02 noderat

@noderat Thank you for clarifying that.

Overdrivr avatar Feb 18 '16 10:02 Overdrivr

looks like the issue has been resolve, closing issue now, please ping me here or open a new issue if as you see fit.

davidcheung avatar Nov 04 '16 18:11 davidcheung

@davidcheung I have the scenario for the issue Let say I have 2 config

config.json

{
    "restApiRoot": "/api",
    "host": "0.0.0.0",
    "port": 80,
    "blur": "disabled"
}

and config.test.json

{
    "restApiRoot": "/api",
    "host": "0.0.0.0",
    "port": 80,
    "blur": "allow"
}

and In the other hand, you added the example code for versioning your API.

var p = require('../package.json');
var version = p.version.split('.').shift();
module.exports = {
  restApiRoot: '/api' + (version > 0 ? '/v' + version : ''),
  host: process.env.HOST || 'localhost',
  port: process.env.PORT || 3000
};

when you try to get the value of blur property with

app.get('blue')

you get the value from the config.json, not from config.test.json. The issue occurs only when a javascript configuration file exists

richard-dev-mx avatar Jan 08 '18 22:01 richard-dev-mx

@rikantro thank you for chiming in. Could you please open a new issue and include a runnable application demonstrating the problem? See http://loopback.io/doc/en/contrib/Reporting-issues.html#bug-report

bajtos avatar Jan 15 '18 15:01 bajtos