npm如何发布和更新软件包

参考npm官网doc进行整理,主要步骤和关键点进行了中文翻译,其他地方保留官方英文说明<br> How to Publish & Update a Package

遇到的问题

1. 注册地址不对,无法发布

如果发布包之前将代理注册到到淘宝的镜像http://registry.npm.taobao.org/

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

必须切换到npm registry http://registry.npmjs.org/

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

因为我们的包是发布到npm registry,而淘宝镜像地址只是10分钟同步一次的只读地址

2. 修改了邮箱地址后,报403 PUT错误

如果修改邮箱地址则需要到邮箱进行验证

3. 其他问题

因为网络慢,会偶发很多问题


如何发布和更新软件包

你能发布任何具有package.json文件的目录。本章主要讲了第一次怎样发布一个软件包,以及以后怎么更新这个软件包。

准备

了解npm政策

在开始之前,去了解一下npm的政策是个好主意,以防出现关于网站礼节,命名,认证或者其他使用引导方面的问题。

创建一个账户

要发布软件到npm registry,你必须是其用户。如果你不是其用户,可以通过 npm adduser 创建一个用户。如果你在网站上创建了账户,可以从terminal终端用 npm login 访问你的账户。

Test:

  1. 从Terminal终端输入 npm whoami 查看是否登录(技术上,这也就是说你的认证信息已经存储在本地了)
  2. 检查你的用户名是否已经被添加到注册中心(registry) https://npmjs.com/~username

For example https://www.npmjs.com/~qunxing

==欢迎加入peony组织,一起交流学习,有意向加入QQ群(518219739)并发npm账号,稍后邀请加入peony==

检查包目录

检查内容

Note that everything in the directory will be included unless it is ignored by a local .gitignore or .npmignore file. To learn how to use these commands, see npm-developers

选择一个名字

Choose a unique name for your package. Try to choose a descriptive name that:

  • isn't already owned by somebody else
  • is not spelled like another name, except with typos
  • will not confuse others about the authorship
  • meets npm policy guidelines. For example, do not name your package something offensive, and don't use someone else's trademarked name.
  • Specify the name in the appropriate line of the package.json file.

Note: The first 3 caveats don't apply if you are using scopes

提供文档(<span>readme.md<span>)

npm recommends that you include a readme file to document your package. The readme file must have the filename readme.md. The file extension .md indicates that the file is a markdown file. This file will appear on the npm website when someone finds your package.

Before you begin, please look at some of the package pages to get ideas for the information you can add to your readme file, and to see why this is so important.

  1. Create a file using any text editor.

  2. Save it in the project directory with the name readme.md

  3. When you publish, this documenation will display on the web page where people download your package.

发布(Publish)

npm publish 发布包

测试(Test)

访问 https://npmjs.com/package/<package> 你会看到你发布的所有新包的页面。 看起来有点像这样 https://docs.npmjs.com/images/lodash-package-page.png

恭喜


如何更新一个包

如何更新版本号

当你做出改变时,你能用 npm version <update_type> 更新包。 这里 <update_type> 是 语义化版本(semantic versioning)的发布类型 ,补丁(patch),次版本(minor),主版本(major). 这个命令会改变package.json文件中的版本号。

Note:如果你已经关联了一个git respository 到你的npm账号上,那么这也会增加一个与被修改版本的版本号保持一致的tag到你的仓库上。

当修改了版本号(version number)后,再次运行 npm publish

重新检查,版本号应该被更新了

如何更新Reademe文件

显示在网站上的README文件不能被更新了,除非你的包新版本被发布。所以你需要去运行 ** npm version path , npm publish**去更新显示在网站上的文档。

Learn More

  • To find out more about node modules and packages, see here.

  • To learn about semantic versioning, click here.

  • To learn more about tags, click here.

  • To learn more about package.json files, click here.

  • To learn more about naming, including how npm protects you against typosquat confusion, click here

转载于:https://my.oschina.net/starforever/blog/1799109

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值