What are `rc` files in nodejs?
Asked Answered
S

3

111

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

Scoff answered 25/3, 2016 at 0:28 Comment(2)
There is nothing special about these files, different modules "just happen" to use a similar strategy.Inspectorate
Related post - What does the 'rc' in .bashrc, etc. mean?Lindon
F
72

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:

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)

Filly answered 25/3, 2016 at 0:47 Comment(2)
So the package would access it like any other file right? require('./.modulerc')?Scoff
Depends on what you mean by "package", but yes. I'll update w/ simple exampleFilly
T
26

It's not specific to Node or Babel, but *rc files are generally configuration files in Unix systems

From Wikipedia

Configuration files also do more than just modify settings, they often (in the form of an "rc file") run a set of commands upon startup (for example, the "rc file" for a shell might instruct the shell to change directories, run certain programs, delete or create files — many things which do not involve modifying variables in the shell itself and so were not in the shell's dotfiles). This convention is borrowed from "runcom files" on the CTSS operating system.

This functionality can and has been extended for programs written in interpreted languages such that the configuration file is actually another program rewriting or extending or customizing the original program; Emacs is the most prominent such example.

The "rc" naming convention of "rc files" was inspired by the "runcom" facility mentioned above and does not stand for "resource configuration", "runtime configuration", or "remote control" as is often wrongly guessed.

"rc" files are traditionally files which end in the "(.)rc" suffix and which contain data and information that is used as configuration information for the associated program. Typically the name of that program is the first part of the rc file's name, with the "(.)rc" suffix being used to indicate the file's purpose, e.g. ".xinitrc", ".vimrc", ".bashrc", "xsane.rc".

And Runcom

Unix: from runcom files on the CTSS system 1962-63, via the startup script /etc/rc

Script file containing startup instructions for an application program (or an entire operating system), usually a text file containing commands of the sort that might have been invoked manually once the system was running but are to be executed automatically each time the system starts up. See also dot file.

In other words, "rc" is just something that stuck from back in the sixties, and has been used quite often for configuration files in different sorts of programs since, including Node, Babel and many, many others.

There's nothing special about "rc" files, and they can virtually contain any kind of data, there's no specification or other restrictions.

Translunar answered 25/3, 2016 at 0:40 Comment(2)
Hmm .. so it all depends on the reader to decide what to do, right? Any recommended way of accessing it?Scoff
@GopikrishnaS - As an "*rc" file can be almost anything, javascript, JSON, plain text, XML, YAML or whatever, how you parse the file depends entirely on what it contains.Translunar
B
11

RC refers to

  • run commands
  • run-time configuration

https://en.wikipedia.org/wiki/Run_commands

The ‘rc’ suffix goes back to Unix's grandparent, CTSS. It had a command-script feature called "runcom". Early Unixes used ‘rc’ for the name of the operating system's boot script, as a tribute to CTSS runcom.

Bagger answered 6/3, 2017 at 10:54 Comment(4)
there are no references to "RC", "runcom", or "CTSS" in en.wikipedia.org/wiki/Run_commandAba
@Aba you are referring wrong url. Run_command instead of Run_commands. @rselvagenesh given correct url.Milt
@bala The URL in the answer was edited after my commentAba
@Aba ok got it.Milt

© 2022 - 2024 — McMap. All rights reserved.