💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
[TOC] # 模板引擎 [art-template](https://aui.github.io/art-template/) 是一个简约、超快的模板引擎。 它采用作用域预声明的技术来优化模板渲染速度,从而获得接近 JavaScript 极限的运行性能,并且同时支持 NodeJS 和浏览器。[在线速度测试](https://aui.github.io/art-template/rendering-test/)。 [EJS](https://ejs.co/) [handlebars](http://handlebarsjs.com/) [jade](http://jade-lang.com/) # loT ## Node-RED 官网:https://github.com/node-red # 系统 ## systeminformation 官网:https://hub.fastgit.org/sebhildebrandt/systeminformation # Web开发 ## Ramda Ramda is a practical functional library for JavaScript programmers. The library focuses on immutability and side-effect free functions. Ramda functions are also automatically curried. ~~~ $ npm i ramda ~~~ ## cheerio 官网:https://github.com/cheeriojs/cheerio jQuery 核心功能的 服务器端实现 ## Osmosis 官网:https://github.com/rchipka/node-osmosis Osmosis 是一个 HTML/XML 解析器和网页数据采集工具。它是用 Node.js 编写的,用 CSS3/xpath 选择器和轻量级 HTTP 包装器打包。它与 Cheerio、jQuery 和 jsdom 相比,它没有显著的依赖项。 ## Nightmare 官网:https://github.com/segmentio/nightmare 一个来自 Segment 的高级浏览器的自动化库。 ## request-promise 官网:https://github.com/request/request-promise Request-Promise 是 npm 库中的一个变体,它通过自动化的浏览器提供了一个更快的解决方案。当内容不是动态呈现时,可以使用此网页数据采集工具。如果要处理的网站有一个认证系统,它可以是一个更先进的解决方案。如果我们把它和 Puppeteer 相比,它的用法正好相反。 > 目前已被不赞成使用,可替代:https://github.com/request/request/issues/3143 ## monk 官网:https://automattic.github.io/monk/ 说明:一个很小的层,为Node.JS中的MongoDB使用提供了简单而实质性的可用性改进。 ## joi 官网:[https://npm.io/package/joi](https://npm.io/package/joi) 说明:Object schema description language and validator for JavaScript objects. ## morgan HTTP request logger middleware for node.js # 文件操作 ## rimraf 官网:https://github.com/isaacs/rimraf#readme 说明:以包的形式包装`rm -rf`命令,用来删除文件和文件夹的,不管文件夹是否为空,都可删除. 安装: ~~~ npm i -D rimraf ~~~ 使用: ~~~ const rimraf = require('rimraf'); rimraf('./test.txt', function (err) { // 删除当前目录下的 test.txt console.log(err); }); ~~~ ## chokidar 官网:https://github.com/paulmillr/chokidar 说明: 使用: ## node-watch 官网:https://github.com/yuanchuan/node-watch 说明: 使用: # 辅助 npm ## npm-check-updates 官网:https://github.com/tjunnone/npm-check-updates 说明:npm 包版本更新工具,可以将`package.json` 或 `bower.json`中的依赖项升级到最新版。 使用:升级项目的包文件 ~~~ $ ncu -u express 4.12.x → 4.13.x ~~~ ## Concurrently 官网:https://github.com/kimmobrunfeldt/concurrently#readme 说明:在前端工程化的时候,需要同时启动**并行多个命令**。但是本身的 `&` 有时会意外出现问题,所以需要一个更好的解决方法。 安装: ~~~ npm i -D concurrently ~~~ 使用: 首先通过 安装开发依赖。然后按照以下格式将其添加到脚本中: ~~~ copy{ "start": "concurrently \"command1 arg\" \"command2 arg\"" } ~~~ > 前端工程化并行解决方案-concurrently](https://zhuanlan.zhihu.com/p/65564606) ## npm-run-all 官网:http://github.com/mysticatea/npm-run-all 说明:一个CLI工具,可以并行或连续运行多个npm脚本。 > npm 本身也可以并行运行命令:参考《Web 前端工程化-node.js-`npm run`》部分。 使用: 为了使事情更加一致,我们可以使用一个名为`npm-run-all`的包。它提供了额外的命令,更具体地说就是,用`run-s`来运行串联任务, 用 `run-p` 来运行并行任务,它将正确处理所有的子进程。 并行: ``` "scripts": { "lint": "run-p eslint csslint htmllint" // "npm-run-all --parallel eslint csslint htmllint" } ``` 串行: ``` "scripts": { "build": "run-s babel jest" } ``` ## ntl 官网:https://github.com/ruyadorno/ntl#readme 使用:script 的任务管理器 ~~~ ntl ~~~ ## json 官网:https://github.com/trentm/json 说明:`package.json` 是一个常规的 `json` 文件,因此可以使用工具库 [json](http://trentm.com/json/) 从命令行进行编辑。 这在修改 `package.json` 提供另外一种新的方式,允许 w 你 q 创建超出默认值的快捷方式。 使用: 全局安装: ~~~ npm i -g json json --version ~~~ 然后,可以使用它来使用 `-I` 进行就地编辑。 例如,要添加值为 “bar” 的新脚本 “foo”,这样写: ~~~shell json -I -f package.json -e 'this.scripts.foo="bar"' ~~~ # 环境变量 ## cross-env * Windows 临时配置 ~~~shell # node中常用的到的环境变量是NODE_ENV,首先查看是否存在 set NODE_ENV # 如果不存在则添加环境变量 set NODE_ENV=production # 环境变量追加值 set 变量名=%变量名%;变量内容 set path=%path%;C:\web;C:\Tools # 某些时候需要删除环境变量 set NODE_ENV= ~~~ * linux 临时配置 ~~~shell # node中常用的到的环境变量是NODE_ENV,首先查看是否存在 echo $NODE_ENV # 如果不存在则添加环境变量 export NODE_ENV=production # 环境变量追加值 export path=$path:/home/download:/usr/local/ # 某些时候需要删除环境变量 unset NODE_ENV # 某些时候需要显示所有的环境变量 ls env ~~~ 问题是:`set` 只在 windows 下管用,在 Linux 下要使用 `export`,所有才有了 `cross-env` ### 使用方法 * 安装 `npm i  -S cross-env` * 在 `NODE_ENV=xxxxxxx` 前面添加 `cross-env` 就可以了。(Linux方式) ~~~ { ... "scripts": { "demo2": "cross-var echo $npm_package_name", "demo1": "cross-env NODE_ENV=test node src/index.js" } ... } ~~~ ### vue 3.0 项目 `.env` 文件配置全局环境变量 首先根目录下创建. ``` .env 或者.env.production 文件(生产环境), .env.development 文件(开发环境) ``` 变量命名格式: ``` VUE_APP_NAME = '' ``` `VUE_APP_` 是规定的命名格式,`NAME` 是自定义的变量名(这点和 CRA 很像 [https://create-react-app.dev/docs/adding-custom-environment-variables](https://create-react-app.dev/docs/adding-custom-environment-variables)) 通过: ``` process.env.variableName ``` 获取环境变量 ``` data():{   return{     url:process.env.VUE_APP_URL   } } ``` ## dotenv 官网:https://github.com/motdotla/dotenv 一个可以使得 Node.js **从文件中**加载环境变量的库,使用 `dotenv`,我们只需要将程序的环境变量配置写在 `.env` 文件中。 > create-react-app 就使用了这种方式,可以参考:[添加自定义环境变量](https://www.html.cn/create-react-app/docs/adding-custom-environment-variables/) > ~~~ > * .env :默认。 > * .env.local :本地覆盖。除 test 之外的所有环境都加载此文件。 > * .env.development , .env.test , .env.production :设置特定环境。 > * .env.development.local , .env.test.local , .env.production.local:设置特定环境的本地覆盖。 > ~~~ ~~~ # .env 默认文件 DB_HOST=localhost DB_USER=root DB_PASS=s1mpl3 ~~~ 然后,在 Node.js 程序启动时运行: ~~~ require('dotenv').config() ~~~ 接着,我们就可以在接下来的程序中方便地使用环境变量了: ~~~ const db = require('db') db.connect({ host: process.env.DB_HOST, username: process.env.DB_USER, password: process.env.DB_PASS }) ~~~ # 下载操作 ## degit 官网:https://github.com/Rich-Harris/degit 说明:git 仓库代码下载 ~~~shell npx degit sveltejs/template my-svelte-project ~~~ ## download-git-repo 官网:https://github.com/flippidippi/download-git-repo 说明:git 仓库代码下载 # 交互工具库 | 包名 | 描述 | | --- | --- | | sade | | | [sirv-cli](https://github.com/lukeed/sirv) | Quickly start a server to preview the assets of*any*directory! | | [Enquirer](https://github.com/enquirer/enquirer) | Stylish CLI prompts that are user-friendly, intuitive and easy to create. | | [validate-npm-package-name](https://www.npmjs.com/package/validate-npm-package-name) | 校验包名 | | [chalk](https://github.com/chalk/chalk) | 命令行输出样式美化 | | [ora](https://github.com/sindresorhus/ora) | 命令行有趣的进度输出 | | [didyoumean](https://www.npmjs.com/package/didyoumean) | 脚本命令匹配 | | [ncp](https://www.npmjs.com/package/ncp) | 异步的拷贝文件,包含空文件夹 | | [log-symbols](https://www.npmjs.com/package/log-symbols) | 打印日志的特殊标志 | | [recursive-readdir](https://www.npmjs.com/package/recursive-readdir) | 递归地列出目录和子目录下的所有文件,不包含目录本身。 | | [global-prefix](https://www.npmjs.com/package/global-prefix) | 获取 npm 全局安装的前缀 | | [exec-sh](https://www.npmjs.com/package/exec-sh) | 执行 shell 命令转发所有 stdio 流,比 shelljs exec 命令好用 | | [wml](https://www.npmjs.com/package/wml) | wml 侦听某个文件夹中的更改(使用 watchman),然后将更改的文件复制到另一个文件夹中。 | | [cmd-open](https://blog.ihaiu.com/cmd-open/) | 扩展 Windows 命令 open | | [shx](https://github.com/shelljs/shx) | shx 是对 ShellJS Unix 命令的包装,为 npm 包脚本中的简单的类 Unix 的跨平台命令提供了一个简单的解决方案。 | > [nodejs 交互工具库 -- hash-sum, deepmerge 和 yaml-front-matter](https://segmentfault.com/a/1190000037656240) ## Inquirer https://github.com/SBoudrias/Inquirer.js 命令行交互输入插件 ### 参数详解 * `type`: 表示提问的类型,包括:`input`, `confirm`, `list`, `rawlist`, `expand`, `checkbox`, `password`, `editor`; * `name`: 存储当前问题回答的变量; * `message`: 问题的描述; * `default`: 默认值; * `choices`: 列表选项,在某些 type 下可用,并且包含一个分隔符 (separator); * `validate`: 对用户的回答进行校验; * `filter`: 对用户的回答进行过滤处理,返回处理后的值; * `transformer`: 对用户回答的显示效果进行处理 (如:修改回答的字体或背景颜色),但不会影响最终的答案的内容; * `when`: 根据前面问题的回答,判断当前问题是否需要被回答; * `pageSize`: 修改某些 `type` 类型下的渲染行数; * `prefix`: 修改 `message` 默认前缀; * `suffix`: 修改 `message` 默认后缀。 ## fs-extra 官网:https://www.npmjs.com/package/fs-extra 说明:fs 的替代品,提供了比 原生 fs 更多的方法 ## semver 官网:https://www.npmjs.com/package/semver 说明:语义化日志控制 ## oclif 官网:https://github.com/oclif/oclif 说明:简单快速的命令行 App 开发框架 ## yargs 官网:https://github.com/yargs/yargs 说明:模块能够解决如何处理命令行参数。 安装 ~~~ npm i -S yargs ~~~ 使用: yargs 模块提供了 `argv` 对象,用来读取命令行参数 ~~~ #!/usr/bin/env node let argv = require('yargs').argv; console.log('hello ',argv.name); ~~~ 运行: ~~~ hello --name=zxmf hello --name zxmf ~~~ process.argv ~~~ [ '/usr/local/bin/node', '/usr/local/bin/hello4', '--name=zxmf' ] ~~~ Argv ~~~ { name: 'zxmf', } ~~~ ## execa 官网:[execa](https://hub.fastgit.org/sindresorhus/execa) 说明:比 [`child_process`](https://nodejs.org/api/child_process.html) 更友好的进程执行库 ## shelljs 官网:https://hub.fastgit.org/shelljs/shelljs 说明:这个库能够让我们在`js`文件中执行 shell 命令。 安装: ~~~ npm i -D shelljs ~~~ 使用: ``` //局部模式 var shell = require('shelljs'); //全局模式下,就不需要用shell开头了。 //require('shelljs/global'); if (shell.exec('npm run build').code !== 0) {//执行npm run build 命令 shell.echo('Error: Git commit failed'); shell.exit(1); } //由于我的用另外一个仓库存放dist目录,所以这里要将文件增量复制到目标目录。并切换到对应目录。 shell.cp ('-r', './dist/*', '../../Rychou'); shell.cd('../../Rychou'); shell.exec('git add .'); shell.exec("git commit -m 'autocommit'") shell.exec('git push') ``` ## Commander 官网:http://github.com/tj/commander.js 使用: * `command`: 定义命令行指令,后面可跟上一个 name,用空格隔开,如 `.command( 'app [name]')` * `alias`: 定义一个更短的命令行指令 * `description`: 描述,它会在 help 里面展示 * `option`: 定义参数。它接受四个参数 * 在第一个参数中,它可输入短名字 `-a` 和长名字 `–app`,使用 `|` 或者 `,` 分隔,在命令行里使用时,这两个是等价的,区别是后者可以在程序里通过回调获取到 * 第二个为描述,会在 `help` 信息里展示出来 * 第三个参数为回调函数,他接收的参数为一个 `string`,有时候我们需要一个命令行创建多个模块,就需要一个回调来处理 * 第四个参数为默认值 * `action`: 注册一个 `callback` 函数,这里需注意目前回调不支持 let 声明变量 * `parse`: 解析命令行 > [Commander.js 助力命令行程序开发](http://www.uedlinker.com/2018/08/13/commander-jszhu-li-qian-duan-tong-xue-ming-ling-xing-cheng-xu-kai-fa/) ## terminal-kit 官网:https://github.com/cronvel/terminal-kit