Abstract: What is NPM? How to release your own NPM package
Abstract: What is NPM?
NPM is a well-known package management tool for javascript, which is a landmark product of front-end modularization. Simply put, it is to download modules through NPM, reuse existing code, improve work efficiency and mobile end development, iOS using Cocoapods, Android using Maven has the same idea.
How do I publish my own NPM package
1. Create an NPM account
You must register an NPM account before distributing packages
2. Initialize a simple project release
A. Create a local folder, for example, z-tool. B. Run the $CD z-tool command to go to the directory. Enter all the way by default
sh-neverleave:z-tool neverleave$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See `npm help json` for definitive documentation on these fields
and exactly what they do.
Use `npm install <pkg>` afterwards to install a package and
save it as a dependency inThe package.json file. Press ^C at any time to quits. Package name: (z-tool) Version: (1.0.0) Description: entry point: (index.js)test command:
git repository:
keywords:
author:
license: (ISC)
About to write to /Users/neverleave/Desktop/z-tool/package.json:
{
"name": "z-tool"."version": "1.0.0"."description": ""."main": "index.js"."scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": ""."license": "ISC"
}
Is this ok? (yes)
Copy the code
Description of the default fields: name: indicates the name of the published package. The default value is the name of the upper-level folder. Must not be the same as the package name in the current NPM. Package names must not have uppercase letters/Spaces/slippage! Version: The version of your package. The default is 1.0.0. There are a series of rules for the version number of THE NPM package. The version number of the module adopts the format of X.Y.Z, which is embodied as follows: 1. Fix bugs, make small changes and add Z. Description: mian: entry file, default is index.js, can be changed to its own file scripts: contains various scripts to execute commandstest: Test command. License: Open Source file license, or MIT, if you need it.Copy the code
D, create a file named index.js in the z-tool folder and simply write the contents.
!function(){console.log(' This is the imported package entry ')}()Copy the code
3. If this machine releases the package for the first time (non-first time can be ignored)
Enter NPM adduser in the terminal, prompting you to enter your account, password and email address, and then prompt you to create successfully, as shown in the following figure. If NPM adduser succeeds, you are already logged in, so skip step 4.
Segmentfault.com/a/119000000…
4. Non-first release package
Enter NPM login on the terminal, then enter your account and password, and email address to login. The result is the same as step 3.
5. NPM publishes packets
Successful release:
Sh -neverleave:z-tool neverleave$NPM publish + [email protected]Copy the code
Note: If there is some private code in the project that you do not want to publish to NPM, you can write it to.gitignore or.npmignore and the upload will be ignored
6. Query published packages
Go to NPM official website global search
7. Installation and use
It is used in the same way as other packages. For details, see the source code introduction or readme.md.
8. How to undo a published package
NPM unpublish [email protected] Delete a version. 2. NPM unpublish z-tool –force delete the entire NPM market package
Using this command will not cancel existing packages in the community, but will warn anyone who tries to install the package. For example: NPM Deprecate Z-tool ‘This package I no longer maintain’
[note] If a permission error is reported, add –force
Publish error highlights
1. You need to increase the version number
NPM publish failed
sh-neverleave:z-tool neverleave$ npm publish
npm ERR! publish Failed PUT 400
npm ERR! code E400
npm ERR! deprecations must be strings : z-tool
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/neverleave/.npm/_logs/2018-11-23T10_52_01_742Z-debug.log
sh-neverleave:z-tool neverleave$ npm publish
#2, NPM publish failed
sh-neverleave:z-tool neverleave$ npm publish
npm ERR! publish Failed PUT 403
npm ERR! code E403
npm ERR! You cannot publish over the previously published versions: 1.0.3. : z-tool
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/neverleave/.npm/_logs/2018-11-23T11_24_57_662Z-debug.log
sh-neverleave:z-tool neverleave$
Copy the code
NPM publish –access public: NPM publish –access public
Reference: stackoverflow.com/questions/5…
NPM publish failed
sh-neverleave:npm neverleave$ npm publish
npm ERR! publish Failed PUT 400
npm ERR! code E400
npm ERR! unscoped packages cannot be private : z-tool
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/neverleave/.npm/_logs/2018-11-23T08_44_21_310Z-debug.log
sh-neverleave:npm neverleave$
NPM publish --access publicSh -neverleave: NPM neverleave$NPM publish --access public + [email protected] sh-neverleave: NPM neverleave$Copy the code
3. Ensure that the login user account is correct
sh-neverleave:npm neverleave$ npm publish
npm ERR! publish Failed PUT 404
npm ERR! code E404
npm ERR! 404 User not found : z-tool
npm ERR! 404
npm ERR! 404 'z-tool' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/neverleave/.npm/_logs/2018-11-23T07_32_28_518Z-debug.log
Copy the code
4. Add ‘~’ before username for login
sh-neverleave:npm neverleave$ npm login
Username: (~neverleave) neverleave
Password: (<default hidden>)
Email: (this IS public) ([email protected])
npm ERR! code EAUTHIP
npm ERR! Unable to authenticate, need: Basic
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/neverleave/.npm/_logs/2018-11-23T07_27_50_877Z-debug.log
sh-neverleave:npm neverleave$
Copy the code
5, do not have permission to delete the package online (there is a time limit, 24 hours) solution: add –force
sh-neverleave:z-tool neverleave$ npm unpublish z-tool
npm ERR! Refusing to delete entire project.
npm ERR! Run with --force to do this.
npm ERR! npm unpublish [<@scope>/]<pkg>[@<version>]
sh-neverleave:z-tool neverleave$
😄 I sure hope you know what you are doing.
sh-neverleave:z-tool neverleave$ npm unpublish z-tool --force
npm WARN using --force I sure hope you know what you are doing.
- z-tool
sh-neverleave:z-tool neverleave$
Copy the code
6, remove NPM market package with the same name 24 hours after re-release
sh-neverleave:z-tool neverleave$ npm publish
npm ERR! publish Failed PUT 403
npm ERR! code E403
npm ERR! z-tool cannot be republished until 24 hours have passed. : z-tool
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/neverleave/.npm/_logs/2018-11-23T11_41_24_086Z-debug.log
sh-neverleave:z-tool neverleave$
Copy the code
The end
Pay attention to wechat public accounts to improve comprehensive skills