发布自定义组件到npm

原文地址:https://segmentfault.com/a/1190000006250554

github创建项目

https://github.com 注册账户,新建项目,然后clone到本地。

终端进入到项目文件夹,执行npm init命令,构建模块的描述文件,系统会提示你输入所需的信息,不想输入就直接Enter跳过。这里主要的几个配置如下:

  • name就是你要发布的module名;
  • version版本信息(每发布一次版本号都必须大于上一次发布的版本号);
  • entry入口文件。
$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg> --save` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
name: (react-native-dmg-pingpp) 
version: (0.0.1) 
license: (MIT) 

npm注册

输入完用户名,密码,邮箱后没有错误信息就完成了。

$ npm adduser
Username: your name
Password: your password
Email: (this IS public) your email

查询或者登陆别的用户命令:

$ npm whoami
$ npm login

npm module 发布

module开发完毕后,剩下的就是发布啦,进入项目根目录,输入命令。

$ npm publish

可能遇到的问题:

  • 问题1:
npm ERR! no_perms Private mode enable, only admin can publish this module:

这里注意的是因为国内网络问题,许多小伙伴把npm的镜像代理到淘宝或者别的地方了,这里要设置回原来的镜像。

npm config set registry=http://registry.npmjs.org
  • 问题2:
npm ERR! you do not have permission to publish "your module name". 
Are you logged in as the correct user?

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

+ react-native-dmg-pingpp@0.0.1

更新版本,发布

$ npm version 0.1.1
$ npm publish
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容