Home Web Front-end JS Tutorial Detailed introduction to Node.js packages_node.js

Detailed introduction to Node.js packages_node.js

May 16, 2016 pm 04:20 PM
node.js Bag

In the Node.js language, there is no essential difference between packages and modules. Packages are a deeper abstraction based on modules. Packages encapsulate an independent function and are used for publishing, updating, dependency management and Perform version control. Node.js implements the package mechanism according to the CommonJS specification, and npm was developed to solve the package publishing and acquisition requirements.

The package of Node.js is a directory that contains the package description file package.json in JSON format. The package of Node.js basically follows the CommonJS specification, so it has the following characteristics:

Package features defined by the CommonJS specification:

1) The top-level directory contains the package.json file;
2) The bin directory stores binary files;
3) The lib directory stores JavaScript files;
4) The doc directory stores documents;
5) The test directory stores unit tests.

Node.js modules and files have a one-to-one correspondence. Files can not only be JavaScript source files or binary files, but also directories. The simplest package is a directory module.

Node.js packages are usually a collection of modules, which provide a higher level of abstraction based on the modules, which is equivalent to a function library that provides some fixed interfaces.
By customizing package.json, we can create more complex, more complete, and more compliant packages for release.

When Node.js calls a package, it will first check the main field of the package.json file in the package and use it as the interface module of the package. If the main field of the package.json file does not exist, then Node.js will try Look for index.js or index.node as the interface of the package.

The package.json file is a file used by the CommonJS specification to describe a package. A package.json file that fully conforms to the specification should contain the following fields:
1) name: package name. The package name is unique and consists of lowercase letters, numbers and underscores, and cannot contain spaces.
2) description: package description. Give a brief description of the package.
3) version: version number. A version string that meets the "Semantic Version Identification" specification.
4) keywords: array of keywords, usually used for search.
5) maintainers: array of maintainers. Each element contains name, email (optional), web (optional) fields.
6) contributors: array of contributors. The format is the same as the maintainer array. The package author should be the first element of the contributors array.
7) Bugs: The address to submit bugs, which can be a website or email address.
8) licenses: license array. Each element should contain type (license name) and url (address link to the license text) fields.
9) repositories: array of warehouse hosting addresses. Each element must contain type (type of warehouse, such as Git), url (warehouse address) and path (path relative to the warehouse, optional) fields.
10) dependencies: package dependencies. Is an associative array consisting of package name and version number.

Note: The "Semantic Version Identification" specification is a set of version naming specifications proposed abroad. The original purpose was to solve various version number size comparison problems. It is currently adopted by many package management systems.

The following is a package.json example that fully complies with the CommonJS specification:

Copy code The code is as follows:

{  
  "name": "testpackage", 
  "description": "My package for CommonJS.", 
  "version": "0.1.0", 
  "keywords": [ 
     "testpackage", 
     "liq" 
  ],  
  "maintainers": [ 
     { 
        "name": "liq", 
        "email": "liq@hotmail.com", 
     } 
  ], 
  "contributors": [ 
     { 
        "name": "liq", 
        "web": "http://blog.csdn.net/chszs" 
     } 
  ], 
  "bugs": { 
     "mail": "liq@hotmail.com", 
     "web": "http://blog.csdn.net/chszs" 
  }, 
  "licenses": [ 
     { 
        "type": "Apache License v2", 
        "url": "http://www.apache.org/licenses/apache2.html" 
     } 
  ], 
  "repositories": [ 
     { 
        "type": "git", 
        "url": "http://github.com/chszs/packagetest.git" 
     } 
  ], 
  "dependencies": {  
     "webkit": "1.2", 
     "ssl": {  
        "gnutls": ["1.0", "2.0"], 
        "openssl": "0.9.8" 
     } 
  } 
}
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

An article about memory control in Node An article about memory control in Node Apr 26, 2023 pm 05:37 PM

The Node service built based on non-blocking and event-driven has the advantage of low memory consumption and is very suitable for handling massive network requests. Under the premise of massive requests, issues related to "memory control" need to be considered. 1. V8’s garbage collection mechanism and memory limitations Js is controlled by the garbage collection machine

Detailed graphic explanation of the memory and GC of the Node V8 engine Detailed graphic explanation of the memory and GC of the Node V8 engine Mar 29, 2023 pm 06:02 PM

This article will give you an in-depth understanding of the memory and garbage collector (GC) of the NodeJS V8 engine. I hope it will be helpful to you!

Let's talk in depth about the File module in Node Let's talk in depth about the File module in Node Apr 24, 2023 pm 05:49 PM

The file module is an encapsulation of underlying file operations, such as file reading/writing/opening/closing/delete adding, etc. The biggest feature of the file module is that all methods provide two versions of **synchronous** and **asynchronous**, with Methods with the sync suffix are all synchronization methods, and those without are all heterogeneous methods.

Let's talk about how to choose the best Node.js Docker image? Let's talk about how to choose the best Node.js Docker image? Dec 13, 2022 pm 08:00 PM

Choosing a Docker image for Node may seem like a trivial matter, but the size and potential vulnerabilities of the image can have a significant impact on your CI/CD process and security. So how do we choose the best Node.js Docker image?

Let's talk about the event loop in Node Let's talk about the event loop in Node Apr 11, 2023 pm 07:08 PM

The event loop is a fundamental part of Node.js and enables asynchronous programming by ensuring that the main thread is not blocked. Understanding the event loop is crucial to building efficient applications. The following article will give you an in-depth understanding of the event loop in Node. I hope it will be helpful to you!

What should I do if node cannot use npm command? What should I do if node cannot use npm command? Feb 08, 2023 am 10:09 AM

The reason why node cannot use the npm command is because the environment variables are not configured correctly. The solution is: 1. Open "System Properties"; 2. Find "Environment Variables" -> "System Variables", and then edit the environment variables; 3. Find the location of nodejs folder; 4. Click "OK".

Learn more about Buffers in Node Learn more about Buffers in Node Apr 25, 2023 pm 07:49 PM

At the beginning, JS only ran on the browser side. It was easy to process Unicode-encoded strings, but it was difficult to process binary and non-Unicode-encoded strings. And binary is the lowest level data format of the computer, video/audio/program/network package

Improve programming efficiency: optimize the use of Golang packages Improve programming efficiency: optimize the use of Golang packages Jan 16, 2024 am 10:46 AM

As artificial intelligence and cloud computing continue to advance, software development has become a vital part of today's business world. As an efficient and scalable programming language, Golang is increasingly favored by software developers. However, even when using Golang, developers must always guard the standards of program execution efficiency. In this article, we will focus on how to improve programming efficiency by optimizing the use of Golang packages. And, we will provide code examples to help readers better understand this

See all articles