Package management tool npm- node package management related knowledge, check package update, NPM package upload, mirror replacement, npm ERR! registry error parsing json

node package management

NPM-related knowledge

Introduction

  1. NPMis the built-in package manager of nodejs - used to manage packages
  2. Short book recommendation

NPM consists of three separate parts:

  • Website: The main way for developers to find packages, set parameters, and manage npm experience. The URL is: https://www.npmjs.com/
  • Registry: It is a huge database that saves the basic information of each package.
  • Command-line tools: tools for developers to work with npm packages.

common command

  • View version:node -v npm -v
  • View setup help:npm help config
  • Set proxy:
    • npm config set proxy 192.168.80.202:808
    • npm config set https-proxy 192.168.80.202:808
  • Proxy username and password
    • npm config set proxy http://username:password@server:port
    • npm confit set https-proxy http://username:password@server:port
  • Cancel proxy:
    • npm config delete proxy
    • npm config delete https-proxy
  • View installation information and download path:npm config ls
  • Change the default cache path of npm:npm config set cache "D:/TempDate/nodedata/cache"
  • Change the default download path of npmnpm config set prefix "D:/TempDate/nodedata/download"
  • Set the registry:
    • Original configuration:npm config set registry http://registry.npmjs.org
    • Taobao Mirror:npm config set registry http://registry.npm.taobao.org
  • Installation package:npm install
  • exit status:Ctrl+C 输入Y




npm check for package updates

Npm-check is used to check whether the npm dependency package is updated, wrong and not in use, we can also use npm-check to update the package

Check for package updates

Install npm-check:

npm install -g npm-check

Check the status of the npm package:

npm-check -u -g

package update

  1. update global package
npm update <name> -g
  1. Update production environment dependencies
npm update <name> --save
  1. Update development environment dependencies
npm update <name> --save-dev




NPM package upload

Original article: Package and upload your own project to npm

Magically modify other people's packages: modify giant's npm package & how to upload and update your own package with npm

Register an account

For account registration, please move to: Open the link

initialization package

Initialize a package.json file with npm init

npm init

The initialization is package.jsonas follows:

{
  "name": "bigmushroom_first_test",
  "version": "1.0.0",
  "description": "My first package to test upload npm package",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "liuwanqiang",
  "license": "ISC"
}

login npm

Use the command line locally to connect to npm Use the following command, and then follow the prompts, the password will not be displayed by default

npm login
  • Enter your user name
  • enter password
  • Enter email

Replace mirror

Because most people npm use the Taobao mirror, the upload will fail, so it needs to be replaced with the original mirror https://registry.npmjs.org/

npm config set registry https://registry.npmjs.org/

Check again if the setting is successful

npm config get registry

Expand knowledge points:

// 查看当前配置
npm config list

// 安装镜像
npm config set registry https://registry.npm.taobao.org/
npm config set disturl https://npm.taobao.org/dist

// 删除镜像
npm config delete registry
npm config delete disturl

log in again

After changing the image, you need to follow the steps mentioned above and log in to your npm account locally again.

npm login

release package

npm publish

Precautions

You must use exports to expose your method, define an npmTest method as follows and expose it, and then use require to import after installing this package

exports.npmTest = (name) => {
    console.log(name)
}

FAQ:

Q1: npm ERR! registry error parsing json

If it is found during the upload process, npm ERR! registry error parsing json will report an error

To solve the error reporting method, delete the registry and continue to install the module

npm config delete registry

undo upload

npm unpublish --force

Q2: Update the uploaded npm package

  • The first is that you can manually modify the version number in package.json. The modified version number must be larger than the previous one, and then use npm publish to upload
  • The second is to use the npm command line tool to update the version number, and then use npm publish to upload,
  • The version number consists of three digits abc, switch to the directory where package.json is located
  • Update c: npm version patch
  • Update point b: npm version minor
  • Update a: npm version major

Every time you use the above command, add one to the place corresponding to the version number

Guess you like

Origin blog.csdn.net/qq_33704787/article/details/126089731