1 新建一个mypackagemodule的文件
2 在命令行下切换到给文件下,执行npm init,按要求提示填写
E:\node\mypackagemodule>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: (mypackagemodule) jinhan
version: (1.0.0) 1.0.0
description: this is my frist mypackagemodule
entry point: (index.js) index.js
test command: none
git repository: none
keywords:
author: jinhan
license: (ISC)
About to write to E:\node\mypackagemodule\package.json:
{
"name": "jinhan",
"version": "1.0.0",
"description": "this is my frist mypackagemodule",
"main": "index.js",
"scripts": {
"test": "none"
},
"repository": {
"type": "git",
"url": "none"
},
"author": "jinhan",
"license": "ISC"
}
Is this ok? (yes) yes
E:\node\mypackagemodule>
3 填写成功后在mypackagemodule文件下会出现package.json文件
创建一个index.js作为包的接口,一个包就这样制造好了。
在发布前,我们还需要获得一个账号用于今后维护自己的包,使用
npm adduser 根据提示输入用户名、密码、邮箱,等待账号创建完成。完成后可以使用 npm whoami 测验是否已经取得了账号。接下来,在 package.json 所在目录下运行 npm publish ,稍等片刻就可以完成发布了。打开浏览器,访问 http://search.npmjs.org/ 就可以找到自己刚刚发布的包了。现在我们可以在世界的任意一台计算机上使用 npm install mypackagemodule 命令来安装它,如果你的包将来有更新,只需要在 package.json 文件中修改version字段,然后重新使用 npm publish 命令就行了。如果你对已发布的包不满意(比如我们发布的这个毫无意义的包),可以使用 npm unpublish 命令来取消发布。