Nodejs 中的‘ rc’文件是什么?

关于典型节点应用程序中的各种 rc文件,如 .npmrc.babelrc等,我有一些问题。

  • Rc 文件是什么,我知道它是模块的运行时配置,但是还有别的吗?
  • Rc 文件是否必须遵循 .[module]rc变数命名原则,还是只是推荐的格式?
  • 支持哪些格式?我已经看到了 yaml 和 json 格式,它是否依赖于模块使用的阅读器?
  • 如何从模块的角度访问 rc 文件?将它命名为 [module]rc是否会使它自动对模块可用?如果有的话,在哪里可以买到?
  • 或者模块应该访问该文件,就像使用该模块的应用程序中的任何其他文件一样,并期望该文件采用可理解的格式?(这就是我现在使用 json 格式所做的工作)
  • 我还看到人们需要 package.json来加载配置。哪个是推荐的,package.json还是 rc 文件?
  • 它与像 gulpfile.jsmodule.exports这样的 javascript 文件有什么不同?(我的意思是在推荐的意义上,当然我知道 js 和 rc 文件的区别和优势)

每次我在谷歌搜索,我结束了 给你给你,这是一个读取 rc 文件的工具,但没有解释它们是什么或如何构造和/或连接到模块。

任何线索都很有用,谢谢

17091 次浏览

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.

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)

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.