0

I have 2 questions, and I am asking two questions together since it is related to each other.

I am using sequelize-cli for migration, (I used sync({force: true}) before)

when I use the command

sequelize init

the config/config.json is created, and that json file is structured in the way of key-value pair, development / test / production.

// like below

{
    "development": {

    },
    "test": {

    },
    "production": {

    }
}

Before using sequelize migration, I had no idea of the concepts of development, test, production, thus I searched the articles about this concepts.

As a result, I have learned that it is recommended to set either NODE_ENV to development or production (or test).

My issue starts from here.

First, I used to use .env file only, with variables set inside that file (Before using sequelize migration, I had no experience using config folder. I used .env file only.)

since this was my first time using config.js file, I could not distinguish the role between .env file and config file.

So, while reading,

How to store Node.js deployment settings/configuration files?

I saw that process.env.something (to use variables set in .env file) being used inside the config file.

below is the code,

//config.js

config.twitter.user_name = process.env.TWITTER_USER || 'username';
config.twitter.password=  process.env.TWITTER_PASSWORD || 'password';

So the first question is that the .env file only set some secret variables, and config file uses that .env file(with process.env.) thus ultimately all the settings of my express application is inside of config file?

I am confused with the structure that seperates config file, and .env file.

I also read the article

https://goenning.net/2016/05/13/how-i-manage-application-configuration-with-nodejs/

according to that article,

there are development.js, production.js, test.js files inside of config folder.

but as I mentioned above, config.json file created from the command "sequelize init", has a key-value pair. (development, test, production).

vice versa, the article just above shows that config folder has files each for development, test, production.

So my second question is that, do I have to delete that config.json file which was created from "sequelize init" command, and integrate them to each files? (there may be no 100% correct-answer, but I want to know the most-widely-used and efficient way)

From now on, I even do not know how to setup the NODE_ENV, but I will keep searching for it.

Thanks for reading this long question, and it would be really appreciated to give me some advice on this issue.

jwkoo
  • 2,393
  • 5
  • 22
  • 35

1 Answers1

1

That file (config) is the actual configuration file that will be used. That file just happens to pull some values from the env. This allows you to not have to change the config files themselves when you have a project on different servers. You can just have a different.env file on each host that specifies its unique values for those.

Imagine you are working with other people. They will have a different configuration for their database. Having to hardcode values into the config file that is committed to a repository would end up with some problems as they pull down changes their configs might get overwritten. The .env file is not committed and should not be as its unique to the 'host'.

By having the config files pull values from env, each person (host) can have their own .env file with their own values without having to make changes to the actual config files.

I found from here: https://laravel.io/forum/05-28-2016-env-file-vs-configdatabasephp

This answer is not just for PHP but all the language apply this king of concept.

Jignesh Sanghani
  • 602
  • 6
  • 12
  • Appreciate for your detailed-explanation. Any ideas regarding to my second issue? thanks again. – jwkoo Mar 10 '18 at 16:30