配置读取 development.json 时出现解析错误
Parse error when config reading the development.json
index.js 中需要配置模块,如下所示:
const config = require("config");
但是当使用 nodemon index.js
启动 nodejs 应用程序时会抛出错误。
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
应该很好用。
来自配置文档:
配置目录中的文件按以下顺序加载:
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.
由于我在 phone,我无法检查它是否正常工作,但我想这一定是文件名的问题。尝试使用 default.json
而不是 development.json
.
如果 development.json
或 production.json
为空,您可以删除它们或在每个文件中添加一个空花括号 ({}
)。
index.js 中需要配置模块,如下所示:
const config = require("config");
但是当使用 nodemon index.js
启动 nodejs 应用程序时会抛出错误。
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
应该很好用。
来自配置文档:
配置目录中的文件按以下顺序加载: 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.
由于我在 phone,我无法检查它是否正常工作,但我想这一定是文件名的问题。尝试使用 default.json
而不是 development.json
.
如果 development.json
或 production.json
为空,您可以删除它们或在每个文件中添加一个空花括号 ({}
)。