0
votes

I am using the config module for NodeJS. The configuration files are copied in the output direction properly. However, the config module still does not load them.

import config from 'config';

// Prints "undefined"
console.log(config);

The configuration files are in the config sub-directory:

+ config
  > default.json
  > development.json
1

1 Answers

0
votes

In your example, you are only 'requiring' the config package. You haven't actually asked for it to be loaded.

Typical use of the loading of config is based on the NODE_ENV value. This describes the environment you are running the node app under. E.g. production, development etc.

You can set the NODE_ENV value from the command line. E.g.

export NODE_ENV=development; node ./src/app.js

Then, in your code, you then assign an element from your config file into a var or constant. E.g.

const dbconfig = config.get('dbConfig');

So, using the below file as an example...

In /config/development.json

{
  "dbConfig": {
      "user": "some-user",
      "password": "somePassword",
      "connectString": "server:port/schema"
}

In the file you're trying to reading the config into...

import config from 'config';

const dbconfig = config.get('dbConfig');
console.log ("User is " + dbConfig.user); // should print 'User is some-user'

Don't forget to set the NODE_ENV value so that config knows what file to read from...

export NODE_ENV=development; node ./src/app.js

or, using your example

export NODE_ENV=default; node ./src/app.js