package.json在Node JS应用程序中的重要性

Before starting Node JS applications development, we should learn some basics and importance of package.json file. Every Node JS application or module or package should contain this package.json file.

在开始Node JS应用程序开发之前,我们应该学习package.json文件的一些基础知识和重要性。 每个Node JS应用程序,模块或程序包都应包含此package.json文件。

Prerequisite: You should have some basic knowledge about JSON file format, JSON data types etc.

先决条件 :您应该具有有关JSON文件格式,JSON数据类型等的一些基本知识。

什么是package.json? (What is package.json?)

package.json is a plain JSON(Java Script Object Notation) text file which contains all metadata information about Node JS Project or application.

package.json是一个普通的JSON(Java脚本对象表示法)文本文件,其中包含有关Node JS Project或应用程序的所有元数据信息。

Every Node JS Package or Module should have this file at root directory to describe its metadata in plain JSON Object format.

每个Node JS软件包或模块都应在根目录下具有此文件,以纯JSON对象格式描述其元数据。

We should use same file name with same case “package” and same file extension “*.json”.

我们应该使用相同的文件名,并使用相同的大小写“ package”和相同的文件扩展名“ * .json”。

Why that filename is “package”: because Node JS platform manages every feature as separate component. That component is also known as “Package” or “Module”.

为什么该文件名是“包”:因为Node JS平台将每个功能作为单独的组件进行管理。 该组件也称为“包装”或“模块”。

谁使用package.json文件? (Who uses package.json file?)

NPM (Node Package Manager) uses this package.json file information about Node JS Application information or Node JS Package details.

NPM(节点程序包管理器)使用有关节点JS应用程序信息或节点JS程序包详细信息的package.json文件信息。

package.json file contains a number of different directives or elements. It uses these directives to tell NPM “How to handle the module or package”.

package.json文件包含许多不同的指令或元素。 它使用这些指令告诉NPM“如何处理模块或包”。

package.json文件的强制性指令 (Mandatory Directives of package.json file)

package.json file contains a number of different directives or elements. Some are mandatory and some are optional directives.

package.json文件包含许多不同的指令或元素。 有些是强制性指令,有些是可选指令。

强制性指令 (Mandatory Directives)

ackage.json file contains two mandatory directives;

ackage.json文件包含两个强制指令;

  1. name

    名称
  2. version

name: It is a unique Node JS Package (Module) name or our Node JS Project name. This name should be in lower case letters. It is mandatory directive. Without this directive, we cannot install our Node JS Package by using NPM.

name :这是唯一的Node JS软件包(模块)名称或我们的Node JS Project名称。 此名称应使用小写字母。 这是强制性指令。 没有此指令,我们将无法使用NPM安装Node JS软件包。

JSON file follows key-value pair format. Key is of JSON String type and value may be of any JSON Data type. Key and Value elements are separated by colon “:”.

JSON文件遵循键值对格式。 密钥是JSON字符串类型,值可以是任何JSON数据类型。 键和值元素之间用冒号“:”分隔。

Here “name” directive’s value is of JSON String type.

在这里,“名称”指令的值是JSON字符串类型。

Example:

例:

"name":"simplename"

"name":"simplename"

When we run “npm install <package-name>” command, NPM reads package.json file available at root directory and checks that package name by reading this name directive. If it does not find this directive in package.json file then it throws an error message and cannot install that package.

当我们运行“ npm install <程序包名称>”命令时,NPM读取根目录中可用的package.json文件,并通过读取此名称指令来检查该程序包名称。 如果在package.json文件中找不到此指令,则会抛出错误消息,并且无法安装该软件包。

version: version is the Node JS Package version number. It is a Mandatory directive in package.json file. NPM uses this version number to install or uninstall or update the right package in our NODE JS Environment.

version :版本是Node JS软件包的版本号。 它是package.json文件中的Mandatory指令。 NPM使用此版本号在我们的NODE JS环境中安装或卸载或更新正确的软件包。

“version” directive’s value is of JSON String type.

“ version”指令的值是JSON String类型。

If NPM does not find this directive in package.json file then it throws an error message and cannot install that package.

如果NPM在package.json文件中找不到此指令,则它将引发错误消息,并且无法安装该软件包。

Example:

范例

"version":"1.0.0"
Here we need to define 3 parts of version: major, minor and patch

"version":"1.0.0"
在这里,我们需要定义版本的3个部分:主要,次要和补丁

Both name and version directives of package.json file identifies a Node JS Package uniquely.

package.json文件的name和version指令都唯一标识Node JS Package。

Sl No.DirectiveDescription
1.nameUnique Package name
2.versionPackage version number
Sl号 指示 描述
1。 名称 唯一的包裹名称
2。 套件版本号

Example of sample package.json file

示例package.json文件的示例

{
   "name" : "sample",
   "version" : "1.0.0"
}
可选指令 (Optional Directives)

