Create config file on first use

25 views
Skip to first unread message

Noel Hibbard

unread,
Mar 19, 2016, 2:35:10 AM3/19/16
to nodejs
I am brand new to Node and so far things have gone smooth but I am trying to put some settings in a config.json and use require to load them. My problem is the first time the tool runs the file does not exist so I wrote some code to check to see if it exists and if not I create a default config file and then once it's created I load it with require. My problem is it seems to be executing require before my code that creates the default config and then throws an error.

Here is some sample code, I must be missing something basic:
var fs = require('fs');

fs.exists('./config.json', (exists) => {
    if(!exists)
    {
        fs.writeFile('./config.json', '{ "port" : "8282"}', function (err) {
        if (err) return console.log(err);
        });
    }
});

var config = require('./config.json');

console.log(config.port);

Thanks!

Daniel Rinehart

unread,
Mar 19, 2016, 10:26:20 AM3/19/16
to nodejs
A few comments:
1) Not sure what version of node you are using but fs.exists is deprecated with fs.stat being one of the recommended replacements.
2) Using either fs.exists or fs.stat both are asynchronous function so will not immediately execute the callback function which means the following require happens immediately.
3) If you did use the synchronous version of fs.stat, depending on how paranoid you want to be, there is a potential race condition between the stats check and require call.

An alternative approach would be to use the fact that require is a synchronous function and will throw an error with a specific code if the module can't be found:

var fs = require('fs');

// initialize with defaults
var config = {
  port: 8282
};
try {
  // attempt to override the defaults
  config = require('./config.json');
} catch (error) {
  // only handle a missing module error
  if (error.code !== 'MODULE_NOT_FOUND') {
    throw error;
  }
  // asynchronously write out the default config
  fs.writeFile('./config.json', JSON.stringify(config), function(error) {
    if (error) {
      throw error;
    }
  });
}

// at this point config will be the default if config.json didn't exist
// or the contents of config.json if it did exist
console.log(config);

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/68a2c764-ee59-441f-9cf0-e5dc89837513%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Noel Hibbard

unread,
Mar 22, 2016, 2:21:24 AM3/22/16
to nodejs
Thanks for the hint. I switched to using exitsSync and writeFileSync and that solved the problem. As you point out though, exists is depreciated so I need to switch to stat.

Thanks!
Reply all
Reply to author
Forward
0 new messages