1) 注册一个npm帐号 https://www.npmjs.com/signup’,并验证邮箱。邮箱未验证的话,不能发布模块
2) 创建一个目录,如gulp-demo
,可通过npm init
初始化自己的模块,目录结构eg:
lib //存放封装好的代码
node_modules //存放依赖,最后publish的时候不会上传
index.js //入口文件,正常情况下都是index.js
LICENSE //声明文件
package.json
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
发布
D:\project\gulp-demo>npm login
Username: youname
Password:
Email: (this IS public) yourname@xxx.com
Logged in as xieguishi on http://r.cnpmjs.org/.
D:\project\gulp-demo>npm publish
npm ERR! publish Failed PUT 401
npm ERR! code E401
npm ERR! 404 unauthorized Login first: gulp-demo
npm ERR! 404
npm ERR! 404 'gulp-demo' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\tx156425456\AppData\Roaming\npm-cache\_logs\2018-03-28T06_36_56_787Z-debug.log
上面的代码的问题,提示不是在npm注册,仔细一看,发现登录的是 cnpmjs,这是设置了代码的缘故
通过nrm
切换回 npm
D:\project\gulp-demo>nrm ls
npm ---- https://registry.npmjs.org/
* cnpm --- http://r.cnpmjs.org/
taobao - https://registry.npm.taobao.org/
nj ----- https://registry.nodejitsu.com/
rednpm - http://registry.mirror.cqupt.edu.cn/
npmMirror https://skimdb.npmjs.com/registry/
edunpm - http://registry.enpmjs.org/
D:\project\gulp-demo>nrm use npm
Registry has been set to: https://registry.npmjs.org/
再次执行 npm login
npm publish
,发布成功
D:\project\gulp-demo>npm publish
+ gulp-demo@1.0.0
发布成功后,我们就可以通过 npm install gulp-demo --save-dev
来安装使用了
ps:
如果注册的邮箱没有验证,会提示
D:\project\gulp-demo>npm publish
npm ERR! publish Failed PUT 403
npm ERR! code E403
npm ERR! you must verify your email before publishing a new package: https://www.npmjs.com/email-edit : gulp-demo
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\tx156425456\AppData\Roaming\npm-cache\_logs\2018-03-28T06_39_42_688Z-debug.log
internal/validators.js:124 throw new ERR_INVALID_ARG_TYPE(name, ‘string‘, value)
如何在VUE项目中添加stylelint,检查css,scss,less的语法问题,保证团队代码的规范统一
npm ERR! Cannot read property 'match' of undefined
nodejs 社区乃至 Web 前端工程化领域发展到今天,作为 node 自带的包管理工具的 npm 已经成为每个前端开发者必备的工具。但是现实状况是,我们很多人对这个nodejs基础设施的使用和了解还停留在: 会用 npm install 这里(一言不合就删除整个 node_modules 目录然后重新 install 这种事你没做过吗?)
如何发布一个npm模块到npmjs.com
nrm是一个npm源管理工具,利用它我们可以快速切换npm源,利用nrm还可以测试哪个源最快。如果哪个npm依赖安装失败,不防切换一下npm源试试
npm 教程
阮一峰老师的package.json文件详解