package.json file may contain many optional directives. We should use them only if our Node JS package really requires. We will discuss some important optional directives here.

package.json文件可能包含许多可选指令。 仅当我们的Node JS软件包确实需要时,才应使用它们。 我们将在这里讨论一些重要的可选指令。

description: It is the description of the Node JS Project or module. We need to provide brief and concise description about “What this module does”.

description :这是Node JS项目或模块的描述。 我们需要提供有关“此模块的作用”的简要说明。

dependencies: Every Node JS Project or Module may dependent on other Node JS or Third Party Modules or our own Custom Modules. We should provide all those dependencies by using this “dependencies” directive.

依赖项 :每个Node JS项目或模块都可能依赖于其他Node JS或第三方模块或我们自己的自定义模块。 我们应该使用此“ dependencies”指令来提供所有这些依赖项。

“dependencies”: {
<Define all your Module Dependencies here in key-value pairs>
}

“依赖关系”:{
<在此处将键/值对定义所有模块依赖项>
}

Here key is module name and value is required module version. For example;

此处的键是模块名称,值是必需的模块版本。 例如;

"dependencies": {
      "mypackage": "1.0.0",
      "async": "0.8.0",
      "express": "4.2.x"
}

Complete package.json file example:

完整的package.json文件示例:

{
"name" : "sampleapp",
"version" : "1.0.0"'
"dependencies": {
	"mypackage": "1.0.0",
	"async": "0.8.0",
	"express": "4.2.x"
}
}

Here sample app is depending on 3 other Node JS Modules: mypackage (My own Custom Module), async and express (Node JS Modules)

这里的示例应用程序依赖于其他3个Node JS模块:mypackage(我自己的自定义模块),async和express(Node JS模块)

While defining dependencies version we can use some pattern matching syntax.

在定义依赖版本时,我们可以使用一些模式匹配语法。

Scenario 1: Any available version with * character

方案1 :任何带*字符的可用版本

"dependencies": {
   "mypackage": "*"
}

It will pickup any available version of mypackage module.

它将提取mypackage模块的任何可用版本。

Scenario 2: Range selection with ~ symbol

方案2 :使用〜符号选择范围

"dependencies": {
   "mypackage": "~0.2.0"
}

It pickup any available version of mypackage module between >=0.2.0 and < 0.3.0(Exclusive).

它获取> = 0.2.0到<0.3.0(独占)之间的mypackage模块的任何可用版本。

In development mode, we always want our Node JS application to use latest available module from Node JS repository.

在开发模式下,我们始终希望我们的Node JS应用程序使用Node JS存储库中的最新可用模块。

Scenario 3: Range selection with ^ symbol

方案3 :使用^符号选择范围

"dependencies": {
   "mypackage": "^0.2.0"
}

It will pickup any available version of mypackage module between >=0.2.0 and < 1.0.0(Exclusive).

它将获取在> = 0.2.0到<1.0.0(独占)之间的mypackage模块的任何可用版本。

repository: This directive is used to specify the URL where your source code is live for other people to contribute to your module or to learn your module. Its syntax is shown below.

repository :此伪指令用于指定源代码所在的URL,以供其他人对您的模块做出贡献或学习您的模块。 其语法如下所示。

"repository": {
   <Define  your URL here>
}

Here we need to define two elements:

在这里,我们需要定义两个元素:

  • Type of source to which you are going to provide URL

    您要提供URL的来源类型
  • url

    网址

We define these two elements in key-value pairs. Here key is element name and value is either type of source or url.

我们在键值对中定义这两个元素。 这里的键是元素名称,值是源类型或URL。

For example;

例如;

"repository": {
"type" : "git",
"url" : "https://github.com/journaldev/jd.git"
}

Complete package.json file example with mandatory and optional directives:

完整的package.json文件示例,带有强制和可选指令

{
	"name" : "sample",
	"version" : "1.0.0"'
	"repository": {
		"type" : "git",
		"url" : "https://github.com/journaldev/jd.git"
	},
	"dependencies": {
		"mypackage": "1.0.0",
		"async": "0.8.0",
		"express": "4.2.x"
	}
}

如何在Enide Studio IDE中创建package.json? (How to create package.json in Enide Studio IDE?)

We have already discussed in last post about how to setup Enide Studio 2014 IDE with Eclipse and also how to create a new Node.js project.

我们在上一篇文章中已经讨论过如何使用Eclipse 设置Enide Studio 2014 IDE以及如何创建新的Node.js项目。

When we create new Node.js Project in Enide Studio, it will create a package.json file automatically with default values as shown below.

当我们在Enide Studio中创建新的Node.js项目时,它将自动使用默认值创建一个package.json文件,如下所示。

We will use this package.json file knowledge in coming posts to develop our Node JS full-fledged Applications.

我们将在以后的文章中使用该package.json文件知识来开发我们的Node JS完整应用程序。

翻译自: https://www.journaldev.com/7553/importance-of-package-json-in-node-js-applications

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值