Location>code7788 >text

NodeJs-Package Management Tool

Popularity:111 ℃/2024-12-18 00:31:51

The English word for package is package, which stands for a collection of source code for a particular function.

Package management application that allows you to download, install, update, delete and upload packages.

Rapid development of projects with the help of package management tools to improve development efficiency

Commonly used front-end package management tools are npm, yarn, cnpm

npm

npm is automatically installed during installation.

Basic npm usage
  1. initialization

    The npm init command is used to initialize a folder as a package, interactively create files

    is the configuration file for the package, and every package must have the

    npm init
    

    After that, the command line is entered interactively at the prompt, and after it's all run, it creates the

    {
      "name": "test", // package name
      "version": "1.0.0", // package version
      "description": "test", // package description
      "main": "", // Package entry file
      "scripts": { // script configuration
        "test": "echo \"Error: no test specified\" && exit 1"
      }, // script configurations
      "author": "", // author
      "license": "ISC" // Open Source Certificate
    }
    

    You can use npm init -y or npm init --yes to create packages very quickly. The package name should not be in Chinese or uppercase, and the default value is the folder name if you don't write it.

  2. Search Toolkit

    # command line
    npm search keyword
    

    You can also go tonpm package URLlook for sth.

  3. Download the installation package

    npm install package name
    npm i package name
    # After installation, the installed package is a dependency of the current package.
    

    After running it, two resources will be added to the folder

    • node_modules folder Stores downloaded packages.
    • The package lock file, which is used to lock the version of the package.
  4. The basic process of importing an npm module with require

    const fs = require('fs')
    
    • Look for the corresponding folder with the same name in the current folder node_modules
    • If it is not found, look for a folder with the same name in node_modules in the parent directory until you find the disk root.
Development dependency vs. production dependency
  1. To produce dependencies, package information is stored in the dependencies property of the

    npm --save package name
    npm i -S package name # abbreviation
    
  2. Development dependencies, package information is stored in the devDependencies property of the

    npm i --save dev package name
    npm -D package name #short
    
  3. Development dependencies are packages that are only used during the development phase, while production dependencies are packages that are used during both the development phase and the final go-live phase.

    dependency package

npm global install

The global install command is independent of the location of the working directory, and you can view the location of the globally installed packages with npm root -g

npm i package name # local install, package can only be used in the corresponding working directory
npm i -g package name # global installation
# nodemon serves to automatically restart Node applications
npm i -g nodemon


nodemon # runs js files instead of node, so you don't need to restart the service manually when the code changes.
Install all dependencies

This command installs project dependencies based on the dependency declarations for and

npm install
npm i # abbreviation
Install the specified version of the package
npm i package name @ version number
Delete Package
npm remove package name
npm r package name #short

npm r -g package name #global remove
Configuring Command Aliases

Commands can be executed more easily by configuring command aliases, and the scripts attribute in the

  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1", "server": "node .
    "server": "node . /" // Configure the command alias

  },
# Execute this command, it will go to scripts to find the corresponding Server, and then execute the Server's code node run code
npm run server

If you configure the start attribute in scripts, you don't need to add run when running, just npm start

npm start is a common command used to start a project.

npm run has a feature that automatically looks up in the parent directory, just like the require function.

cnpm

cnpm is a full mirror of the Taobao build, also known as Taobao Mirror, at /etc/cnpm.com

The cnpm service is deployed on the domestic AliCloud servers, which can increase the download speed of packages.

There is also a global toolkit, cnpm, that does much the same thing as npm.

Installing cnpm
# Install cnpm
npm install -g cnpm --registry=

cnpm initialization, add, delete, change, and other operations commanded in the same way as npm

npm configure taobao mirrors
  1. Direct Configuration

    npm config set registry /
    
  2. Tool configuration, use nrm to configure npm's mirror address

    # Install nrm
    npm i -g nrm
    
    # Modify the image
    nrm use taobao
    
    # Check if the configuration was successful
    npm config list
    

yarn

yarn is a new Javascript package management tool introduced by Facebook in 2016.official website

Features.

  • Speed: yarn caches each downloaded package, so there's no need to re-download it when you use it again. It also utilizes parallel downloads to maximize resource utilization, resulting in faster installations!

  • Security: yarn algorithmically verifies the integrity of each installed package before executing the code.

  • Reliable: Using a detailed, concise lock file format and a well-defined installation algorithm, yarn is guaranteed to work flawlessly on different systems.

  1. Installing yarn

    npm i -g yarn
    
  2. initialization

yar init -y # quick initialization
yar init -y # Fast initialization
  1. Installation of dependencies

    yarn add package name # Production dependencies
    yarn add package name --dev # Development dependencies
    yarn global add package name # Global installation
    
  2. Remove dependencies

    yarn remove package name # local remove
    yarn global remove package name # Remove globally
    
  3. Install project dependencies

    yarn
    
  4. The configuration is the same, run with the following command

    yarn Alias
    
  5. Configuring Taobao mirrors with yarn

    # This can be done by yarn config list ferret out yarn configuration item
    yarn config set registry /
    
  6. The lock file for npm is ,the lock file for yarn is

Managing Release Packages

You can publish your own toolkits to the npm service for your own use and the use of other developers.

  1. Create the folder and create the file , declare the function in the file, and expose it using the
  2. npm initializes the package, filling in the package information (the package name is unique)
  3. Register for an account/signup
  4. Activate account
  5. Modify to the official official image (run nrm use npm on the command line)
  6. command line npm login Fill in the relevant user information
  7. Command-line npm publish commits packages
  8. Modify the package: Modify the version number in the package, and then use npm publish to release the update.
  9. Packages can be removed using npm unpublish --force