关于npm包发布流程

158 阅读2分钟

1、准备待发布的包

进入要发布的包的根目录,如果不存在package.json文件,则执行 npm init命令,根据提示填写包名(package name)、版本号(version)、描述(description)、github地址(git respository)、关键字(key words)、license等;输入的这些信息可以在该文件中修改。如果存在该文件,只需要填写号对应信息即可。

{
  "name": "muzi-test",
  "version": "1.0.0",
  "description": "The test release",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": ["muzi"],
  "author": "muzi",
  "license": "ISC"
}

在根目录创建LICENSE文件,写入如下内容:

MIT License

Copyright (c) 2022 muzi lixia

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
  • Copyright © 2022 muzi lixia 这一行,2020是年份,自行修改为当前年份,muzi lixia是作者姓名,改成你自己的。
  • package.json里修改license字段值为MIT

2、注册npm账号

注册npm账号有两种方式:

方法一:npm官网注册,地址

方法二:通过命令注册。

Windows系统,打开 cmd 命令窗口,输入 npm adduser,根据提示输入用户名、密码、邮箱即可。如果提示Unable to authenticate, need:Basic,则表示当前用户名已被注册,换一个再试。

3、登录npm账号

在要发布的包的根目录,打开cmd命令窗口,首先查看本地npm地址是否是官方地址。

// 查看npm地址
npm config get registry

如果不是,则需要更换为官方地址。注:一定要是https开头,否则可能导致登录不成功.

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

切换为官方地址,发布之后在切换回来即可。

// 淘宝源
npm config set registry http://registry.npm.taobao.org

输入npm login,需要根据提示输入用户名、密码、邮箱,邮箱会收到一封临时密码邮件,在命令中输入临时密码,登录成功会提示:Logged in as 用户名 on https://registry.npmjs.org/.

4、发布

在cmd窗口中输入npm publish即可发布。之后便可以在npm上搜索查看刚刚发布的包。

5、更新

// 这个操作会在原先的版本号上+1,如1.0.0 会变成1.0.1
npm version patch

// 重新发布
npm publish

6、删除

// 删除指定的包和版本号
npm unpublish 包名@版本号