npm publish 发布自己的npm包

官网的发布写的比较简单,直接就是npm pulish .但实际上直接运行这个命令很可能报错。
正确的发布顺序是:

1.初始化 package.json

npm init

2.验证你在 npmjs.org 上的账号

npm adduser

3.发布

npm publish .

上面三部是初次发布插件的步骤。当然,在发布前需要你先去npmjs.org上注册一个账号。
如果你以后修改了代码,然后想要同步到 npm 上的话请修改 package.json 中的 version 然后再次 publish

4.安装

npm install XXX --save

版本号规范

npm社区版本号规则采用的是semver(语义化版本),主要规则版本格式:主版本号.次版本号.修订号,版本号递增规则如下:

主版本号:当你做了不兼容的 API 修改,
次版本号:当你做了向下兼容的功能性新增,
修订号:当你做了向下兼容的问题修正。

先行版本号及版本编译信息可以加到“主版本号.次版本号.修订号”的后面,作为延伸。

问题

问题1

no_perms Private mode enable, only admin can publish this module

那么可能是你用了国内的镜像地址了,只需要重新把地址注册回npmjs即可。

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

返回淘宝镜像

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

问题2

npm ERR! you do not have permission to publish "your module name". Are you logged in as the correct user?

提示没有权限,其实就是你的module名在npm上已经被占用啦,这时候你就去需要去https://www.npmjs.com搜索你的模块名称,如果搜索不到,就可以用,并且把package.json里的name修改过来,重新npm publish,看到如下信息就表示安装完成了,rc-fullpage就是我的模块名。

Package.json格式

{
  "name": "my_package",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "ag_dubs",
  "license": "ISC",
  "repository": {
    "type": "git",
    "url": "https://github.com/ashleygwilliams/my_package.git"
  },
  "bugs": {
    "url": "https://github.com/ashleygwilliams/my_package/issues"
  },
  "homepage": "https://github.com/ashleygwilliams/my_package"
}

name: defaults to author name unless in a git directory, in which case it will be the name of the repository
version: always 1.0.0
main: always index.js
scripts: by default creates a empty test script
keywords: empty
author: whatever you provided the CLI
license: [ISC](https://opensource.org/licenses/ISC)
repository: will pull in info from the current directory, if present
bugs: will pull in info from the current directory, if present
homepage: will pull in info from the current directory, if present

参考链接:
https://docs.npmjs.com/all#option-1-change-the-permission-to-npms-default-directory

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容