Home Web Front-end JS Tutorial Several npm dependency package management sharing

Several npm dependency package management sharing

Jan 27, 2018 pm 01:52 PM
rely share manage

This article mainly introduces you to several types of npm dependency package management that you should know. npm is the package manager in node.js and is a command line tool. The article introduces it in detail through sample code. Friends who need it You can use it as a reference, and follow the editor to learn together.

npm currently supports the following types of dependency package management:

  • dependencies

  • ##devDependencies

  • peerDependencies

  • optionalDependencies

  • bundledDependencies / bundleDependencies

If you Which kind of dependency management you want to use, then you can put it in the corresponding dependency object in package.json, such as:

 "devDependencies": {
 "fw2": "^0.3.2",
 "grunt": "^1.0.1",
 "webpack": "^3.6.0"
 },
 "dependencies": {
 "gulp": "^3.9.1",
 "hello-else": "^1.0.0"
 },
 "peerDependencies": { },
 "optionalDependencies": { },
 "bundledDependencies": []
Copy after login
Let’s look at it one by one:

dependencies


Application dependencies, or business dependencies, are our most commonly used dependency package management objects! It is used to specify the external packages that the application depends on. These dependencies are required for normal execution after the application is released, but do not include packages used during testing or local packaging. You can use the following command to install:

npm install packageName --save
Copy after login
dependencies is a simple JSON object, including the package name and package version, where the package version can be a version number or a URL address. For example:

{ 
 "dependencies" :{ 
 "foo" : "1.0.0 - 2.9999.9999", // 指定版本范围
 "bar" : ">=1.0.2 <2.1.2", 
 "baz" : ">1.0.2 <=2.3.4", 
 "boo" : "2.0.1", // 指定版本
 "qux" : "<1.0.0 || >=2.3.1 <2.4.5 || >=2.5.2 <3.0.0", 
 "asd" : "http://asdf.com/asdf.tar.gz", // 指定包地址
 "til" : "~1.2", // 最近可用版本
 "elf" : "~1.2.3", 
 "elf" : "^1.2.3", // 兼容版本
 "two" : "2.x", // 2.1、2.2、...、2.9皆可用
 "thr" : "*", // 任意版本
 "thr2": "", // 任意版本
 "lat" : "latest", // 当前最新
 "dyl" : "file:../dyl", // 本地地址
 "xyz" : "git+ssh://git@github.com:npm/npm.git#v1.0.27", // git 地址
 "fir" : "git+ssh://git@github.com:npm/npm#semver:^5.0",
 "wdy" : "git+https://isaacs@github.com/npm/npm.git",
 "xxy" : "git://github.com/npm/npm.git#v1.0.27",
 }
}
Copy after login
devDependencies


Development environment dependencies, second only to the frequency of use of dependencies! Its object definition is the same as dependencies, except that the packages in it are only used in the development environment, not in the production environment. These packages are usually unit tests or packaging tools, such as gulp, grunt, webpack, moca, coffee, etc., which can be used The following command is used to install:

npm install packageName --save-dev
Copy after login
For example:

{ "name": "ethopia-waza",
 "description": "a delightfully fruity coffee varietal",
 "version": "1.2.3",
 "devDependencies": {
 "coffee-script": "~1.6.3"
 },
 "scripts": {
 "prepare": "coffee -o lib/ -c src/waza.coffee"
 },
 "main": "lib/waza.js"
}
Copy after login
The prepare script will be run before publishing, so users do not need to rely on it when compiling the project. In development mode, running npm install will also execute the prepare script, which can be easily tested during development.

So far, do you understand the difference between --save and --save-dev?


peerDependencies


Equal dependencies, or peer dependencies, are used to specify host versions that are compatible with the current package (that is, the package you wrote). How to understand it? Just imagine, we write a gulp plug-in, but gulp has multiple main versions. We only want to be compatible with the latest version. At this time, we can use peerDependencies to specify:

{
 "name": "gulp-my-plugin",
 "version": "0.0.1",
 "peerDependencies": {
 "gulp": "3.x"
 }
}
Copy after login
When others use When we add a plug-in, peerDependencies will tell the user clearly which host version you need to install the plug-in.

