美文网首页程序员饥人谷技术博客
如何发布Node模块到NPM上?

如何发布Node模块到NPM上?

作者: 取个帅气的名字真好 | 来源:发表于2017-12-04 18:54 被阅读400次

一、创建项目


$ 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/

相关文章

网友评论

    本文标题:如何发布Node模块到NPM上?

    本文链接:https://www.haomeiwen.com/subject/qxpobxtx.html