简体   繁体   中英

config(npm) throws configuration property not found but getConfigSources method logs correct object

I am trying to integrate config package in my nx.dev typescript project. I have a config directory at root level that contains default.json

{
  "server": { "port": 3001 }
}

in my index.ts I try

import { get, util } from 'config';

console.log(util.getConfigSources());
console.log(get('server'));

and get the following:

[
  {
    name: 'path to config file.. /config/default.ts',
    original: '{\n  "server": {\n    "port": 3001\n  }\n}',
    parsed: { server: [Object] }
  }
]

Error: Configuration property "server" is not defined

And everywhere it says this is all I need to hook it up but get function throws.

The node-config get method is designed-- and documented-- to be called as a method of a config object. Try using the documented import syntax:

const config = require('config');
console.log(config.get('server'));

An object method can't be expected to work the same when called as a function in JavaScript. An object method has this bound to the object. A function does not, unless bind() has been explicitly called on the function to bind it to an object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM