一、先列一下总体的步骤:
二、具体操作:
1:新建目录
<pre>
$ mkdir npm_module_test
</pre>
2:初始化,输入npm init,直接按enter下一步,在git repository步骤可先输入url占位
<pre>
$ cd npm_module_test/
$ 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: (npm_module_test)
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository: www.xxx.com
keywords:
author:
license: (ISC)
About to write to /Users/baidu/Desktop/work/npm/npm_module_test/package.json:
{
"name": "npm_module_test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo "Error: no test specified" && exit 1"
},
"repository": {
"type": "git",
"url": "www.xxx.com"
},
"author": "",
"license": "ISC"
}
Is this ok? (yes)
</pre>
3、创建index.js,输入下列代码
<pre>
function test(e) {
console.log(e);
};
exports.test = test;
</pre>
4、创建test.js,输入下列代码
<pre>
var Mynpm = require('./index.js');
Mynpm.test('hello');
</pre>
5、执行node test,确认输出正常
<pre>
$ node test.js
hello
</pre>
6、发布到github上,得到代码仓库地址
7、修改package.json中的git repository地址
8、发布到npm
a:https://www.npmjs.com注册账号
b:执行npm adduser
<pre>
$ npm adduser
Username: 注册的用户名
Password: 注册的密码
Email: (this IS public) 注册的邮箱
Logged in as 用户名 on http://registry.npmjs.org/.
</pre>
c:执行npm publish,如下则表示发布成功
<pre>
$ npm publish
- 包名@1.0.0
</pre>