npm creation and usage instructions

1. Create and publish npm package

1. Create npm account on
npm official website npm website address: https://www.npmjs.com/
npm website registration address: https://www.npmjs.com/signup

2. Log in to npm using command line tools

$ npm  login
C:\Users\Administrator>npm  login
Username: wang******928
Password:
Email: (this IS public) wang******[email protected]
Logged in as wang******928 on https://registry.npmjs.org/.
验证登录是否成功
C:\Users\Administrator>npm who am i
wang******928

2. To create a project, you need to create a folder in advance under the specified folder

$ npm init
E:\ownProject\npm_object>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.
package name: (npm_object) average_mood//项目名称
version: (1.0.0) //项目版本
 // 以下内容可以一直回车
description: Project description //项目描述
entry point: (index.js) //启动文件入口
test command: //测试命令
git repository: //git存储库储存地址
keywords:  //关键词
author:  //作者
license: (ISC) //许可证
// 项目包配置文件
About to write to E:\ownProject\npm_object\package.json:

{
    
    
  "name": "average_mood",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    
    
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}


Is this OK? (yes)

E:\ownProject\npm_object> // 创建成功

3. Create a new index.js file in the same level directory and write content

Insert picture description here
At this point, we have created a simple npm package. Next, let’s publish the npm package.

5. Publish npm package

$ npm publish

If the publishing is successful, it will prompt:

E:\ownProject\npm_object>npm publish
npm notice
npm notice package: average_mood@1.0.0
npm notice === Tarball Contents ===
npm notice 81B  index.js
npm notice 208B package.json
npm notice === Tarball Details ===
npm notice name:          average_mood
npm notice version:       1.0.0
npm notice package size:  331 B
npm notice unpacked size: 289 B
npm notice shasum:        dd03c0a5740f835719616829ce8c4f70dd77d0b4
npm notice integrity:     sha512-4CwyE6UoJkUvQ[...]BKAygd/NeFUdw==
npm notice total files:   2
npm notice
+ average_mood@1.0.0  //发布成功

6. Verify that the release is successful.
Change to a directory and download the npm package we just released

>npm install average_mood
E:\ownProject\npm_test>npm install average_mood
npm WARN saveError ENOENT: no such file or directory, open 'E:\ownProject\npm_test\package.json'
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN enoent ENOENT: no such file or directory, open 'E:\ownProject\npm_test\package.json'
npm WARN npm_test No description
npm WARN npm_test No repository field.
npm WARN npm_test No README data
npm WARN npm_test No license field.

+ [email protected]
added 1 package and audited 1 package in 2.415s
found 0 vulnerabilities


E:\ownProject\npm_test>dir     //进入目录后查看目录中的列表
 驱动器 E 中的卷是 新加卷
 卷的序列号是 A06D-CB03

 E:\ownProject\npm_test 的目录

2021/03/01  11:49    <DIR>          .
2021/03/01  11:49    <DIR>          ..
2021/03/01  11:49    <DIR>          node_modules
2021/03/01  11:49               329 package-lock.json
               1 个文件            329 字节
               3 个目录 524,167,602,176 可用字节

The description has been published successfully.
7. Version update

npm version <update_type> -m "<message>"
//           更新类型		  更新版本说明
其中update_type 有三种:
patch增加一位补丁号(比如 1.1.1 -> 1.1.2)
minor增加一位小版本号(比如 1.1.1 -> 1.2.0)
major增加一位大版本号(比如 1.1.1 -> 2.0.0)
比如:
E:\ownProject\npm_object>npm version patch -m "从版本1.0.0到1.0.1"
v1.0.1
E:\ownProject\npm_object>npm publish  //最后提交更新的版本:
npm notice
npm notice package: [email protected]
npm notice === Tarball Contents ===
npm notice 81B  index.js
npm notice 208B package.json
npm notice === Tarball Details ===
npm notice name:          average_mood
npm notice version:       1.0.1
npm notice package size:  331 B
npm notice unpacked size: 289 B
npm notice shasum:        63676d2b2f3417cc84a9f144143e5b1b9d26e221
npm notice integrity:     sha512-CVmRvPwl0SW3b[...]Z89tw+fljohfg==
npm notice total files:   2
npm notice
+ [email protected] //发布成功

8. Abandon a certain version of the module

npm deprecate my-thing@"< 1.0.2" "critical bug fixed in v1.0.2"

9. Withdraw the version published by yourself. This is just a test package, it is best to undo it of course

//删除要用force强制删除。超过24小时就不能删除了。自己把握好时间。
npm --force unpublish average_mood
9.1删除版本成功
E:\ownProject\npm_test>npm install average_mood
npm ERR! code ENOVERSIONS
npm ERR! No valid versions available for average_mood

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Administrator\AppData\Roaming\npm-cache\_logs\2021-03-01T06_26_11_467Z-debug.log

10. Matters needing attention:

a. When using domestic mirrors when publishing, an error will be reported, we should use the default:

npm config set registry http://registry.npmjs.org 

b. The registry attribute in package.json of the npm package must be filled in, and the version in package.json must be greater than the previous one every time you publish npm.
c.npm publish failed put 500 unexpected status code 401 error message, it is often that the login is not successful, and
the name of the npm login d.npm package is unique. If there is the same name, an error will be reported when publishing.

Finally recommend a URL:
http://javascript.ruanyifeng.com/nodejs/npm.html#toc19

Guess you like

Origin blog.csdn.net/wangyanxin928/article/details/114257510