发布或删除Node模块到NPM社区

“学骑自行车最快的方式就是先骑上去”

本文主要介绍如何发布自己的npm插件到npm社区,那么别人就可以直接使用npm install的命令安装你的模块了,并不会讲解npm模块的编写方法和相关知识,其实你可以自己研究一下其他npm模块的代码。本文最后会介绍如何删除自己发布的npm模块。

发布或删除Node模块到NPM社区

安装node和npm

安装node(注意:需要Python 2.6或2.7已经安装)

1
2
3
$ wget http://nodejs.org/dist/v0.10.26/node-v0.10.26.tar.gz
$ tar -zxvf node-v0.10.26.tar.gz
$ make && make install

成功安装node之后,npm(node package manager)已经包含在内了,不需要独立安装,可以执行一下npm检查是否安装好。

1
2
$ npm
Usage: npm <command>

创建repository

接下来我们需要创建module所需的repository,默认你拥有一个github账号(如果没有自行解决),这个repository用来托管我们module的代码,并方便用户报告bug,最重要的是可以让其他开发者向module贡献代码,这也是乐趣所在。

初始化package.json

接下来我们创建package.json文件,这个文件包含了module的所有信息,比如名称、版本、描述、依赖、作者、license等。笔者以自己的easy_mongo(注意:npm要求项目的名称必须为小写字母)项目为例,看如何创建package.json。

首先创建我们module的目录

1
$ mkdir easy_mongo && cd easy_mongo

之后执行npm init,系统会提示你输入所需的信息,不想输入的直接输入Enter可以跳过。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sane 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: (node) easy_mongo
version: (0.0.0) 0.1.0
description: An easy mongodb client for node.js based on native mongodb driver.
entry point: (index.js)
test command: make test
git repository: https://github.com/JeremyWei/easy_mongo.git
keywords: Mongodb node easy
author: JeremyWei
license: (BSD-2-Clause) MIT

输入完成之后,系统会要你确认文件的内容是否有误,如果没有问题直接输入yes,那么package.json就创建好了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
About to write to /Users/weizhifeng/dev/node/package.json:
{
“name”: “easy_mongo”,
“version”: “0.1.0”,
“description”: “An easy mongodb client for node.js based on native mongodb driver.”,
“main”: “index.js”,
“directories”: {
“example”: “examples”
},
“scripts”: {
“test”: “make test”
},
“repository”: {
“type”: “git”,
“url”: “https://github.com/JeremyWei/easy_mongo.git”
},
“keywords”: [
“Mongodb”,
“node”,
“easy”
],
“author”: “JeremyWei”,
“license”: “MIT”,
“bugs”: {
“url”: “https://github.com/JeremyWei/easy_mongo/issues”
}
}
Is this ok? (yes)

npm init创建的package.json文件只包含了基本的信息,我们还需要加入对其他module的依赖关系:

1
2
3
4
5
“dependencies”: {
“mocha” : “1.18.2”,
“should” : “3.3.0”,
“mongodb” : “1.3.19”
}

项目结构

package.json文件只是第一步,要完成一个module,我们还需要许多其他文件,整体的文件夹结构如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
easy_mongo
├─┬ lib
│ └── easy_mongo.js
├─┬ test
│ └── all.js
├── .gitignore
├── .npmignore
├── .travis.yml
├── index.js
├── LICENSE
├── makefile
├── package.json
├── README.md

这些文件的作用是:

1
2
3
4
5
6
7
lib目录下存放业务逻辑文件
test目录下存放单元测试用例
.npmignore记录哪些文件不需要被发布到npmjs.org
.travis.yml是持续集成服务travis的描述文件
index.js是入口文件
makefile方便我们用make test进行测试
README.md是此module的描述和使用方法

持续集成

开源项目多如牛毛,从中找出靠谱的项目需要花费一定的精力,开发者都会对持续更新,并且经过测试(很多公司采用)的项目更加的信赖,对于刚上线并且用户数量很少的项目开发者都会有个疑虑:这项目靠谱吗?所以你需要对自己的项目打上一个标识:老子的项目靠谱。如何做?持续集成。

目前Github已经整合了持续集成服务travis,我们只需要在项目中添加.travis.yml文件,在下一次push之后,travis就会定时执行npm test来测试你的项目,并且会在测试失败的时候通知到你,你也可以把项目当前的状态显示在README.md中,让人一目了然,比如:

1
Build Status

.travis.yml是一个YAML文件,关于node.js相关的配置见这里,例子如下:

1
2
3
4
5
6
7
language: node_js
node_js:
– “0.10”
– “0.8”
– “0.6”
services:
– mongodb

这个例子的是让travis在node.js的0.6.x,0.8.x,0.10.x三个版本下对项目进行测试,并且需要mongodb的服务。

发布

