The config module is required in the index.js as this:
const config = require("config");
But it throws an error when starting nodejs app with nodemon index.js
.
C:\d\code\js\emps_backend\node_modules\config\lib\config.js:838
throw new Error("Cannot parse config file: '" + fullFilename + "': " + e3);
^
Error: Cannot parse config file: 'C:\d\code\js\emps_backend\config\development.json': SyntaxError: Unexpected token } in JSON at position 45
at Config.util.parseFile (C:\d\code\js\emps_backend\node_modules\config\lib\config.js:838:11)
at C:\d\code\js\emps_backend\node_modules\config\lib\config.js:600:28
at Array.forEach (<anonymous>)
at C:\d\code\js\emps_backend\node_modules\config\lib\config.js:596:14
at Array.forEach (<anonymous>)
at Config.util.loadFileConfigs (C:\d\code\js\emps_backend\node_modules\config\lib\config.js:595:13)
at new Config (C:\d\code\js\emps_backend\node_modules\config\lib\config.js:136:27)
at Object.<anonymous> (C:\d\code\js\emps_backend\node_modules\config\lib\config.js:1643:31)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
[nodemon] restarting due to changes...
[nodemon] starting `node index.js`
The development.json
is very simple:
{
"PORT":3000,
"DB_PASSWORD":"password"
}
If the content of the development.json
is removed and becomes {}
, config throws another error.
C:\d\code\js\emps_backend\node_modules\config\lib\config.js:1194
throw Error(msg);
^
Error: Illegal key type for substitution map at : number
at _substituteVars (C:\d\code\js\emps_backend\node_modules\config\lib\config.js:1194:15)
at Config.util.substituteDeep (C:\d\code\js\emps_backend\node_modules\config\lib\config.js:1199:3)
at C:\d\code\js\emps_backend\node_modules\config\lib\config.js:1219:43
at Array.forEach (<anonymous>)
at Config.util.getCustomEnvVars (C:\d\code\js\emps_backend\node_modules\config\lib\config.js:1215:12)
at Config.util.loadFileConfigs (C:\d\code\js\emps_backend\node_modules\config\lib\config.js:653:28)
at new Config (C:\d\code\js\emps_backend\node_modules\config\lib\config.js:136:27)
at Object.<anonymous> (C:\d\code\js\emps_backend\node_modules\config\lib\config.js:1643:31)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
Here is the package.json
:
{
"name": "emps_backend",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "jest --watchAll --verbose "
},
"author": "",
"license": "ISC",
"dependencies": {
"config": "^3.0.1",
"cors": "^2.8.5",
"ejs": "^2.6.1",
"express": "^4.16.4",
"express-async-errors": "^3.1.1",
"joi": "^14.3.1",
"jsonwebtoken": "^8.4.0",
"moment": "^2.24.0",
"nexmo": "^2.4.1",
"nodemon": "^1.18.9",
"pg": "^7.8.0",
"pg-hstore": "^2.3.2",
"randomstring": "^1.1.5",
"sequelize": "^4.42.0",
"socketio": "^1.0.0",
"winston": "^3.2.1"
},
"devDependencies": {
"jest": "^24.1.0",
"postman": "^0.2.0",
"supertest": "^3.4.2"
}
}
What causes the error? config
should be very easy to use.
From config’s documentation:
Files in the config directory are loaded in the following order: default.EXT
default-{instance}.EXT
{deployment}.EXT
{deployment}-{instance}.EXT
{short_hostname}.EXT
{short_hostname}-{instance}.EXT
{short_hostname}-{deployment}.EXT
{short_hostname}-{deployment}-{instance}.EXT
{full_hostname}.EXT
{full_hostname}-{instance}.EXT
{full_hostname}-{deployment}.EXT
{full_hostname}-{deployment}-{instance}.EXT
local.EXT
local-{instance}.EXT
local-{deployment}.EXT
local-{deployment}-{instance}.EXT
(Finally, custom environment variables can override all files)
Where
• EXT can be .yml, .yaml, .xml, .coffee, .cson, .properties, .json, .json5, .hjson, .ts or .js depending on the format you prefer (see below)
• {instance} is an optional instance name string for Multi-Instance Deployments
• {short_hostname} is your server name up to the first dot, from the $HOST or $HOSTNAME environment variable or os.hostname() (in that order). For example if your hostname is www.example.com then it would load www.EXT.
• {full_hostname} is your whole server name, you may use this when {short_hostname} collides with other machines.
• {deployment} is the deployment name, from the $NODE_ENV (or if specified, $NODE_CONFIG_ENV) environment variable
The default.EXT file is designed to contain all configuration parameters from which other files may overwrite. Overwriting is done on a parameter by parameter basis, so subsequent files contain only the parameters unique for that override.
{hostname} and {deployment} files allow you to tune configurations for a particular server or deployment. These files are designed to live along with other files in your version control system.
As I am on the phone, I could not check if it is working, but I guess it must be an issue with the filename. Try to use default.json
instead development.json
.