NPM使用详解(上)

时间:2023-03-09 16:55:24
NPM使用详解(上)

1、NPM是什么?

NPM是JavaScript的包管理工具,在安装NodeJS(什么?你不知道node?来,我们合计合计:https://nodejs.org/)的时候,会自动安装上npm。

要查看安装的npm版本,只需要打开cmd控制台,输入npm -v

NPM使得JavaScript开发者分享和重用代码非常容易,同时也让你能否非常方便的更新你分享的代码。

NPM能够自己升级自己,使用命令如下: npm install npm -g

2、NPM的使用

以下代码示例中:<>表示必选参数,[]表示可选参数

#最常用命令


2.1、 init:用于初始化项目

/*
* npm init [-f|--force|-y|--yes]
*/ //在文件夹中打开cmd,然后输入npm init,打开项目初始化向导
npm init //如果文件夹名称满足npm的module name,
//那么使用如下方式,可以直接生成一个默认的package.json
//如果文件夹名称不满足要求,那么会出错
npm init -f
npm init --force
npm init --force=true
npm init -y
npm init --yes
npm init --yes=true

2.2、install:用于安装模块

/*
* npm install (with no args in a package dir)
* npm install <tarball file>
* npm install <tarball url>
* npm install <folder>
* npm install [@<scope>/]<name> [--save|--save-dev|--save-optional] [--save-exact]
* npm install [@<scope>/]<name>@<tag>
* npm install [@<scope>/]<name>@<version>
* npm install [@<scope>/]<name>@<version range>
* npm i (with any of the previous argument usage)
*/ //直接使用npm install 或者是npm i,表示根据package.json,安装所有依赖
npm install
npm i //如果加上--production参数,那么只会安装dependencies的模块,
//而不会安装devDependencies的内模块
npm install --production
npm i --production //使用全局上下文来初始化
npm install -g
npm i -g //安装指定模块
npm install <packageName>
npm install <packageName> -g //全局安装
npm install <packageName>@<version> //指定要安装的模块版本
npm install <packageName>@<version_start-version_end> //指定要安装的模块版本
npm install <packageName> --registry=<url> //指定零食的仓库地址
npm install <packageName> --msvs_version=<vs_version> //指定编译使用的VS版本
npm install <packageName> --save // 安装模块并修改package.json的dependencies
npm install <packageName> --save-dev //安装模块并修改package.json的devDependencies npm install <tarball url> //从指定的压缩包地址安装,示例如下:
npm install https://github.com/indexzero/forever/tarball/v0.5.6 npm install <tarball file> //从指定的压缩包安装,如下(注意压缩包格式):
npm install del-1.2.0.tar.gz //使用.tgz和.tar.gz格式 npm install @<scope>/<packageName> //安装私有包

2.3、uninstall:用于卸载模块

/*
* npm uninstall [@<scope>/]<package> [--save|--save-dev|--save-optional]
*/ //直接卸载模块,加上-g参数,表示卸载全局的模块
npm uninstall <packageName>
npm uninstall <packageName> -g //卸载时同时修改package.json文件
npm uninstall <packageName> --save-dev
npm uninstall <packageName> --save

2.4、update:用于更新模块

/*
* npm update [-g] [<name> [<name> ...]]
*/ //更新一个或多个模块,加上-g参数,表示更新全局的模块
npm update <packageName> [packageName2...]
npm update <packageName> [packageName2...] -g //更新时同时修改package.json文件
npm update <packageName> [packageName2...] --save-dev
npm update <packageName> [packageName2...] --save

2.5、config:用于设置npm参数

//设置指定参数
npm config set <key> <value> [--global]
npm set <key> <value> [--global] //可以省略config
//获取现有参数值
npm config get <key>
npm get <key> //可以省略config
//删除指定参数,此时参数值会变为默认值
npm config delete <key>
//查看npm信息;注意:此命令不是查看所有参数配置
npm config list
//编辑全量的npm配置文件(.npmrc)
npm config edit
//可以将config使用c代替,执行以上所有命令
npm c [set|get|delete|list]

2.6、cache:管理包缓存

//将指定的包加入npm缓存
npm cache add <tarball file>
npm cache add <folder>
npm cache add <tarball url>
npm cache add <name>@<version>
//查看现有的npm包缓存,如果加上path参数,则查看该路径下的文件
npm cache ls [<path>]
eg: npm cache ls gulp
//清空缓存。如果加上path,则清理指定路径下的包缓存
npm cache clean [<path>]
eg: npm cache clean gulp

*:first-child {
margin-top: 0 !important;
}

body>*:last-child {
margin-bottom: 0 !important;
}

/* BLOCKS
=============================================================================*/

p, blockquote, ul, ol, dl, table, pre {
margin: 15px 0;
}

/* HEADERS
=============================================================================*/

h1, h2, h3, h4, h5, h6 {
margin: 20px 0 10px;
padding: 0;
font-weight: bold;
-webkit-font-smoothing: antialiased;
}

h1 tt, h1 code, h2 tt, h2 code, h3 tt, h3 code, h4 tt, h4 code, h5 tt, h5 code, h6 tt, h6 code {
font-size: inherit;
}

h1 {
font-size: 28px;
color: #000;
}

h2 {
font-size: 24px;
border-bottom: 1px solid #ccc;
color: #000;
}

h3 {
font-size: 18px;
}

h4 {
font-size: 16px;
}

h5 {
font-size: 14px;
}

h6 {
color: #777;
font-size: 14px;
}

body>h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h4:first-child, body>h5:first-child, body>h6:first-child {
margin-top: 0;
padding-top: 0;
}

a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 {
margin-top: 0;
padding-top: 0;
}

h1+p, h2+p, h3+p, h4+p, h5+p, h6+p {
margin-top: 10px;
}

/* LINKS
=============================================================================*/

a {
color: #4183C4;
text-decoration: none;
}

a:hover {
text-decoration: underline;
}

/* LISTS
=============================================================================*/

ul, ol {
padding-left: 30px;
}

ul li > :first-child,
ol li > :first-child,
ul li ul:first-of-type,
ol li ol:first-of-type,
ul li ol:first-of-type,
ol li ul:first-of-type {
margin-top: 0px;
}

ul ul, ul ol, ol ol, ol ul {
margin-bottom: 0;
}

dl {
padding: 0;
}

dl dt {
font-size: 14px;
font-weight: bold;
font-style: italic;
padding: 0;
margin: 15px 0 5px;
}

dl dt:first-child {
padding: 0;
}

dl dt>:first-child {
margin-top: 0px;
}

dl dt>:last-child {
margin-bottom: 0px;
}

dl dd {
margin: 0 0 15px;
padding: 0 15px;
}

dl dd>:first-child {
margin-top: 0px;
}

dl dd>:last-child {
margin-bottom: 0px;
}

/* CODE
=============================================================================*/

pre, code, tt {
font-size: 12px;
font-family: Consolas, "Liberation Mono", Courier, monospace;
}

code, tt {
margin: 0 0px;
padding: 0px 0px;
white-space: nowrap;
border: 1px solid #eaeaea;
background-color: #f8f8f8;
border-radius: 3px;
}

pre>code {
margin: 0;
padding: 0;
white-space: pre;
border: none;
background: transparent;
}

pre {
background-color: #f8f8f8;
border: 1px solid #ccc;
font-size: 13px;
line-height: 19px;
overflow: auto;
padding: 6px 10px;
border-radius: 3px;
}

pre code, pre tt {
background-color: transparent;
border: none;
}

kbd {
-moz-border-bottom-colors: none;
-moz-border-left-colors: none;
-moz-border-right-colors: none;
-moz-border-top-colors: none;
background-color: #DDDDDD;
background-image: linear-gradient(#F1F1F1, #DDDDDD);
background-repeat: repeat-x;
border-color: #DDDDDD #CCCCCC #CCCCCC #DDDDDD;
border-image: none;
border-radius: 2px 2px 2px 2px;
border-style: solid;
border-width: 1px;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
line-height: 10px;
padding: 1px 4px;
}

/* QUOTES
=============================================================================*/

blockquote {
border-left: 4px solid #DDD;
padding: 0 15px;
color: #777;
}

blockquote>:first-child {
margin-top: 0px;
}

blockquote>:last-child {
margin-bottom: 0px;
}

/* HORIZONTAL RULES
=============================================================================*/

hr {
clear: both;
margin: 15px 0;
height: 0px;
overflow: hidden;
border: none;
background: transparent;
border-bottom: 4px solid #ddd;
padding: 0;
}

/* IMAGES
=============================================================================*/

img {
max-width: 100%
}
-->