Normally, we will use many plug-ins of one host (such as gulp) in a project. If there is host incompatibility between each other, when executing npm install, the cli will throw an error message to tell Let's, for example:

npm ERR! peerinvalid The package gulp does not satisfy its siblings' peerDependencies requirements!
npm ERR! peerinvalid Peer gulp-cli-config@0.1.3 wants gulp@~3.1.9
npm ERR! peerinvalid Peer gulp-cli-users@0.1.4 wants gulp@~2.3.0
Copy after login
Run the command npm install gulp-my-plugin --save-dev to install our plug-in. Let's take a look at the dependency graph:

├── gulp-my-plugin@0.0.1
└── gulp@3.9.1
Copy after login
OK, Nice!

Note that npm 1 and npm 2 will automatically install the same dependencies. npm 3 will no longer be automatically installed and a warning will be generated! Manually adding dependencies in package.json file can be solved.


optionalDependencies


Optional dependencies, if there are some dependent packages that fail to install, but the project can still run or you want npm to continue running, you can use optionalDependencies. In addition, optionalDependencies will overwrite the dependency package with the same name in dependencies, so do not write it in both places.

For example, an optional dependency package is like a plug-in for a program. If it exists, the existing logic will be executed. If it does not exist, another logic will be executed.

try {
 var foo = require('foo')
 var fooVersion = require('foo/package.json').version
} catch (er) {
 foo = null
}
if ( notGoodFooVersion(fooVersion) ) {
 foo = null
}

// .. then later in your program ..

if (foo) {
 foo.doFooThings()
}
Copy after login
bundledDependencies / bundleDependencies


Packaging dependencies, bundledDependencies is an array object containing dependent package names. When publishing, the packages in this object will be packaged into the final release package. . For example:

{
 "name": "fe-weekly",
 "description": "ELSE 周刊",
 "version": "1.0.0",
 "main": "index.js",
 "devDependencies": {
 "fw2": "^0.3.2",
 "grunt": "^1.0.1",
 "webpack": "^3.6.0"
 },
 "dependencies": {
 "gulp": "^3.9.1",
 "hello-else": "^1.0.0"
 },
 "bundledDependencies": [
 "fw2",
 "hello-else"
 ]
}
Copy after login
Execute the packaging command npm pack, and the generated fe-weekly-1.0.0.tgz package will include fw2 and hello-else. However, it is worth noting that these two packages must be declared in devDependencies or dependencies first, otherwise the packaging will report an error.

Related recommendations:


Spring Boot introduces dependency package Druid

Detailed explanation of npm and webpack configuration methods in node.js

Share how to use the latest version of nodejs to install npm

The above is the detailed content of Several npm dependency package management sharing. For more information, please follow other related articles on the PHP Chinese website!

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)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months 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)

How to share Quark Netdisk to Baidu Netdisk? How to share Quark Netdisk to Baidu Netdisk? Mar 14, 2024 pm 04:40 PM

Quark Netdisk and Baidu Netdisk are very convenient storage tools. Many users are asking whether these two softwares are interoperable? How to share Quark Netdisk to Baidu Netdisk? Let this site introduce to users in detail how to save Quark network disk files to Baidu network disk. How to save files from Quark Network Disk to Baidu Network Disk Method 1. If you want to know how to transfer files from Quark Network Disk to Baidu Network Disk, first download the files that need to be saved on Quark Network Disk, and then open the Baidu Network Disk client. , select the folder where the compressed file is to be saved, and double-click to open the folder. 2. After opening the folder, click "Upload" in the upper left corner of the window. 3. Find the compressed file that needs to be uploaded on your computer and click to select it.

How to share NetEase Cloud Music to WeChat Moments_Tutorial on sharing NetEase Cloud Music to WeChat Moments How to share NetEase Cloud Music to WeChat Moments_Tutorial on sharing NetEase Cloud Music to WeChat Moments Mar 25, 2024 am 11:41 AM

1. First, we enter NetEase Cloud Music, and then click on the software homepage interface to enter the song playback interface. 2. Then in the song playback interface, find the sharing function button in the upper right corner, as shown in the red box in the figure below, click to select the sharing channel; in the sharing channel, click the &quot;Share to&quot; option at the bottom, and then select the first &quot;WeChat Moments&quot; allows you to share content to WeChat Moments.

