I have some questions regarding various rc
files in a typical node application, like .npmrc
, .babelrc
etc.
.[module]rc
naming convention or is it just a recommended format?[module]rc
would make it automatically available to the module? If so where will it be available?package.json
to load config. Which is recommended, package.json
or an rc file?gulpfile.js
with module.exports
? (I meant in the sense of recommendations, of course I know the difference and advantages of the js and rc files)Every time I search in google, I end up here and here, which is a tool to read rc file but doesn't explain what are they or how are they constructed and/or connected to the module.
Any insight would be really useful. Thanks
So first off, nicely-asked.
rc
dotfiles are configuration files that can vary in their use, formatting, and overall meaning. You can create .[whatever name you like]rc
files to inform whatever package you happen to be creating (provided another package isn't looking for the same one). Usually, they're useful for some sort of tool that acts on your source code and needs some tuning specific to your project. My understanding is that there were similar files that played an important role in UNIX systems in years past and the idea has stuck.
In short:
.[program or binary name]rc
package.json
files can contain external metadata appropriate for config, it just depends on whether or not your project will expect a .rc
file or expect it in package.json
(or both, as in the case of babel)See also:
As an incredibly-simple example:
Say you wanted to read this .foorc
file that uses JSON encoding:
{
"cool": true
}
You could do something like this:
'use strict';
const fs = require('fs');
fs.readFile('./.foorc', 'utf8', (err, data) => {
if (err) throw new Error(err);
console.log(JSON.parse(data));
})
There are far, far better ways to do this, but you could easily either write your own or find a package that would support YAML, ini, etc. parsing and provide some other nice bits of an API, too (for example, rc)