首页
Javascript
Html
Css
Node.js
Electron
移动开发
小程序
工具类
服务端
浏览器相关
前端收藏
其他
关于
公司注册

在npmjs上发布自己的模块

2018年03月29日 发布 阅读(2526) 作者:Jerman

一、准备工作

1) 注册一个npm帐号 https://www.npmjs.com/signup’,并验证邮箱。邮箱未验证的话,不能发布模块

2) 创建一个目录,如gulp-demo,可通过npm init初始化自己的模块,目录结构eg:

  1. lib //存放封装好的代码
  2. node_modules //存放依赖,最后publish的时候不会上传
  3. index.js //入口文件,正常情况下都是index.js
  4. LICENSE //声明文件
  5. package.json
  6. README.md

3) npm int 相关设置

name:填写你这个包的名字,默认是你这个文件夹的名字。不过这里要着重说一下,最好先去npm上找一下有没有同名的包。最好的测试方式就是,在命令行里面输入npm install 你要取的名字,如果报错,那么很好,npm上没有跟你同名的包,你可以放心大胆地把包发布出去。如果成功下载下来了。。。那么很不幸,改名字吧。。。

version:你这个包的版本,默认是1.0.0

description:其实我也不知道是什么,按回车就好了。。。,这个用一句话描述你的包是干嘛用的,比如我就直接:‘a plugin for express.register routes base on file path’

entry point:入口文件,默认是Index.js,你也可以自己填写你自己的文件名

test command:测试命令,这个直接回车就好了,因为目前还不需要这个。

git repository:这个是git仓库地址,如果你的包是先放到github上或者其他git仓库里,这时候你的文件夹里面会存在一个隐藏的.git目录,npm会读到这个目录作为这一项的默认值。如果没有的话,直接回车继续。

keyword:这个是一个重点,这个关系到有多少人会搜到你的npm包。尽量使用贴切的关键字作为这个包的索引。我这个包嘛,第一是在express下工作的,然后又是一个插件plugin,然后又是一个注册路由route用的,而这个路由又是基于文件目录dir,所以很好就得出我的包的索引关键字。

author:写你的账号或者你的github账号吧

license:这个直接回车,开源文件来着。。。

二、发布

在目录gulp-demo下打开命令行,用注册的帐号密码登录npm

通过两步发一个模块

npm login 用于登录npm,输入用户名密码,邮箱即可,密码输入的时候不会显示

npm publish 发布

  1. D:\project\gulp-demo>npm login
  2. Username: youname
  3. Password:
  4. Email: (this IS public) yourname@xxx.com
  5. Logged in as xieguishi on http://r.cnpmjs.org/.
  6. D:\project\gulp-demo>npm publish
  7. npm ERR! publish Failed PUT 401
  8. npm ERR! code E401
  9. npm ERR! 404 unauthorized Login first: gulp-demo
  10. npm ERR! 404
  11. npm ERR! 404 'gulp-demo' is not in the npm registry.
  12. npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
  13. npm ERR! 404
  14. npm ERR! 404 Note that you can also install from a
  15. npm ERR! 404 tarball, folder, http url, or git url.
  16. npm ERR! A complete log of this run can be found in:
  17. npm ERR! C:\Users\tx156425456\AppData\Roaming\npm-cache\_logs\2018-03-28T06_36_56_787Z-debug.log

上面的代码的问题,提示不是在npm注册,仔细一看,发现登录的是 cnpmjs,这是设置了代码的缘故

通过nrm切换回 npm

  1. D:\project\gulp-demo>nrm ls
  2. npm ---- https://registry.npmjs.org/
  3. * cnpm --- http://r.cnpmjs.org/
  4. taobao - https://registry.npm.taobao.org/
  5. nj ----- https://registry.nodejitsu.com/
  6. rednpm - http://registry.mirror.cqupt.edu.cn/
  7. npmMirror https://skimdb.npmjs.com/registry/
  8. edunpm - http://registry.enpmjs.org/
  9. D:\project\gulp-demo>nrm use npm
  10. Registry has been set to: https://registry.npmjs.org/

再次执行 npm login npm publish,发布成功

  1. D:\project\gulp-demo>npm publish
  2. + gulp-demo@1.0.0

发布成功后,我们就可以通过 npm install gulp-demo --save-dev来安装使用了

ps:
如果注册的邮箱没有验证,会提示

  1. D:\project\gulp-demo>npm publish
  2. npm ERR! publish Failed PUT 403
  3. npm ERR! code E403
  4. npm ERR! you must verify your email before publishing a new package: https://www.npmjs.com/email-edit : gulp-demo
  5. npm ERR! A complete log of this run can be found in:
  6. npm ERR! C:\Users\tx156425456\AppData\Roaming\npm-cache\_logs\2018-03-28T06_39_42_688Z-debug.log
版权声明:本站文章除特别声明外,均采用署名-非商业性使用-禁止演绎 4.0 国际 许可协议,如需转载,请注明出处