0

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

4

1 回答 1

1

从配置的文档中:

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.

当我在打电话时,我无法检查它是否正常工作,但我想这一定是文件名的问题。尝试default.json改用development.json.

于 2019-03-06T21:21:10.337 回答