实现js代码的复用,分享和管理
npm consists of three distinct components:
1. the website
通过 www.npmjs.com 网址查找需要的 包
2. the Command Line Interface (CLI)
通过命令行界面 进行包的安装…
3. the registry
我的理解就是一个 js 模块的登记处,这里就是一个 js 模块的公共库。我们可以从这里下载自己需要的模块,同时也可以分享js 模块到这里,供别人使用
Use the website to discover packages, set up profiles, and manage other aspects of your npm experience. For example, you can set up Orgs (organizations) to manage access to public or private packages.
The CLI runs from a terminal. This is how most developers interact with npm.
The registry is a large public database of JavaScript software and the meta-information surrounding it.
npm 有专门针对 个人,组织,公司的账户,需要付费,可以协助多人开发工作
C:\Users\by>npm --help
Usage: npm
where is one of:
access, adduser, bin, bugs, c, cache, ci, completion,
config, ddp, dedupe, deprecate, dist-tag, docs, doctor,
edit, explore, get, help, help-search, i, init, install,
install-test, it, link, list, ln, login, logout, ls,
outdated, owner, pack, ping, prefix, profile, prune,
publish, rb, rebuild, repo, restart, root, run, run-script,
s, se, search, set, shrinkwrap, star, stars, start, stop, t,
team, test, token, tst, un, uninstall, unpublish, unstar,
up, update, v, version, view, whoami
npm -h quick help on
npm -l display full usage info
npm help search for help on
npm help npm involved overview
Specify configs in the ini-formatted file:
C:\Users\by\.npmrc
or on the command line via: npm --key value
Config info can be viewed via: npm help config
npm@5.8.0 C:\Users\by\AppData\Roaming\npm\node_modules\npm
where
: 这里告诉我们 npm 有哪些执行可以使用的,感觉好多,后面介绍几个常用指令
npm
: quick help on ,在 cmd interface 查看相关命令的简洁使用方法
C:\Users\by>npm install -h
npm install (with no args, in package dir)
npm install [<@scope>/]
npm install [<@scope>/]@
npm install [<@scope>/]@
npm install [<@scope>/]@
npm install
npm install
npm install
npm install <git:// url>
npm install /
aliases: i, isntall, add
common options: [--save-prod|--save-dev|--save-optional] [--save-exact] [--no-save]
<@scope>
表示域: –local(一般省略) 和 –global 之分
表示js包名: npm install jquery
中 moment 就是包名
@
表示 包的版本号: npm install [email protected]
npm help
: 这个用来本地查看某个指令的详细用法,比如: npm help install
就是本地打开 file:///C:/Users/by/AppData/Roaming/npm/node_modules/npm/html/doc/cli/npm-config.html
, 这个指令和 npm
是成对的
npm help npm
: 这个指令是本地打开 npm 的介绍文档file:///C:/Users/by/AppData/Roaming/npm/node_modules/npm/html/doc/cli/npm.html
aliases: i, isntall, add
表示 install 这个指令的别名,如:npm i jquery
等价于 npm install jquery
common options:
表示可选项, 比如:
npm install jquery --save
npm install jquery --save-dev
指令: npm init [--yes]
执行 npm init
一路 enter 键 ,等价于 npm init --yes
{
"name": "npm_usage_f_youtube",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
官网案例
{
"name": "my_package",
"description": "",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/ashleygwilliams/my_package.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/ashleygwilliams/my_package/issues"
},
"homepage": "https://github.com/ashleygwilliams/my_package"
}
name: 表示当前目录的名字
version, main: 一般不用管
scripts: 可以通过 npm run + 指令名字
在node 平台 执行js 文件
{
// ...
"scripts": {
"start": "node ./js/demo.js",
"demo": "node ./js/demo.js"
},
// ...
}
因为start 是默认值 所以可以直接: npm start
来启动一个 node application
demo 指令需要这样调用: npm run demo
,同样可以用来启动一个 node application
npm config set init.author.email "[email protected]"
npm config set init.author.name "ag_dubs"
npm config set init.license "MIT"
使用 命令 npm config list
都是可以查看到设置了哪些 默认项
npm install [--local] moment
import moment from 'moment'
,或者 commonJS require('moment')
npm install
执行命令: npm install moment --save
和 npm install lodash --save-dev
,对应 package.json 文件增加了两个属性成员
"dependencies": {
"moment": "^2.22.0"
},
"devDependencies": {
"lodash": "^4.17.5"
}
dependecies: 表示生产环境依赖的模块
devDependencies: 表示开发环境依赖的模块
一般作为工具使用的模块我们使用 全局安装
如vue的脚手架工具: npm install --global vue-cli
vue -V
可以查看是否安装成功
要修改 package.json 文件 需要使用参数
--save
三个命令:
1.npm uninstall
- 从 node_modules 中卸载(删除) 对应包名模块,不会修改package.json 文件 如 npm uninstall moment
2.npm uninstall
- 从 node_modules 中卸载模块并且 删除 package.json 文件中生产环境依赖, 如npm uninstall moment --save
3.npm uninstall
- 从 node_modules 中卸载模块并且 删除 package.json 文件中 开发环境依赖,如: npm uninstall lodash --save-dev
最终,node_modules 文件夹空了(注意一定一定要打开文件夹查看,编辑器不准),然后 package.json 对应的改变:
{
// ...
"dependencies": {},
"devDependencies": {}
// ...
}
npm uninstall --global
比如我们删除 vue-cli 工具: npm uninstall --global vue-cli
使用这条指令可以 展示本地或者全局的 已安装包的列表
不一定在 json 文件中保存,node_modules中的模块
npm list --depth 0
一般深度为零,不然会展示所有的依赖模块树结构
PS C:\Users\MSI-PC\Desktop\npm_usage_f_youtube> npm list --depth 0
[email protected] C:\Users\MSI-PC\Desktop\npm_usage_f_youtube
+-- [email protected] extraneous
+-- [email protected]
`-- [email protected]
npm list
: 查看本地某个模块是否存在
PS C:\Users\MSI-PC\Desktop\npm_usage_f_youtube> npm list jquery
[email protected] C:\Users\MSI-PC\Desktop\npm_usage_f_youtube
`-- (empty)
npm list --global true --depth 0
PS C:\Users\MSI-PC\Desktop\npm_usage_f_youtube> npm list --global true --depth 0
C:\Users\MSI-PC\AppData\Roaming\npm
+-- browser-sync@2.18.8
+-- cnpm@5.2.0
+-- csslint@1.0.5
+-- eslint@4.18.2
+-- gulp-cli@1.2.2
+-- http-server@0.9.0
+-- i5ting_toc@1.1.4
+-- jshint@2.9.4
+-- less@2.7.2
+-- live-server@1.2.0
+-- nodemon@1.11.0
+-- npm@4.1.1
+-- protractor@5.1.1
+-- stylus@0.54.5
+-- vue-cli@2.9.3
+-- webpack@2.2.1
+-- xg-htmlhint@0.1.0
`-- yarn@0.22.0
查看某个模块是否存在: npm list --global true
PS C:\Users\MSI-PC\Desktop\npm_usage_f_youtube> npm list --global true webpack --depth 0
C:\Users\MSI-PC\AppData\Roaming\npm
`-- [email protected]
npm install pkg@version
version: 一般由3个数字构成
- 第一个数字 major version: 表示模块有非常巨大的功能特色变化,比如 angular2,angular4
- 第二个数字 minor version: 表示模块有些小的特色功能变化。
- 第三个数字 patch version: 表示模块 bug 补丁修复
npm install lodash --save
这个命令会安装对应的包的 lastest stable version
{
// ...
"dependencies": {
"lodash": "^4.17.5"
}
// ...
}
npm install [email protected] --save
这个命令会下载 版本为3.3.0 的lodash包
{
// ...
"dependencies": {
"lodash": "^3.3.0"
}
// ...
}
npm install [email protected] --save
这个命令会下载 4.14, latest && stable patch version
{
// ...
"dependencies": {
"lodash": "^4.14.2"
}
// ...
}
npm install lodash@4 --save
这个命令会下载 lodash 4., latest && stable minor , patch version
{
// ...
"dependencies": {
"lodash": "^4.17.5"
}
// ...
}
主要讲述了怎么从 一个package.json 文件安装符合我们要求的包版本
npm 包在发布的时候是可以指定版本范围的
感觉这个是针对于项目的发布者
{
// ...
"dependencies": {
"lodash": "^4.14.1"
},
// ...
}
上尖括号,在执行 npm install
后,会下载 latest version of minor && patch
PS C:\Users\MSI-PC\Desktop\npm_usage_f_youtube> npm install
[email protected] C:\Users\MSI-PC\Desktop\npm_usage_f_youtube
`-- [email protected]
{
// ...
"dependencies": {
"lodash": "~4.14.1"
}
// ...
}
波浪线,在执行 npm install
后会下载 latest patch version
PS C:\Users\MSI-PC\Desktop\npm_usage_f_youtube> npm install
[email protected] C:\Users\MSI-PC\Desktop\npm_usage_f_youtube
`-- [email protected]
官网说: major release ,下载的时候现在了最新的包
{
// ...
"dependencies": {
"lodash": "*"
},
// ...
}
{
// ...
"dependencies": {
"lodash": "3.0.1"
},
// ...
}
显然下载了 指定版本
PS C:\Users\MSI-PC\Desktop\npm_usage_f_youtube> npm install
[email protected] C:\Users\MSI-PC\Desktop\npm_usage_f_youtube
`-- [email protected]
If you were starting with a package 1.0.4, this is how you would specify the ranges:
Patch releases: 1.0 or 1.0.x or ~1.0.4
Minor releases: 1 or 1.x or ^1.0.4
Major releases: * or x
一般 npm update 更新本地包即可
至关重要的是: npm outdated [–local] 指令可以用来查看哪些指令过期
npm update pkg --save
更新本地生产环境*指定的包并写入 pakage.json 文件
npm update --save-dev
更新本地开发环境所有的包并写入 pakage.json 文件
npm update --global [pkg]
npm update --global
更新所有的全局工具npm update --global pkg
更新 指定的 全局工具npm install --global npm@latest
用来删除 本地package.json 文件中没有保存,但是我们下载过的模块
也就是我们项目不用依赖的模块
{
// ...
"dependencies": {
"lodash": "^4.17.5"
},
"devDependencies": {
}
// ...
}
PS C:\Users\MSI-PC\Desktop\npm_usage_f_youtube> npm list --depth 0
[email protected] C:\Users\MSI-PC\Desktop\npm_usage_f_youtube
+-- [email protected]
`-- [email protected] extraneous
prune 之后
PS C:\Users\MSI-PC\Desktop\npm_usage_f_youtube> npm prune
- [email protected] node_modules\moment
npm WARN [email protected] No repository field.
PS C:\Users\MSI-PC\Desktop\npm_usage_f_youtube> npm list --depth 0
[email protected] C:\Users\MSI-PC\Desktop\npm_usage_f_youtube
`-- [email protected]
传送门
The following shorthands are parsed on the command-line:
-v: –version
-h, -?, –help, -H: –usage
-s, –silent: –loglevel silent
-q, –quiet: –loglevel warn
-d: –loglevel info
-dd, –verbose: –loglevel verbose
-ddd: –loglevel silly
-g: –global
-C: –prefix
-l: –long
-m: –message
-p, –porcelain: –parseable
-reg: –registry
-f: –force
-desc: –description
-S: –save
-P: –save-prod
-D: –save-dev
-O: –save-optional
-B: –save-bundle
-E: –save-exact
-y: –yes
-n: –yes false
传送门