npm-hello-world-demo
v1.0.2
Published
a sample tutorial that step to step publish npm pageage.
Downloads
4
Readme
npm publish 操作步骤
- 编写模块
1)新建文件夹,比如:npm-hello-world-demo
- 该文件夹下新建js文件,比如:index.js
js内容如下:
exports.sayHello=function(){ return "Hello world!"; };
- 初始化包描述文件
使用cmd命令定位到npm-hello-world-demo文件夹
输入 npm init 并执行
npm的init命令可以帮助你生成package.json文件,这是我的文件内容:
{
"name": "your_package_name",
"version": "1.0.0",
"description": "'something...'",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"Hello",
"world"
],
"author": "Your Name",
"license": "MIT"
}
- 注册包仓库账号
npm adduser
输入这个命令会有提示输入用户名,密码,邮箱等资料
这和去官方源仓库https://www.npmjs.com/注册是一样的
- 上传包
npm publish
如果上传成功会提示
否则上传失败
这个时候去https://www.npmjs.com/登陆仓库账号就可以看到自己的包啦
- 安装包
npm install npm-hello-world-demo
通过此命令可以在世界上任一一台机器上安装npm-hello-world-demo了
发布包过程可能会遇到很多问题,我印象比较深刻的是npm ERR publish 403
You do not have permission to publish 'npm-hello-world-demo'.Are you logged in as
the corrent user?:npm-hello-world-demo
意思是我没权限发布somepackage,并问我是否使用了正确的账号,
那也许是npm-hello-world-demo被别人发布过了吧,所以我修改了package.json文件
把name改成npm-hello-world-demo1.
- 分析包
这个命令可以为你分析出当前路径下能够通过模块路径找到的所有包,并生成依赖树。
npm ls