nodejs 中的 `rc` 文件是什么? [英] What are `rc` files in nodejs?

查看:118
本文介绍了nodejs 中的 `rc` 文件是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对典型节点应用程序中的各种 rc 文件有一些疑问,例如 .npmrc.babelrc 等.

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

  • 什么是 rc 文件,我知道它是模块的运行时配置,但还有别的吗?
  • rc 文件是否必须遵循 .[module]rc 命名约定,还是只是推荐的格式?
  • 支持哪些格式?yaml 和 json 格式我都看过,是不是看模块使用的读者?
  • 如何从模块的角度访问 rc 文件?将其命名为 [module]rc 会使其自动可供模块使用吗?如果是这样,它将在哪里提供?
  • 或者模块是否应该像使用该模块的应用程序中的任何其他文件一样访问该文件,并希望它采用可理解的格式?(这就是我现在正在做的,使用 json 格式)
  • 我还看到有人要求 package.json 加载配置.推荐使用 package.json 还是 rc 文件?
  • 另外,它与带有 module.exportsgulpfile.js 这样的 javascript 文件有何不同?(我说的是推荐意义上的,我当然知道js和rc文件的区别和优势)
  • 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)

每次我在谷歌搜索时,我都会在这里这里,这是一个读取 rc 文件的工具,但没有解释它们是什么或它们是如何构造和/或连接到模块的.

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

推荐答案

所以首先,问得好.

rc dotfile 是配置文件,它们的用途、格式和整体含义可能会有所不同.您可以创建 .[whatever name you like]rc 文件来通知您正在创建的任何包(前提是另一个包没有在寻找相同的包).通常,它们对于某种作用于您的源代码并且需要针对您的项目进行一些特定调整的工具很有用.我的理解是,过去几年在 UNIX 系统中曾有类似的文件发挥了重要作用,但这个想法一直停滞不前.

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.

简而言之:

  • 它们并非特定于节点.
  • 它们只是另一个文件
  • 就格式而言,它们几乎可以是任何东西——这取决于您将使用什么来解析和阅读它们.YAML、JSON 和 ini 可能是最常见的(至少我见过).
  • 在大多数情况下,它们似乎遵循约定.[程序或二进制名称]rc
  • package.json 文件可以包含适合配置的外部元数据,这取决于您的项目是否需要 .rc 文件或在 package.json(或两者皆有,如 babel)
  • 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)

另见:

举一个非常简单的例子:

As an incredibly-simple example:

假设你想读取这个使用 JSON 编码的 .foorc 文件:

Say you wanted to read this .foorc file that uses JSON encoding:

{
  "cool": true
}

你可以这样做:

'use strict';
const fs = require('fs');
fs.readFile('./.foorc', 'utf8', (err, data) => {
  if (err) throw new Error(err);
  console.log(JSON.parse(data));
})

有很多更好的方法可以做到这一点,但您可以轻松地编写自己的或找到一个支持 YAML、ini 等解析的包,并提供一些其他不错的 API(例如, rc)

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)

这篇关于nodejs 中的 `rc` 文件是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