preface
Our project installs and uses various NPM packages all the time, and there are children who want to have their own NPM packages and make them available to others. What should we do at this point? Next, this article will take you step-by-step through creating and publishing your own NPM package to NPM.
The preparatory work
- First we need to register an NPM account
Register portal
-
Install NRM globally to manage THE NPM repository — (not necessary, just for easy switching)
npm i nrm -g Copy the code
-
NRM is the software of NPM warehouse management, which can be used for fast switching of NPM warehouse
NRM add <registry> <url> // Add new repository NRM use <registry> // Switch the warehouseCopy the code
-
Publish a package
- Create project folder
mkdir magic-demo-npm && cd magic-demo-npm
Copy the code
- Create package. Json
npm init
Copy the code
-
Follow the instructions step by step, or use NPM init -y to use the NPM default Settings and modify them later by editing package.json. Note: The package entry file for this demo is index.js. Make sure that the value of the main field in package.json is “index.js”.
-
Create the results
{ "name": "magic-demo-npm"."version": "1.0.0"."description": "my first npm"."main": "index.js"."scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [ "npm"]."author": "magic_yuanwei"."license": "ISC" } Copy the code
- To create a README. Md
### magic-demo-npm
This is my first npm package!
It is just for learning.
Copy the code
- Create index. Js
module.exports = {
printMsg: function () {
console.log('this message is from magic-demo-npm!');
}
}
Copy the code
- Final directory structure
├─ ├─ ├─ ├─ ├─ download.txtCopy the code
- Publish a package
npm publish
Copy the code
-
Note: Errors may be encountered
- We’re not logged in
-
———– Solution: NPM adduser
-
———– Enter username, password, and email address in sequence
2. The mirror source is incorrect
- Use NRM to switch to the NPM repository and execute the command
nrm use npm
After the preceding problems are rectified, run the NPM publish command again. The publish succeeds.
Use as normal package use, NPM install will do.
The iteration
Each iteration of the NPM package involves two aspects:
- Change of content
- Version changes
- Content change
module.exports = { printMsg: function () { console.log('this message is from magic-demo-npm! '); console.log('update') } }Copy the code
- Version changes
NPM uses semantic versions, which are separated by periods (.) from left to right: Major, Minor, and patch.
For example, 1.0.0 major.minor.patch
Specification for Version Changes:
State of the code | level | The rules | Version of the sample |
---|---|---|---|
First release | New project release | Begin with 1.0.0 | 1.0.0 |
Bug fixes, backward compatibility | Patch Release | Change the third digit | 1.0.1 |
New functionality, backward compatibility | Second Release | Change the second digit and reset the third digit to 0 | 1.1.0 |
Major changes, not backward compatible | Major Release | Change the first digit and reset the second and third digits to 0 | 2.0.0 |
Change the version number of the command: NPM version < major | minor | patch >
If we are issuing a secondary issue, we execute the command:
npm version minor
Copy the code
So the version will become
{" name ":" magic - demo - NPM ", "version" : "1.1.0", "description" : "my first NPM", "main" : "index. Js", "scripts" : {" test ": "echo \"Error: no test specified\" && exit 1" }, "keywords": [ "npm" ], "author": "magic_yuanwei", "license": "ISC" }Copy the code
release
npm publish
Copy the code
Install updates
npm up magic-demo-npm
Copy the code
Discard/Delete
After an NPM package is published, it can be discarded or deleted. The difference between the discarded and deleted packages is as follows:
- Deprecation does not remove packages or versions from the NPM repository, but downloads and installations can continue, with a warning at installation time
- Deleting will completely remove the package from NPM and will not be downloaded and installed
Whether discarded or deleted, there are two levels:
- Version deprecation/deletion
- Package deprecation/deletion
-
abandoned
Syntax: NPM deprecate < PKG >[@
]
npm deprecate magic-demo-npm 'test deprecate'
Copy the code
-
delete
NPM does not encourage any kind of deletion, mainly because the package we distribute may have been referenced by others, and if we delete this package, others may have problems finding the package when reinstalling the project that contains our package’s dependencies.
Based on this, NPM makes relevant deletion restrictions:
Deleted version can be reissued 24 hours later!
Only packages published within 72 hours can be deleted!
npm unpublish magic-demo-npm --force
Copy the code