Модуль config требуется в index.js следующим образом:
const config = require("config");
Но выдает ошибку при запуске приложения nodejs с 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`
development.json
очень просто:
{
"PORT":3000,
"DB_PASSWORD":"password"
}
Если содержимое development.json
удаляется и становится {}
, config выдает еще одну ошибку.
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)
Вот это 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"
}
}
Что вызывает ошибку? config
должно быть очень простым в использовании.