node package manage

NodeJs 中包管理及分发工具。

NPM - Node Package Manager

node 包管理工具,是全球最大的开源库生态系统。
网络环境,node 包安装或下载速度慢时,可使用淘宝 npm 镜像 cnpm

1
2
# npm install -g cnpm --registry=https://registry.npm.taobao.org
# cnpm install <package_name>
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
Usage: npm <command>

where <command> is one of:
access, adduser, audit, bin, bugs, c, cache, ci, cit,
clean-install, clean-install-test, completion, config,
create, ddp, dedupe, deprecate, dist-tag, docs, doctor,
edit, explore, get, help, help-search, hook, i, init,
install, install-ci-test, install-test, it, link, list, ln,
login, logout, ls, org, 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:
/Users/lipingzhang/.npmrc
or on the command line via: npm <command> --key value
Config info can be viewed via: npm help config

npm@6.8.0 /usr/local/lib/node_modules/npm

退出 node 环境

  1. 两次ctrl+C
  2. 一次ctrl+D
  3. process.exit()
  4. .exit

npm 库

命令行界面解决方式

The complete solution for node.js command-line interfaces

postcss-pxtorem

react-app-polyfill

babel-polyfill 重写,适用于 react 框架,用于ES6 内置 API 兼容处理,使用方法

  • 安装 npm i react-app-polyfill
  • 入口引用,具体如下:
    1
    2
    3
    // main.js
    import "react-app-polyfill/IE11"
    ...

dotenv

NodeJs 运行时加载不同的配置,process.env.DB_HOST 获取环境变量,程序启动时,从文件加载环境变量时就需要用到 dotenv 库。

用法 创建 .env

1
2
3
4
# .env
DB_HOST = localhost
DB_USER = root
DB_PASS = 123456

NODE 中运行

1
2
3
4
5
6
7
8
9
10
const dotenv = require('dotenv')
dotenv.config()

// 程序中使用环境变量
const db = require('db')
db.connect({
host: process.env.DB_HOST,
username: process.env.DB_USER,
password: process.env.DB_PASS
})

debug 专门控制日志输出的库

判断 DEBUG 环境变量,调整运行环境控制日志是否输出。DEBUG 对环境变量进行解析,允许我们选择性的控制输出哪些日志模块,解决控制台日志堆积的问题。