Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are `rc` files in nodejs?

I have some questions regarding various rc files in a typical node application, like .npmrc, .babelrc etc.

  • What is an rc file, I know its a runtime-config for the module, but anything else?
  • Does the rc file has to follow .[module]rc naming convention or is it just a recommended format?
  • What are the formats supported? I have seen both yaml and json formats, does it depend on the reader that the module use?
  • How to access an rc file from a module's perspective? Does naming it as [module]rc would make it automatically available to the module? If so where will it be available?
  • Or should the module access the file just like any other file from the app that is using the module and expect it to be in an understandable format? (This is what I am doing right now, with json format)
  • I have also seen people requiring package.json to load config. Which is recommended, package.json or an rc file?
  • Also how does it differ from a javascript file like 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

like image 743
Gopikrishna S Avatar asked Mar 25 '16 00:03

Gopikrishna S


People also ask

What are rc files for?

What is an RC file? RC files mostly belong to Visual Studio by Microsoft. An RC file is a resource script used by compilers for applications written in various programming languages. A resource script (RC) file contains information such as source file references, version information, and identifiers for an application.

What does NPM RC stand for?

It definitely does not mean Release candidate. The rc, taking from a ST post, can stand for various things, but I will take that in this context, it stands for Runtime configuration.

What does RC stand for config?

The rc at the end of a file is related to the phrase "run commands"; its use derives from the /etc/rc. * files used to start most Unix systems. The rc suffix is commonly used for any file that contains startup information for a program. Common rc files include .

Why do we need .npmrc file?

npmrc is the configuration file that npm allows to be used globally or user level or project level to optimize your npm environment.


1 Answers

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:

  • They're not specific to node.
  • They're just another file
  • As far as formats, they can be almost anything — it just depends on what you'll use to parse and read them. YAML, JSON, and ini are probably the most common (at least that I've seen).
  • In most cases they seem to follow the convention .[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:

  • What does "rc" mean in dot files
  • http://www.faqs.org/docs/artu/ch10s03.html#ftn.id2941902
  • https://en.wikipedia.org/wiki/Configuration_file

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)

like image 100
markthethomas Avatar answered Sep 30 '22 22:09

markthethomas