如何发布Node模块到NPM上?

一、创建项目


$ mkdir test-NPM //新建test-NPM 文件夹
$ cd test-NPM  //进入test-NPM 文件夹
$ npm init //#输入包名,版本号,已经其他信息,使用默认直接按回车

结果

package name: (my-test-1)
version: (1.0.1)
git repository:
author: luoshushu
license: (ISC)
About to write to /Users/susonglin/Desktop/test-NPM/package.json:

{
  "name": "my-test-1",
  "version": "1.0.1",
  "description": "测试",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "luoshushu",
  "license": "ISC",
  "dependencies": {},
  "keywords": []
}


Is this ok? (yes) y

接下来依次填写, 不想填写也可以一路(回车)Enter。

  • name:
    你的模块名称。(必填)

  • version:
    版本号。(只要修改项目版本号就必须修改,必填)

  • description:
    描述自己的模块。

  • main:
    指定了程序的主入口文件,require('xxx') 就会加载这个文件。这个字段的默认值是模块根目录下面的 index.js。

  • scripts:
    命令行。

  • author:
    作者信息,可以之后编辑更详细一些。

  • license:
    代码授权许可。参考

  • keywords:
    关键字,可以通过npm搜索你填写的关键词找到你的模块。

  • git repository:
    git仓库地址

以上可以大胆填写,过后可以修改,更多配置请看

初始化完成,项目中多出了package.json 文件

{
  "name": "my-test-1",
  "version": "1.0.1",
  "description": "测试",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "luoshushu",
  "license": "ISC",
  "dependencies": {},
  "keywords": []
}

二、编写代码


1、新建index.js(测试)
2、新建README.md(说明)
3、如图

测试.png

三、发布模块


1、需要NPM账号,没有就注册
2、在终端登录NPM

$ npm login

# 输入登录用户名
# 输入登录密码
# 输入的邮箱

ps:输入的用户、密码、邮箱是当时www.npmjs.com注册的时候填的

3、发布

$ npm publish

//出现以下是发布成功了
+ my-test-1@1.0.1

4、我踩的坑


坑1:

npm ERR! publish Failed PUT 403
npm ERR! code E403
npm ERR! You do not have permission to publish "oh-my-lib". Are you logged in as the correct user? : oh-my-lib

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/susonglin/.npm/_logs/2017-12-04T10_37_31_216Z-debug.log

原因:

你没有权限发布“oh-my-lib”。您是否登录为正确的用户?:oh-my-lib,模块名称同名了,换个模块名就好。

解决:

"name": "oh-my-lib",  

//换成
"name": "my-test-1",

坑2:

npm ERR! publish Failed PUT 403
npm ERR! code E403
npm ERR! You cannot publish over the previously published version 1.0.1. : my-test-1

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/susonglin/.npm/_logs/2017-12-04T10_12_14_606Z-debug.log

原因:

您不能在之前发布的1.0.1版本上发布。:my-test-1 ,也就是我已经发布过一次了。

解决:

修改版本号。把"version": "1.0.1",改成 "version": "1.0.2",


坑3:

npm ERR! publish Failed PUT 403
npm ERR! code E403
npm ERR! no_perms Private mode enable, only admin can publish this module: my-test-1

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/susonglin/.npm/_logs/2017-12-04T10_18_32_973Z-debug.log
susonglindeMacBook-Pro:test-NPM susonglin$ npm publish
npm ERR! publish Failed PUT 403
npm ERR! code E403
npm ERR! no_perms Private mode enable, only admin can publish this module: my-test-1

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/susonglin/.npm/_logs/2017-12-04T10_19_16_518Z-debug.log

原因:

因为之前我为了加快下载速度用了国内的淘宝地址,只要换回原来的地址,重新登陆、发布即可。

解决:

$ npm config delete registry
$ npm login
$ npm publish

ps: 加快下载速度方法:$ npm config set registry https://registry.npm.taobao.org/

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

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,973评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,466评论 25 708
  • npm是什么 NPM的全称是Node Package Manager,是随同NodeJS一起安装的包管理和分发工具...
    build1024阅读 7,972评论 0 9
  • 最近使用 Npm 发布包时遇到了一些坑,集中记录下以便后续查看。 01. 就个人经验来说,我们写的包往哪儿发布,无...
    dkvirus阅读 8,273评论 0 6
  • 负能量 负能量 负能量 都说了 千万不要做自己做不好的事情当工作 都说了 我不行 我不会 我做不好 为什么还要我继...
    是我啊啊吗吗阅读 240评论 0 0