完成了上面的步骤之后,我们接下来要在www.npmjs.org注册一个账号,这个账号会被添加到npm本地的配置中,用来发布module用。

1
2
3
4
$ npm adduser
Username: your name
Password: your password
Email: yourmail[@gmail](/user/gmail).com

如果出现以下错误,可能是你的npm版本太低,通过sudo npm install -g npm升级一下。

1
2
3
4
5
6
7
8
9
npm WARN adduser Incorrect username or password
npm WARN adduser You can reset your account by visiting:
npm WARN adduser
npm WARN adduser http://admin.npmjs.org/reset
npm WARN adduser
npm ERR! Error: forbidden may not mix password_sha and pbkdf2
npm ERR! You may need to upgrade your version of npm:
npm ERR! npm install npm -g
npm ERR! Note that this may need to be run as root/admin (sudo, etc.)

成功之后,npm会把认证信息存储在~/.npmrc中,并且可以通过以下命令查看npm当前使用的用户:

1
$ npm whoami

以上完成之后,我们终于可以发布自己的module了:

1
2
3
4
$ npm publish –tag 0.1.0
npm http PUT https://registry.npmjs.org/easy_mongo
npm http 201 https://registry.npmjs.org/easy_mongo
+ easy_mongo[@0](/user/0).1.0

npm社区版本号规则采用的是semver(语义化版本),主要规则如下:

版本格式:主版号.次版号.修订号,版号递增规则如下:
主版号:当你做了不相容的 API 修改,
次版号:当你做了向下相容的功能性新增,
修订号:当你做了向下相容的问题修正。
先行版号及版本编译资讯可以加到「主版号.次版号.修订号」的后面,作为延伸。
至此,我们已经成功把module发布到了npmjs.org,是不是很简单,快动手把自己的module也贡献出来吧。

删除发布的模块

如何删除呢,执行下面的命令:

1
2
3
$ npm unpublish easy_mongo@0.1.0
或者
$ npm unpublish –force easy_mongo

参考

  1. https://github.com/npm/npm
  2. https://www.npmjs.org/doc/api/npm-publish.html
  3. https://www.npmjs.org/doc/cli/npm-adduser.html
  4. http://docs.travis-ci.com/user/languages/javascript-with-nodejs/
  5. http://docs.travis-ci.com/user/database-setup/
  6. http://semver.org/

谢谢!

转载请注明出处:http://www.fed123.com/2015/12/11/2015_npm_publish/
欢迎关注皓眸学问公众号(扫描左侧二维码),每天好文、新技术!任何学习疑问或者工作问题都可以给我留言、互动。T_T 皓眸大前端开发学习 T_T

未经允许不得转载:前端学堂fed123(Frontend education) » 发布或删除Node模块到NPM社区

赞 (0)
分享到:更多 ()

评论 7

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. 匿名现在使用unpublish 已经没用了,npm 不允许删除上传时间超过24h的包了。回复
    • chalecao👍, 你说的对回复
  2. 2018goupThanks a lot for providing individuals with an extraordinarily wonderful opportunity to read critical reviews from this web site. It's usually so lovely and full of fun for me and my office co-workers to search your website particularly 3 times weekly to see the latest guides you have. Of course, I'm so certainly fulfilled with all the mind-boggling ideas you give. Certain 2 tips in this post are basically the most beneficial we have all had.回复
  3. 2018goupThank you so much for giving everyone an exceptionally superb possiblity to discover important secrets from this website. It is often so kind and packed with fun for me personally and my office mates to visit your site a minimum of 3 times in 7 days to read the fresh secrets you have. And indeed, I am just always amazed with all the excellent points you give. Selected two facts on this page are really the simplest we have all had.回复
  4. 2018goupI wish to voice my admiration for your kind-heartedness giving support to persons who really need guidance on this one concept. Your personal commitment to passing the solution all around was surprisingly interesting and has helped most people just like me to arrive at their desired goals. Your entire useful hints and tips implies a lot to me and additionally to my colleagues. Regards; from each one of us.回复
  5. 2018goupI really wanted to develop a note so as to thank you for the lovely tips you are writing on this website. My incredibly long internet investigation has finally been compensated with wonderful facts and techniques to talk about with my colleagues. I would say that we readers are really fortunate to exist in a fantastic place with many wonderful people with interesting tactics. I feel truly lucky to have discovered the web pages and look forward to tons of more fun minutes reading here. Thanks once more for everything.回复
  6. 2018goupI simply had to say thanks all over again. I'm not certain the things I might have sorted out without the thoughts revealed by you about this area. Completely was a very frustrating condition in my view, nevertheless seeing a new specialized strategy you handled it forced me to cry over delight. I am grateful for this assistance and thus hope you are aware of a great job your are putting in instructing people today via your web site. More than likely you haven't got to know any of us.回复