How to use Redis to implement distributed transaction management How to use Redis to implement distributed transaction management Nov 07, 2023 pm 12:07 PM

How to use Redis to implement distributed transaction management Introduction: With the rapid development of the Internet, the use of distributed systems is becoming more and more widespread. In distributed systems, transaction management is an important challenge. Traditional transaction management methods are difficult to implement in distributed systems and are inefficient. Using the characteristics of Redis, we can easily implement distributed transaction management and improve the performance and reliability of the system. 1. Introduction to Redis Redis is a memory-based data storage system with efficient read and write performance and rich data

How to share files with friends on Baidu Netdisk How to share files with friends on Baidu Netdisk Mar 25, 2024 pm 06:52 PM

Recently, Baidu Netdisk Android client has ushered in a new version 8.0.0. This version not only brings many changes, but also adds many practical functions. Among them, the most eye-catching is the enhancement of the folder sharing function. Now, users can easily invite friends to join and share important files in work and life, achieving more convenient collaboration and sharing. So how do you share the files you need to share with your friends? Below, the editor of this site will give you a detailed introduction. I hope it can help you! 1) Open Baidu Cloud APP, first click to select the relevant folder on the homepage, and then click the [...] icon in the upper right corner of the interface; (as shown below) 2) Then click [+] in the &quot;Shared Members&quot; column 】, and finally check all

CentOS installation gnuplot and CentOS installation sunflower are missing dependencies CentOS installation gnuplot and CentOS installation sunflower are missing dependencies Feb 13, 2024 pm 11:39 PM

LINUX is a widely used operating system that is highly customizable and stable. CentOS is a free and open source operating system built on the Red Hat Enterprise Linux (RHEL) source code. It is widely used in servers and desktop environments. In CentOS Installing software packages on CentOS is one of the common tasks in daily use. This article will introduce how to install gnuplot on CentOS and solve the problem of missing dependencies of Sunflower software. Gnuplot is a powerful drawing tool that can generate various types of charts, including two-dimensional and three-dimensional data visualization. To install gnuplot on CentOS, you can follow the steps below: 1.

Mango tv member account sharing 2023 Mango tv member account sharing 2023 Feb 07, 2024 pm 02:27 PM

Mango TV has various types of movies, TV series, variety shows and other resources, and users can freely choose to watch them. Mango TV members can not only watch all VIP dramas, but also set the highest definition picture quality to help users watch dramas happily. Below, the editor will bring you some free Mango TV membership accounts for users to use, hurry up and take a look Take a look. Mango TV latest member account free sharing 2023: Note: These are the latest member accounts collected, you can log in directly and use them, do not change the password at will. Account number: 13842025699 Password: qds373 Account number: 15804882888 Password: evr6982 Account number: 13330925667 Password: jgqae Account number: 1703

What to do if the right-click menu management cannot be opened in Windows 10 What to do if the right-click menu management cannot be opened in Windows 10 Jan 04, 2024 pm 07:07 PM

When we use the win10 system, when we use the mouse to right-click the desktop or the right-click menu, we find that the menu cannot be opened and we cannot use the computer normally. At this time, we need to restore the system to solve the problem. Win10 right-click menu management cannot be opened: 1. First open our control panel, and then click. 2. Then click under Security and Maintenance. 3. Click on the right to restore the system. 4. If it still cannot be used, check whether there is something wrong with the mouse itself. 5. If you are sure there is no problem with the mouse, press + and enter. 6. After the execution is completed, restart the computer.

Solve the problem that Discuz WeChat sharing cannot be displayed Solve the problem that Discuz WeChat sharing cannot be displayed Mar 09, 2024 pm 03:39 PM

Title: To solve the problem that Discuz WeChat shares cannot be displayed, specific code examples are needed. With the development of the mobile Internet, WeChat has become an indispensable part of people's daily lives. In website development, in order to improve user experience and expand website exposure, many websites will integrate WeChat sharing functions, allowing users to easily share website content to Moments or WeChat groups. However, sometimes when using open source forum systems such as Discuz, you will encounter the problem that WeChat shares cannot be displayed, which brings certain difficulties to the user experience.

See all articles