向 package.json 添加自定义元数据或配置,是否有效? [英] Add custom metadata or config to package.json, is it valid?

查看:124
本文介绍了向 package.json 添加自定义元数据或配置,是否有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<代码>{名称":应用程序名称", "版本": "0.0.1", "私人": 真,依赖关系":{快递":2.4.7", "玉": ">= 0.0.1"}, "_random": 真}

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

谢谢!

解决方案

tl;dr:

  • 是的,您可以package.json 添加自定义条目.
  • 选择一个键名:
    • 尚未定义(详情如下)
    • 未保留供将来使用(详情如下)
    • 避免前缀_$
    • 最好使用单个顶级键,在其中嵌套您的自定义条目.立>

例如,如果您拥有域 example.org,您可以按如下方式存储自定义 random 键,在反向域的顶级键中-名称符号用 _ 代替 . 和,如果适用,-(见评论)(例如,org_example):

{名称":应用程序名称",版本":0.0.1";,私人":真,依赖关系":{快递":2.4.7",玉":>=0.0.1";},org_example":{随机":真}}

要读取此类自定义属性,请使用以下技术:

require("./package.json").org_example.random//->真的


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

至于选择自定义键:CommonJS 包规范a> 状态(强调我的):

<块引用>

以下字段为未来扩展保留:builddefaultemailexternalfilesimportsmaintainerpathsplatformrequiresummarytestusingdownloads, uid.

<块引用>

包描述符规范的扩展应该通过使用与一般包管理无关的无害名称来命名它们的属性,努力避免未来标准名称的冲突.

<块引用>

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

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

{
    "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!

解决方案

tl;dr:

  • Yes, you're allowed to add custom entries to package.json.
  • Choose a key name:
    • not already defined (details below)
    • not reserved for future use (details below)
    • avoid prefixes _ and $
    • and preferably use a single top-level key in which to nest your custom entries.

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

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

To read such custom properties, use the following technique:

require("./package.json").org_example.random // -> true


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

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

The following fields are reserved for future expansion: 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 registries to use at their discretion: id, type. All properties beginning with _ or $ are also reserved for package registries to use at their discretion.

这篇关于向 package.json 添加自定义元数据或配置,是否有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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