npm 常用指令介绍

npm usage

what is npm?

实现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 有专门针对 个人,组织,公司的账户,需要付费,可以协助多人开发工作

npm command line interface

一、 npm –help && npm install

C:\Users\by>npm --help

Usage: npm <command>

where <command> 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 <command> -h     quick help on <command>
npm -l           display full usage info
npm help <term>  search for help on <term>
npm help npm     involved overview

Specify configs in the ini-formatted file:
    C:\Users\by\.npmrc
or on the command line via: npm <command> --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 <command> is one of: 这里告诉我们 npm 有哪些执行可以使用的,感觉好多,后面介绍几个常用指令

npm <command> -h: quick help on ,在 cmd interface 查看相关命令的简洁使用方法

C:\Users\by>npm install -h

npm install (with no args, in package dir)
npm install [<@scope>/]<pkg>
npm install [<@scope>/]<pkg>@<tag>
npm install [<@scope>/]<pkg>@<version>
npm install [<@scope>/]<pkg>@<version range>
npm install <folder>
npm install <tarball file>
npm install <tarball url>
npm install <git:// url>
npm install <github username>/<github project>

aliases: i, isntall, add
common options: [--save-prod|--save-dev|--save-optional] [--save-exact] [--no-save]

<@scope> 表示域: –local(一般省略) 和 –global 之分

<pkg> 表示js包名: npm install jquery 中 moment 就是包名

@<version> 表示 包的版本号: npm install [email protected]

npm help <term>: 这个用来本地查看某个指令的详细用法,比如: npm help install 就是本地打开 file:///C:/Users/by/AppData/Roaming/npm/node_modules/npm/html/doc/cli/npm-config.html这个指令和 npm <command> -h 是成对的

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

二、package.json 文件

创建一个 package.json 文件

指令: 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"
}

package.json常用 介绍

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

本地安装

不保存到 package.json 文件中

npm install [--local] moment

  1. 后面 –local 全部省略,下载 moment.js 在 node_modules 中,可以直接在html 页面中引用
  2. 也可以使用 es6 import moment from 'moment',或者 commonJS require('moment')
保存到 package.json 文件中

npm install <packageName> [--save|--save-dev]

执行命令: npm install moment --savenpm 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 可以查看是否安装成功

四、npm uninstall

要修改 package.json 文件 需要使用参数 --save

本地卸载(删除)模块

三个命令:

1.npm uninstall <pk name>
- 从 node_modules 中卸载(删除) 对应包名模块,不会修改package.json 文件 如 npm uninstall moment

2.npm uninstall <pk name> --save
- 从 node_modules 中卸载模块并且 删除 package.json 文件中生产环境依赖, 如npm uninstall moment --save

3.npm uninstall <pk name> --save-dev
- 从 node_modules 中卸载模块并且 删除 package.json 文件中 开发环境依赖,如: npm uninstall lodash --save-dev

最终,node_modules 文件夹空了(注意一定一定要打开文件夹查看,编辑器不准),然后 package.json 对应的改变:

{
  // ...
 "dependencies": {},
  "devDependencies": {}
  // ...
}

全局卸载模块

npm uninstall --global <pk name>

比如我们删除 vue-cli 工具: npm uninstall --global vue-cli

五、npm list

使用这条指令可以 展示本地或者全局的 已安装包的列表

本地 已安装包 列表

不一定在 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 <pkg>: 查看本地某个模块是否存在


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 <pkg> --depth 0


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 package@version

版本构成

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"
  }
// ...    
}

七、npm install from a package.json

主要讲述了怎么从 一个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 update pkg --save

更新本地生产环境*指定的包并写入 pakage.json 文件

npm update --dev --save-dev

更新本地开发环境所有的包并写入 pakage.json 文件

npm update --global [pkg]

  1. npm update --global 更新所有的全局工具
  2. npm update --global pkg 更新 指定的 全局工具

npm 自身更新

npm install --global npm@latest

九、npm prune

用来删除 本地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]

十、npm shortcuts

传送门

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

npm cli 官方参考文档(英)

传送门

猜你喜欢

转载自blog.csdn.net/palmer_kai/article/details/79919002