1. What is a bag?
The third-party module in Node.js is also called a package. It is developed by a third-party individual or team and is free for everyone to use
2. Package classification
- Project package
– development dependency package (stored in devdependences directory)
– production dependency package (stored in the dependencies directory) - Global package
3. Installation package
npm i package name
npm i package name - g / / install global package
//View the installation location of the global package
npm root -g
Use from the command line ==>Global installation adopt require()Introduction and use ==> Project package You can also refer to the official instructions
4. Publish your own package
First, you need to know the specification of the package
The package consists of three parts:
1. The package must exist in a separate directory
2. The top-level directory must contain the package.json management configuration file
3.package.json must contain three attributes: name version main, which record the package name, version number and package entry respectively
Registered npm account
① Visit https://www.npmjs.com/ Website, click the sign up button to enter the registered user page
② Fill in the information related to the account: full Name, Public Email, Username and Password
③ Click the Create an Account button to register an account
④ Log in to the email and click the verification link to verify the account
If the mailbox is not verified, an error will be reported when executing the following procedures
Initialize package infrastructure
Create a new myselfpackage folder as the root directory
Create three new files in the myselfpackage folder
Open the terminal and enter the npm init -y command to automatically create a package.json file
-
package.json (package management configuration file)
-
index.js (package entry file)
-
README.md (description document of package)
Initialize package.json configuration file '
{
"name": "myselfpackage",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo "Error: no test specified" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
Write the method of the function to be implemented in index.js
For example: format date
// Package entry file index.js // A function that defines the formatting time function dateFormat (dateStr) { const dt = new Date(dateStr) const y = padZero(dt.getFullYear()) const m = padZero(dt.getMonth() + 1) const d = padZero(dt.getDate()) const hh = padZero(dt.getHours()) const mm = padZero(dt.getMinutes()) const ss = padZero(dt.getSeconds()) return `${y}-${m}-${d} ${hh}:${mm}:${ss}` } // Define a function that complements zero function padZero (n) { return n > 9 ? n : '0' + n } // Expose required members module.exports = { dateFormat }
//Test code const itheima = require('./flightloong-tools/index') // Code for formatting time const dtStr = itheima.dateFormat(new Date()) console.log(dtStr) // 2020-06-23 01:16:57
Define the method of escaping HTML in index.js
// index.js // Functions that define escape HTML characters function htmlEscape(htmlstr) { return htmlstr.replace(/<|>|"|&/g, match => { switch (match) { case '<': return '<' case '>': return '>' case '"': return '"' case '&': return '&' } }) }
Divide different modules
-
Create an src folder under the Ayao tools folder
-
Split the time formatting function into SRC - > dateformat.js
-
Split the function of processing HTML strings into SRC - > htmlescape.js
-
In index.js, import two modules to get the methods that need to be shared externally
-
In index.js, use module.exports to share the corresponding methods
Write documentation
1. The README.md file in the root directory of the package is the instruction document of the package. Through it, we can write the instructions of the package in markdown format in advance, which is convenient for users to refer to
2. There are no mandatory requirements for what is written in the readme file; As long as the function, usage and precautions of the package can be clearly described
### install ``` npm i flightloong-tools ``` ### Import ```js const itheima = require('./flightloong-tools') ``` ### Format time ```js // Call dateFormat to format the time const dtStr = itheima.dateFormat(new Date()) // Result 2020-04-03 17:20:58 console.log(dtStr) ``` ### Escape special characters in HTML ```js // HTML string with conversion const htmlStr = '<h1 title="abc">This is h1 label<span>123 </span></h1>' // Call the htmlEscape method to convert const str = itheima.htmlEscape(htmlStr) // Result of conversion & lt; h1 title=" abc"& gt; This is the H1 tag & lt; span> 123& nbsp;& lt;/ span>& lt;/ h1> console.log(str) ``` ### Restore special characters in HTML ```js // HTML string to restore const str2 = itheima.htmlUnEscape(str) // The output result < H1 title = "ABC" > this is the H1 tag < span > 123 & nbsp</ span></h1> console.log(str2) ``` ### Open source protocol ISC -------- Copyright notice: This article is CSDN Blogger「It's ah Yao~」Original articles, follow CC 4.0 BY-SA Copyright agreement, please attach the original source link and this statement. Original link: https://blog.csdn.net/qq_61006976/article/details/120632621
Switch mirror to npm mirror
nrm ls
nrm use npm
Log in npm account at the terminal
npm login / / enter your account, password and email address
Upload package
npm publish
5. Delete package
Run the npm unpublish package name -- force command to delete the published package from npm
matters needing attention
-
The npm unpublish command can only delete packages published within 72 hours
-
npm unpublish deleted packages are not allowed to be published repeatedly within 24 hours
-
Be careful when releasing packages. Try not to release meaningless packages to npm!