Hand in hand teach you to send a bag
Package? School bag? Lv bag, neither of them. When it comes to the package, it will play a great role in the development of programmers and is an indispensable part of our development. Today, I will teach you how to make a package hand in hand
1. What is a bag
Concept: In Node.js, a package is a third-party module called a package;
1.1 There are three modules in Node.js
-
Built-in module
-
Custom modules
-
Third-party modules
The packages we use during development are third-party modules
2. Why do you need a bag
1. Node.js built-in modules only provide some low-level APIS, resulting in relatively few functions in the development process, affecting the development efficiency
2. Package is based on the built-in module encapsulation, provides a more advanced, more convenient API, greatly improve the development efficiency, let you develop the project force directly improved
3. The relationship between the package and the built-in module, and the relationship between jQuery and the browser built-in API
3. The source of the bag
Packages are created by third party individuals or teams, and are different from node.js built-in modules and custom modules
Important: Free download at!!!!
2.2 How do I Download a Package
1. When you download Node.js, a management tool called Node Package Manager(NPM) is installed on your computer with Node.js
2. Run the NPM install command
NPM official website: www.npmjs.com/
NPM I is a shorthand that does not distinguish the full name of the NPM I packageCopy the code
3. Specify the package version
NPM I package name @2.22.2Copy the code
5. Develop and upload your own package
5.1 Package structure of specification
5.1. A standard package, with its constituent structure, must meet the following three requirements:
- Packages must exist in a separate directory
- The package must be included in the top-level directory
package.json
This package manages configuration files package.json
Must containname
.version
.main
These three attributes represent the package name, version number, and package entry, respectively
5.2 Initializing the Package Structure
1. Create the itheiMA-tools folder as the root directory of the package
2. In the itheiMA-tools folder, create the following three files:
package.json
(Package management configuration file)index.js
(Package entry file)README.md
(Package documentation)
5.3 Initializing the package.json configuration file
5.4 the initializationpackage.json
The configuration file
{"name": "flightloong-tools", "version": "1.0.0", "description": "provide formatting time, HTMLEscape related functions ", "main": "index.js", "keywords": [ "itcast", "itheima", "dateFormat", "escape" ], "license": "ISC" }Copy the code
Note: 1. Name is unique. Please check the name on the official website before creating it
2. Version indicates the version number. After the update, you can add and upload the version number
3. Main is the exposed code pointed to
4. Check whether the license complies with ISO
5.5 inindex.js
Defines a method for formatting time in
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} '} function padZero (n) {return n > 9? N: '0' + n} // exports = {dateFormat}Copy the code
// Test code const lvpackage = require('./flightloong-tools/index') const dtStr = lvpackage.dateformat (new Date()) console.log(dtStr) // 2020-06-23 01:16:57Copy the code
5.6 Divide different Modules
-
Split time formatting into SRC -> dateformat.js
-
In index.js, import two modules to get methods that need to be shared outwards
-
In index.js, the corresponding methods are shared out using module.exports
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} '} function padZero(n) {return n > 9? n : '0' + n } module.exports = { dateFormat }Copy the code
5.7 Preparing Package Description Documents
-
The readme.md file in the package root directory is the package instruction document. Through it, we can write the package instructions in markdown format in advance for users’ reference
-
There are no mandatory requirements for what to write in a README file. As long as the functions, usage, and precautions of the package are clearly described, it is ok
-
The readme.md document for the package we created will contain the following six items
- installation
- Import the way
- Formatting time
- Open source licenses
### install NPM I flightloong-tools ### import js const lvpackage = require('./flightloong-tools') ### format dateFormat Const dtStr = lvpackage.dateformat (new Date()) // result 2021-10-07 11:20:58 console.log(dtStr) ### open source protocol ISCCopy the code
5.8 registerednpm
account
- accessnpmWebsite, click
sign up
Button to enter the registration user interface - Fill in the information related to the account:
Full Name
,Public Email
,Username
,Password
- Click on the
Create an Account
Button to register an account - Log in to the mailbox and click the verification link to verify your account
5.9 the loginnpm
account
npm
After the account is registered, you can perform the operation on the TERMINALnpm login
Enter the user name, password, and email address in sequence to log in successfully- Note: running
npm login
Before running the command, you must change the server address of the next packet tonpm
The official server of. Otherwise, publishing the package will fail!
5.10 Publishing the package tonpm
on
After switching the terminal to the root directory of the package, run the NPM publish command to publish the package to NPM (note: the package name cannot be the same)
5.14 – Delete published packages
-
To delete published packages from NPM, run the NPM unpublish package name –force command
-
Matters needing attention
npm unpublish
The commandOnly the packages published within 72 hours can be deletednpm unpublish
Deleted packages,Re-posting is not allowed within 24 hours- Be careful when releasing packages and try not to release meaningless packages to NPM!