S
S
Sergey Murzin2016-12-03 13:54:05
Node.js
Sergey Murzin, 2016-12-03 13:54:05

How is it better to export settings from a file in Node, what are the rules?

Now:

// Настройки по умолчанию
var configDefault = {
  hello: "i dont understand",
  bye: "bye"
}
try {
  const configFile = require('./config');
  if(typeof gulpconfigFile === "object")
    var gulpconfig = Object.assign({}, configDefault, configFile)
} catch(e) {
  var config = configDefault;
}

And how to do the best?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin Kitmanov, 2016-12-03
@k12th

There is a powerful package: https://www.npmjs.com/package/config.
On your knee, you can simply create default.js, production.js, test.js and local.js, read requirethem from each other as needed and overwrite properties using Object.assign (that is, approximately as you have), and from index .js include the one you need, based on the environment variables.

A
Anton L, 2016-12-03
@antonecma

Yes, you have an acceptable option to some extent. Look towards this module https://github.com/flatiron/nconf

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question