npm包管理

安装模块

输入

1
$ npm install express

install 方式用来安装模块 -g 是全局安装, 不加安装在当前

1
node_modules
目录中

查询已安装的模块

输入

1
$ npm ls -g

卸载模块

1
$ npm uninstall express

更新模块

1
$ npm update express

搜索模块

1
$ npm search express

创建模块

  • 输入
1
npm init
  • 按提示
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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> --save` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
name: (02-npm) npm-module
version: (1.0.0) 1.0.0
description:
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to /Users/hans/Documents/project/JavaScriptCodes/nodejs-do/02-npm/package.json:

{
  "name": "npm-module",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}


Is this ok? (yes) yes
  • 查看
    1
    
    package.json
1
2
3
4
5
6
7
8
9
10
11
{
  "name": "npm-module",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

清空NPM本地缓存

1
$ npm cache clear

使用淘宝 NPM 镜像

  • 方式一: 安装 cnpm
1
$ npm install -g cnpm --registry=https://registry.npm.taobao.org

使用

1
$ cnpm install [name]
  • 方式二: 通过config命令
1
2
npm config set registry https://registry.npm.taobao.org 
npm info underscore (如果上面配置正确这个命令会有字符串response)
  • 方式三: 命令行指定
1
npm --registry https://registry.npm.taobao.org info underscore
  • 方式四: 编辑 ~/.npmrc 加入下面内容
1
registry = https://registry.npm.taobao.org

REPL 交互式命令行

表达式计算

1
2
3
4
5
6
7
8
9
10
11
> x = 7
7
> y = 10
10
> x + y
17
> var yy = _
undefined
> console.log(yy)
17
undefined

自动识别多行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
> do {
... x++;
... console.log(x);
... }while(x < 20);
8
9
10
11
12
13
14
15
16
17
18
19
20

常用命令

1
2
3
4
5
6
7
8
9
10
ctrl + c - 退出当前终端。
ctrl + c 按下两次 - 退出 Node REPL。
ctrl + d - 退出 Node REPL.
向上/向下 键 - 查看输入的历史命令
tab 键 - 列出当前命令
.help - 列出使用命令
.break - 退出多行表达式
.clear - 退出多行表达式
.save filename - 保存当前的 Node REPL 会话到指定文件
.load filename - 载入当前 Node REPL 会话的文件内容。

代码

https://github.com/hans007/JavaScriptCodes/tree/master/nodejs-do

我的博客