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
应该很容易使用。