npm是什么?
NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题。
使用场景
- 允许用户从NPM服务器下载别人编写的第三方包到本地使用。
- 允许用户从NPM服务器下载并安装别人编写的命令行程序到本地使用。
- 允许用户将自己编写的包或命令行程序上传到NPM服务器供别人使用。
npm怎么安装?
由于新版的nodejs已经集成了npm,所以之前npm也一并安装好了。同样可以通过输入 "npm -v" 来测试是否成功安装。命令如下,出现版本提示表示安装成功:
$ npm -v
6.13.4
当然旧版的话就得手动升级,不过现在没有人用旧版了吧?这里就不放旧版的了,不啰嗦啦~开整~
初始化/创建一个package.json 包/模块
win+R打开开始输入cmd
打开Doss窗口 或者使用 git的 Git Bash Here
Administrator@SKY-20160224WZM MINGW64 ~/Desktop/npmtest
$ npm init //创建初始化 package.json 模块包
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>` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (npmtest) name //包名
version: (1.0.0) 1.0.1 //版本号,不填默认是 1.0.0
description: JieShao //介绍
entry point: (index.js) index.js //入口文件名,具体是做什么的可以自己查询一下,不知道的话写 `index.js`即可
test command: make test //测试命令,具体是做什么的自己查询
git repository: https:\/\/github.com/xxx/xxx/xx //GitHub仓库地址
keywords: GuanJianZi //包名/模块 的关键字
author: ZuoZheMingZi //作者名字
license: (ISC) ISC //许可证,MIT,ISC等好几种证书,详情自己百度或者查看后面的文章[七种开源许可证cid:205]链接上面的203改成205即可
About to write to C:\Users\Administrator\Desktop\npmtest\package.json:
{
"name": "name",
"version": "1.0.1",
"description": "JieShao",
"main": "index.js",
"scripts": {
"test": "make test"
},
"repository": {
"type": "git",
"url": "https://github.com/xxx/xxx/xx"
},
"keywords": [
"GuanJianZi"
],
"author": "ZuoZheMingZi",
"license": "ISC"
}
Is this OK? (yes) yes //以上信息输入完毕后这里填写`yes` 即可保存.
创建好后继续在Doss
窗口内输入一下代码进行登录
Administrator@SKY-20160224WZM MINGW64 ~/Desktop/npmtest
$ npm login //进行登录
Username: uaerneme //npmjs的帐号
Password: password //npmjs的密码
Email: (this IS public) xxxxx@foxmail.com //npmjs的邮箱
npm notice Please check your email for a one-time password (OTP)
Enter one-time password from your authenticator app: 14707627 //npm给你的邮箱发送的验证码,正常都是8位数
Logged in as anwenanyi on https://registry.npmjs.org/. //输入以上信息后会提示这个,说明登录成功.
登录成功后进行上传操作
npm publish //登录成功后输入此命令回车进行发布~ 等发布成功后去 npmjs上面看就能看到啦~
..... //上传中...
..... //上传中...
..... //上传中...
..... //上传中...
..... //上传中...
..... //上传中...
..... //上传中...
..... //上传中...
..... //上传中...
//上传结束
npm notice ---Tarball Details ===
npm notice name: xxx
npm notice verison: 1.0.0
npm notice package size: 1.3 MB
npm notice unpacked size: 3.5 MB
npm notice shasum: 2a82xxxbd5bxxxd0xxxxed0fcb02xxxx222xxx4f
npm notice integrity: sha512-wxLoxxxxI59Ip[...]1IZxxxxxiusR==
npm notice total files: 186
npm notice
+xxx@1.0.0
//发布生成,可以去npmjs官网看你自己的包/模块 啦~
OK以上就是在npm
上传自己的包/模块 啦~
注意事项
- 发布/升级新版本的时候,先修改
package.json
中的版本号(version)
,然后再执行npm publish
;(不修改版本号上传会失败) - 上传时不能使用淘宝镜像
npm config set registry https://registry.npmjs.org