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。

.tg {border-collapse:collapse;border-spacing:0;border-color:#ccc;} .tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#ccc;color:#333;background-color:#fff;} .tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#ccc;color:#333;background-color:#f0f0f0;}.tg {border-collapse:collapse;border-spacing:0;border-color:#ccc;} .tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#ccc;color:#333;background-color:#fff;} .tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#ccc;color:#333;background-color:#f0f0f0;}

Sl No. Directive Description
1. name Unique Package name
2. version Package 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

package.json在Node JS应用程序中的重要性相关推荐

  1. node.js ejs_如何在Node.js应用程序中使用EJS模板

    node.js ejs by Jennifer Bland 詹妮弗·布兰德(Jennifer Bland) 如何在Node.js应用程序中使用EJS模板 (How to use EJS Templat ...

  2. rethinkdb_如何在Node.js应用程序中使用RethinkDB

    rethinkdb 这篇文章是由同行评审Agbonghama柯林斯和马丁·马丁内斯 . 感谢所有SitePoint的同行评审员使SitePoint内容达到最佳状态! Web应用程序最常见的任务之一就是 ...

  3. 如何在Node.js应用程序中使用RethinkDB

    这篇文章是由同行评审Agbonghama柯林斯和马丁·马丁内斯 . 感谢所有SitePoint的同行评审人员使SitePoint内容达到最佳状态! Web应用程序最常见的任务之一就是保存数据. 没有存 ...

  4. Node.js:项目中的 package.json 格式

    1. node.js 项目包下的 package.json : 实例: {"name" : "chatroooms","version" : ...

  5. 构建node.js基础镜像_在Android上构建Node.js应用程序

    构建node.js基础镜像 by Aurélien Giraud 通过AurélienGiraud 在Android上构建Node.js应用程序-第1部分:Termux,Vim和Node.js (Bu ...

  6. 如何调试Node.js应用程序?

    如何调试Node.js服务器应用程序? 现在,我主要使用带有以下打印语句的警报调试 : sys.puts(sys.inspect(someVariable)); 必须有更好的调试方法. 我知道Goog ...

  7. 942.ava.com_如何使用Ava.js测试Node.js应用程序

    942.ava.com by Nitish Phanse 由Nitish Phanse 如何使用Ava.js测试Node.js应用程序 (How you can test your Node.js a ...

  8. 如何在Docker上构建Node.js应用程序

    Docker has been the latest trending topic over the past couple of years and if you haven't been to s ...

  9. node.js api接口_如何在Node.js API客户端中正常处理故障

    node.js api接口 by Roger Jin 罗杰·金(Roger Jin) 如何在Node.js API客户端中正常处理故障 (How to gracefully handle failur ...

最新文章

  1. Windows 安装 MongoDB 5.0.2版本
  2. numpy.add详解
  3. 【数据结构与算法】之深入解析“随机翻转矩阵”的求解思路与算法示例
  4. c语言 变色程序,【图片】(原创)用纯C变了个变色输出字符的程序。。。【c语言吧】_百度贴吧...
  5. 7. OD-破解收费版限制天数的软件
  6. mysql 在查询语句返回的列中包含一列表示该条记录在整个结果集中的行号
  7. 摘自缪雪峰的博客的JavaScript个人笔记(3)
  8. [置顶] Android之Handler用法总结
  9. Android 杂七杂八记录
  10. kali系统sftp服务
  11. html文字和图片垂直居中,css如何让图片和文字垂直居中?
  12. Jenkins怎么发邮件,自动化大老手把手教你
  13. bugly怎么读_Bugly迁入
  14. PreScan 教程:0. PreScan与Matlab连接
  15. Linux常用库函数
  16. Swift CoreAnimation ---- CALayer的呈现层和模型层
  17. ant design vue 的 description组件中 label 默认样式的更改
  18. XiaoHu日志 6/17~6/19
  19. SpringBoot参数校验
  20. 【OpenCv/EmguCv】指针式仪表读数(一)

热门文章

  1. Android UI 事件研究
  2. 语音信号处理基础(一)
  3. [转载] Python数学实验与建模 课后习题第1章解析
  4. AOP 面向切面编程、拦截器
  5. jQuery读取和设定KindEditor值的方法
  6. BZOJ 4034 树上操作
  7. leetcode - 4. Median of Two Sorted Arrays
  8. 【转】关于OnPaint的工作机制
  9. GIT 查看/修改用户名和邮箱地址
  10. java并发:线程同步机制之计数器Exechanger