当前位置:网站首页>NPM package [details] (including NPM package development, release, installation, update, search, uninstall, view, version number update rules, package.json details, etc.)

NPM package [details] (including NPM package development, release, installation, update, search, uninstall, view, version number update rules, package.json details, etc.)

2022-06-24 13:53:00 Chaoyang 39

npm package

npm Package is compliant npm Official website canonical js modular , It is usually published to npm In the official website , For everyone to download .

npm Package details

npm Package details are stored in npm Bag package.json In file , It usually contains the following information :

  • name - Package name .
  • version - Version number of the package .
  • description - Package description .
  • homepage - Bao's official website url .
  • author - The name of the author of the bag .
  • contributors - Name of other contributors to the package .
  • dependencies - Dependency package list . If the dependency package is not installed ,npm Will automatically install the dependency package in node_module Under the table of contents .
  • repository - The type of place where the package code is stored , It can be git or svn,git Can be found in Github On .
  • main - main Field specifies the main entry file of the program ,require(‘moduleName’) It will load the file . The default value of this field is... Under the module root directory index.js.
  • keywords - keyword

npm Version number of the package

npm The package version number is divided into X.Y.Z Three place , They represent the major version number 、 Minor version number and patch version number .
When the code changes , The version number is updated according to the following principles :

  • Just fix bug, You need to update Z position .
  • New features , But downward compatible , You need to update Y position .
  • Major changes , Down incompatibility , You need to update X position .

View the version number of the specified package

grunt For the package name

npm list grunt

Installation package npm install

express Is the name of the dependent package ,npm install It can be abbreviated as npm i

Local installation

Download the installation package to ./node_modules in ( function npm The directory where the command is located ), If there is no node_modules Directory will be created

npm install express

When using , There is no need to specify a third-party package path .

var express = require('express');

Global installation

Will download the package to node.js In the installation directory of node_modules Directory , All projects can use globally installed packages

npm install express -g

Update package npm update

express Is the name of the dependent package

npm update express

Search package npm search

express Is the name of the dependent package

npm search express

Uninstall package npm uninstall

express Is the name of the dependent package

npm uninstall express

View all packages in the current directory

npm ls

View all packages installed globally

npm list -g

Development npm package

  1. New folder mypack-test
  2. use vscode Open folder mypack-test
  3. Terminal execution npm init Generate package.json file , Enter all the way , Last input y You can enter. .
     Insert picture description here
    The part in the blue box is the package information , You can input by yourself as required ( Or wait to generate package.json We'll change it later )
  • package name Package name , The default is project name
  • version Version number of the package , The default is 1.0.0
  • description Package description information
  • entry point Package entry file ( Everything in the package , To export this file ), The default is index.js
  • test command The test command , The default is “echo “Error: no test specified” && exit 1”
  • git repository Remote of package git Warehouse
  • keywords keyword
  • author The author of the package
  • license Open source file protocol , The default is ISC
  1. New file index.js, The content is
    The main points of : Defined function / Variables must be exported !
/*  The functionality —— Sum up   Parameters —— Two figures  */
function sum(a, b) {
    
  return a + b;
}

module.exports = {
    
  sum: sum,
};

thus , Completed npm Package development , If you want to open download , Please continue to publish npm package

Release npm package

  1. register npm account number
npm adduser

Or go directly to the official website to register https://www.npmjs.com/

  1. Sign in npm account number
npm login

Enter... As prompted npm Account number 、 password 、 mailbox 、 One time verification code received in the email
 Insert picture description here
3. Release

npm publish

In case of the following errors

npm ERR! code E403
npm ERR! 403 403 Forbidden - PUT https://registry.npmjs.org/mypack-test - You do not have permission to publish "mypack-test". Are you logged in as the correct user?

shows npm The current package already exists on the official website .

Solution
take package.json The package name in name Change to a npm Package name that does not exist on the official website , Just publish it again .

For example, the final name of my package is mypack-test-999
 Insert picture description here

After publishing successfully , You can find it on the official website
https://www.npmjs.com/package/mypack-test-999
 Insert picture description here

Try the original npm package

  1. New folder test_mypack
  2. use vscode Open folder test_mypack
  3. Installation package
npm i mypack-test-999

 Insert picture description here
4. New file index.js, The content is

import {
     sum } from "mypack-test-999";

let result = sum(6, 2);
console.log(result);

  1. perform index.js
     Insert picture description here
    An error is reported in the figure , You need to in package.json Add
  "type": "module",

Re execution index.js, Get expected results , Verify success !
 Insert picture description here

原网站

版权声明
本文为[Chaoyang 39]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241059270665.html