node.js 向 package.json 添加自定义元数据或配置,是否有效?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10065564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:28:46  来源:igfitidea点击:

Add custom metadata or config to package.json, is it valid?

node.js

提问by Lance Pollard

I have seen (don't remember where) a package.json file with custom keys starting with an underscore:

我见过(不记得在哪里)一个 package.json 文件,其中包含以下划线开头的自定义键:

{
    "name": "application-name"
  , "version": "0.0.1"
  , "private": true
  , "dependencies": {
      "express": "2.4.7"
    , "jade": ">= 0.0.1"
  }
  , "_random": true
}

Are you allowed to do this? Is it still valid? If this is allowed, is there any documentation on the rules?

你被允许这样做吗?它仍然有效吗?如果允许,是否有任何有关规则的文件?

Thanks!

谢谢!

回答by mklement0

tl;dr:

tl;博士

  • Yes, you're allowedto add custom entries to package.json.
  • Choose a key name:
    • not already defined(details below)
    • not reservedfor future use (details below)
    • avoidprefixes _and $
    • and preferably use a singletop-level keyin which to nestyour custom entries.
  • 是的,您可以将自定义条目添加到package.json.
  • 选择一个键名:
    • 尚未定义(详情如下)
    • 不保留供将来使用(详情如下)
    • 避免前缀_$
    • 并且最好使用单个顶级键嵌套您的自定义条目

E.g., if you own domain example.org, you could store a custom randomkey as follows, inside a top-level key in reverse-domain-name notation with _substituted for .and, if applicable, -(see comments)(e.g., org_example):

例如,如果您拥有 domain example.org,您可以random按如下方式存储自定义键,在反向域名表示法的顶级键中_.并替换为和,如果适用,-(请参阅评论)(例如,org_example):

{
    "name": "application-name"
  , "version": "0.0.1"
  , "private": true
  , "dependencies": {
      "express": "2.4.7"
    , "jade": ">= 0.0.1"
  }  
  , "org_example": {
      "random": true
  }
}


npm's package.jsonfile format mostly complies with the CommonJS package specification:

npmpackage.json文件格式主要符合CommonJS 包规范

As for choosing custom keys: the CommonJS package specificationstates (emphasis mine):

至于选择自定义键CommonJS 包规范指出(强调我的):

The following fields are reservedfor futureexpansion: build, default, email, external, files, imports, maintainer, paths, platform, require, summary, test, using, downloads, uid.

Extensions to the package descriptor specification should strive to avoid collisions for future standard names by name-spacing their properties with innocuous names that do not have meanings relevant to general package management.

The following fields are reserved for package registriesto use at their discretion: id, type. All properties beginning with _or $are also reservedfor package registries to use at their discretion.

以下字段保留用于未来扩展:build, default, email, external, files, imports, maintainer, paths, platform, require, summary, test, using, downloads, uid.

包描述符规范的扩展应努力避免未来标准名称的冲突,方法是将它们的属性命名为与一般包管理无关的无害名称

以下字段保留供软件包注册中心自行决定使用:id, type. _或开头的$所有属性也保留供包注册中心自行决定使用。

回答by Octavian A. Damiean

Given the nature of JSON and this statement from the Nodejitsu documentationI don't see anything wrong with that.

鉴于 JSON 的性质和Nodejitsu 文档中的这一声明,我认为这没有任何问题。

NPM itself is only aware of twofields in the package.json:

{
   "name" : "barebones",
   "version" : "0.0.0",
}

NPM 本身只知道package.json 中的两个字段:

{
   "name" : "barebones",
   "version" : "0.0.0",
}

NPM also cares about a couple of fields listed here. So as long as it is valid JSON and doesn't interfere with Node.js or NPM everything should be alright and valid.

NPM 还关心这里列出的几个字段。因此,只要它是有效的 JSON 并且不干扰 Node.js 或 NPM,一切都应该没问题且有效。

Node's awareness of package.json files seems extends to the mainfield. Ref.

Node 对 package.json 文件的认识似乎扩展到了main领域。参考

 { "name" : "some-library",
   "main" : "./lib/some-library.js" }

If this was in a folder at ./some-library, then require('./some-library') would attempt to load ./some-library/lib/some-library.js.

This is the extent of Node's awareness of package.json files.

 { "name" : "some-library",
   "main" : "./lib/some-library.js" }

如果这是在 ./some-library 的文件夹中,那么 require('./some-library') 将尝试加载 ./some-library/lib/some-library.js。

这就是 Node 对 package.json 文件的认识程度。

To avoid possible conflicts you should prefixing your keys with some character or word. An underscore is a common variant.

为避免可能的冲突,您应该在键前加上一些字符或单词。下划线是一种常见的变体。