NPM 库推荐
enquirer
交互式问答 CLI
javascript
const { prompt } = require('enquirer')
const response = await prompt({
type: 'input',
name: 'username',
message: 'What is your username?'
})
console.log(response)
//=> { username: xxx }
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
minimist
轻量级命令行参数解析引擎
javascript
const argv = require('minimist')(process.argv.slice(2));
console.log(argv);
1
2
3
2
3
$ node example/parse.js -x 3 -y 4 -n5 -abc --beep=boop foo bar baz
{ _: [ 'foo', 'bar', 'baz' ],
x: 3,
y: 4,
n: 5,
a: true,
b: true,
c: true,
beep: 'boop'
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
commander
node.js 命令行界面的完整解决方案
js
const { program } = require('commander');
program
.option('--first')
.option('-s, --separator <char>');
program.parse();
const options = program.opts();
const limit = options.first ? 1 : undefined;
console.log(program.args[0].split(options.separator, limit));
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
$ node split.js -s / --fits a/b/c
error: unknown option '--fits'
(Did you mean --first?)
$ node split.js -s / --first a/b/c
// [ 'a' ]
1
2
3
4
5
6
7
2
3
4
5
6
7
semver
语义化版本
javascript
const semver = require('semver')
semver.valid('1.2.3') // '1.2.3'
1
2
3
2
3
markdown
版本格式:主版本号.次版本号.修订号,版本号递增规则如下:
主版本号:当你做了不兼容的 API 修改,
次版本号:当你做了向下兼容的功能性新增,
修订号:当你做了向下兼容的问题修正。
先行版本号及版本编译信息可以加到 “主版本号.次版本号.修订号” 的后面,作为延伸。
1
2
3
4
5
2
3
4
5
execa
执行命令
javascript
import { execa } from 'execa';
const { stdout } = await execa('echo', [ 'unicorns' ]);
console.log(stdout);
//=> 'unicorns'
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
chalk
终端多彩输出
javascript
const chalk = require('chalk');
console.log(chalk.blue('Hello world!'));
1
2
3
2
3
processenv
解析环境变量
ts
import { processenv } from 'processenv';
const port = processenv('PORT');
// const port = processenv('PORT', 3000); // 提供默认值
1
2
3
4
5
6
2
3
4
5
6
cross-env
设置环境变量
json
{
"scripts": {
"build": "cross-env NODE_ENV=production webpack --config build/webpack.config.js"
}
}
1
2
3
4
5
2
3
4
5
dotenv
环境变量从 .env 文件加载到 process.env 中
js
import * as dotenv from 'dotenv' // see https://github.com/motdotla/dotenv#how-do-i-use-dotenv-with-import
dotenv.config()
1
2
2
dotenv-expand
扩展计算机上已经存在的环境变量
js
const dotenv = require('dotenv')
const dotenvExpand = require('dotenv-expand')
const myEnv = dotenv.config()
dotenvExpand.expand(myEnv)
console.log(process.env)
1
2
3
4
5
6
7
2
3
4
5
6
7
slash
系统路径符处理
js
import path from 'node:path';
import slash from 'slash';
const string = path.join('foo', 'bar');
// Unix => foo/bar
// Windows => foo\\bar
slash(string);
// Unix => foo/bar
// Windows => foo/bar
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
hash-sum
非常快的唯一哈希生成器
js
const sum = require('hash-sum');
console.log(sum([ 0, 1, 2, 3 ])) // 00a34759
console.log(sum('1988-06-09T03:00:00.000Z')) // dff5ee3c
1
2
3
4
2
3
4
address
获取当前机器的IP, MAC和DNS服务器
js
const address = require('address');
// default interface 'eth' on linux, 'en' on osx.
address.ip(); // '192.168.0.2'
address.ipv6(); // 'fe80::7aca:39ff:feb0:e67d'
address.mac(function (err, addr) {
console.log(addr); // '78:ca:39:b0:e6:7d'
});
// local loopback
address.ip('lo'); // '127.0.0.1'
// vboxnet MAC
address.mac('vboxnet', function (err, addr) {
console.log(addr); // '0a:00:27:00:00:00'
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
default-gateway
通过对OS路由接口的exec调用获得机器的默认网关
js
const defaultGateway = require('default-gateway');
const { gateway, interface } = await defaultGateway.v4();
// gateway = '1.2.3.4', interface = 'en1'
const { gateway, interface } = await defaultGateway.v6();
// gateway = '2001:db8::1', interface = 'en2'
const { gateway, interface } = defaultGateway.v4.sync();
// gateway = '1.2.3.4', interface = 'en1'
const { gateway, interface } = defaultGateway.v6.sync();
// gateway = '2001:db8::1', interface = 'en2'
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
lru-cache
删除最近最少使用的项的缓存对象
js
const LRU = require('lru-cache')
const options = {
max: 500,
// for use with tracking overall storage size
maxSize: 5000,
sizeCalculation: (value, key) => {
return 1
},
// for use when you need to clean up something when objects
// are evicted from the cache
dispose: (value, key) => {
freeFromMemoryOrWhatever(value)
},
// how long to live in ms
ttl: 1000 * 60 * 5,
// return stale items before removing from cache?
allowStale: false,
updateAgeOnGet: false,
updateAgeOnHas: false,
// async method to use for cache.fetch(), for
// stale-while-revalidate type of behavior
fetchMethod: async (key, staleValue, { options, signal }) => {
},
}
const cache = new LRU(options)
cache.set('key', 'value')
cache.get('key') // "value"
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
36
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
36
xmldom
Node.js 的 xml 解析器;支持浏览器的 DOMParser 和 XMLSerializer 接口
js
const { DOMParser } = require('xmldom')
const doc = new DOMParser().parseFromString(
'<xml xmlns="a" xmlns:c="./lite">\n' +
'\t<child>test</child>\n' +
'\t<child></child>\n' +
'\t<child/>\n' +
'</xml>',
'text/xml'
)
doc.documentElement.setAttribute('x', 'y')
doc.documentElement.setAttributeNS('./lite', 'c:x', 'y2')
console.info(doc)
const nsAttr = doc.documentElement.getAttributeNS('./lite', 'x')
console.info(nsAttr)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
juice
CSS 属性内联到 style 属性
js
const juice = require('juice');
const result = juice("<style>div{color:red;}</style><div/>");
// <div style="color: red;"></div>
1
2
3
4
2
3
4
html-minifier
基于 JavaScript 的 HTML 压缩工具
js
const minify = require('html-minifier').minify;
const result = minify('<p title="blah" id="moo">foo</p>', {
removeAttributeQuotes: true
});
result; // '<p title=blah id=moo>foo</p>'
1
2
3
4
5
2
3
4
5
release-it
用于自动执行版本控制和包发布相关任务的通用 CLI 工具
shell
npm init release-it
# or
npm install -D release-it
1
2
3
2
3
json
{
"name": "my-package",
"version": "1.0.0",
"scripts": {
"release": "release-it"
},
"devDependencies": {
"release-it": "*"
}
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
colors
在 node.js 控制台中获取颜色和样式
js
const colors = require('colors/safe');
console.log(colors.green('hello')); // outputs green text
console.log(colors.red.underline('i like cake and pies')) // outputs red underlined text
console.log(colors.inverse('inverse the color')); // inverses the color
console.log(colors.rainbow('OMG Rainbows!')); // rainbow
console.log(colors.trap('Run the trap')); // Drops the bass
1
2
3
4
5
6
7
2
3
4
5
6
7
globby
路径匹配工具
markdown
├── unicorn
├── cake
└── rainbow
1
2
3
2
3
js
import { globby } from 'globby';
const paths = await globby([ '*', '!cake' ]);
console.log(paths);
//=> ['unicorn', 'rainbow']
1
2
3
4
5
6
7
2
3
4
5
6
7
fast-glob
一个非常快速和高效的 Node.js glob 库
js
const fg = require('fast-glob');
const entries = fg.sync([ '.editorconfig', '**/index.js' ], { dot: true });
// ['.editorconfig', 'services/index.js']
1
2
3
4
5
2
3
4
5
chokidar
最小且高效的跨平台文件监视库
js
const chokidar = require('chokidar');
// One-liner for current directory
chokidar.watch('.').on('all', (event, path) => {
console.log(event, path);
});
1
2
3
4
5
6
2
3
4
5
6
gray-matter
从字符串或文件中解析 front-matter,也支持 YAML、JSON、TOML 或 Coffee Front-Matte
js
import * as matter from 'gray-matter';
console.log(matter('---\ntitle: Front Matter\n---\nThis is content.'));
const output = {
content: '\nThis is content.',
data: {
title: 'Front Matter'
}
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9