node命令行工具
node命令行工具
注意事项 https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
tsconfig.json
设置"module": "CommonJS",
package.json
一般不建议设置"type":"module"
不然编译出来的js文件需要加上后缀名import help from './help.js'
,然而ts编译出来的并没有后缀
cli依赖库
文件处理
常用cli库
pkg 将你的 Node.js 程序包装在一个可执行文件中。
portfinder 自动寻找没被占用的端口
json-server支持restfulapi增删改查
fs-jetpack Better file system API for Node.js
rc-config-loader 获取配置文件
node-windows Windows support for Node.JS scripts (daemons, eventlog, UAC, etc).
ohmyfetch A better fetch API. Works on node, browser and workers.
enquirer 类似 inquirer
meow 命令行工具
inquirer A collection of common interactive command line user interfaces
cac Command And Conquer is a JavaScript library for building CLI apps.
yargs 一个命令行参数解析工具
defu Assign default properties, recursively. Lightweight and Fast!
cheerio nodejs爬虫
shelljs 跨平台 Unix shell 命令 的 node 封装
blessed blessed-contrib
destr A faster, secure and convenient alternative for
JSON.parse
ufo url工具
pathe 路径工具类似nodejs的path
ora 命令行加载中图标
minimist 命令行解析
yaml js-yaml
pacote Fetches package manifests and tarballs from the npm registry.
cli-progress 命令行进度条
isomorphic-git git相关操作
npm-run-path Get your PATH prepended with locally installed binaries
wait-on wait for files, ports, sockets, and http(s) resources to become available
Cliffy - A Framework For Interactive CLIs
tasuku 一个taskrunner类似gulp
simple-git git客户端
http-proxy
got 类似ohmyfetch,node-fetch
superagent Ajax for Node.js and browsers (JS HTTP client)
socket.io Realtime application framework (Node.JS server)
tap test framework
Web frameworks
- Fastify - Fast and low overhead web framework.
- Next.js - Minimalistic framework for server-rendered universal JavaScript web apps.
- Nuxt.js - Minimalistic framework for server-rendered Vue.js apps.
- Hapi - Framework for building applications and services.
- Micro - Minimalistic microservice framework with an async approach.
- Koa - Framework designed by the team behind Express, which aims to be a smaller, more expressive, and more robust foundation for web applications and APIs.
- Express - Web application framework, providing a robust set of features for building single and multi-page, and hybrid web applications.
- Feathers - Microservice framework built in the spirit of Express.
- LoopBack - Powerful framework for creating REST APIs and easily connecting to backend data sources.
- Meteor - An ultra-simple, database-everywhere, data-on-the-wire, pure-Javascript web framework. (You might like awesome-meteor)
- Restify - Enables you to build correct REST web services.
- ThinkJS - Framework with ES2015+ support, WebSockets, REST API.
- ActionHero - Framework for making reusable & scalable APIs for TCP sockets, WebSockets, and HTTP clients.
- seneca - Toolkit for writing microservices.
- AdonisJs - A true MVC framework for Node.js built on solid foundations of Dependency Injection and IoC container.
- Moleculer - Fast & powerful microservices framework.
- Nest - Angular-inspired framework for building efficient and scalable server-side apps.
- TypeGraphQL - Modern framework for creating GraphQL APIs with TypeScript, using classes and decorators.
- Tinyhttp - Modern and fast Express-like web framework.
- Marble.js - Functional reactive framework for building server-side apps, based on TypeScript and RxJS.
- Lad - Framework made by a former Express TC and Koa member that bundles web, API, job, and proxy servers.
- Ts.ED - Intituive TypeScript framework for building server-side apps on top of Express.js or Koa.js.
数据库
- Drivers
- PostgreSQL - PostgreSQL client. Pure JavaScript and native libpq bindings.
- Redis - Redis client.
- LevelUP - LevelDB.
- MySQL - MySQL client.
- couchdb-nano - CouchDB client.
- Aerospike - Aerospike client.
- Couchbase - Couchbase client.
- MongoDB - MongoDB driver.
- ODM / ORM
- Sequelize - Multi-dialect ORM. Supports PostgreSQL, SQLite, MySQL, and more.
- Bookshelf - ORM for PostgreSQL, MySQL and SQLite3 in the style of Backbone.js.
- Mongoose - Elegant MongoDB object modeling.
- Waterline - Datastore-agnostic tool that dramatically simplifies interaction with one or more databases.
- OpenRecord - ORM for PostgreSQL, MySQL, SQLite3 and RESTful datastores. Similar to ActiveRecord.
- pg-promise - PostgreSQL framework for native SQL using promises.
- slonik - PostgreSQL client with strict types, detailed logging and assertions.
- Objection.js - Lightweight ORM built on the SQL query builder Knex.
- TypeORM - ORM for PostgreSQL, MariaDB, MySQL, SQLite, and more.
- MikroORM - TypeScript ORM based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, PostgreSQL, MySQL and SQLite.
- Prisma - Modern database access (ORM alternative). Auto-generated and type-safe query builder in TypeScript. Supports PostgreSQL, MySQL & SQLite.
- Query builder
- Knex - Query builder for PostgreSQL, MySQL and SQLite3, designed to be flexible, portable, and fun to use.
- Other
- NeDB - Embedded persistent database written in JavaScript.
- Lowdb - Small JavaScript database powered by Lodash.
- Keyv - Simple key-value storage with support for multiple backends.
- Finale - RESTful endpoint generator for your Sequelize models.
- database-js - Wrapper for multiple databases with a JDBC-like connection.
- Mongo Seeding - Populate MongoDB databases with JavaScript and JSON files.
- @databases - Query PostgreSQL, MySQL and SQLite3 with plain SQL without risking SQL injection.
- pg-mem - In-memory PostgreSQL instance for your tests.
其他
- config
- dotenv
- cross-env
- env-cmd
- dotenv-cli 推荐使用env-cmd+dotenv 然后在
main.ts
写
第一种使用cross-env
import dotenv from "dotenv";
dotenv.config({ path: `.env.${process.env.NODE_ENV}` });
在package.json写下script
"start":"cross-env NODE_ENV=dev esno main.ts"
第二种使用env-cmd
"start":"env-cmd -f .env esno main.ts"
第三种 使用dotenv-cli
"start":"dotenv -e .env esno main.ts"
一些好用的cli
全局安装的工具
- taze 更新你的依赖
- concurrently Run commands concurrently. Like
npm run watch-js & npm run watch-less
but better. - npm-check&npm-check-updates
- zx
- npm-home
- fkill-cli Fabulously kill processes. Cross-platform.
- gulp
- tiged 类似gitclone
- grunt
- iroiro 显示一些推荐的颜色
- pm2
- nrm
- npkill delete nodemodules
- nitropack
- serve
- local-web-server
- ipx High performance, secure and easy to use image proxy
- docute 一个文档生成器
打包工具
- webpack
- Vite
- pm2
- forever
- yo (yeoman)
- Parcel
- tsup
- unbuild
- nativefier
- esno
- nodemon
- ts-node
- nexe 和pkg类似
- pkg 把nodejs打包为一个单文件,不需要安装nodejs就可以执行
提示
首次使用会出现下载基础包时间过长的问题,如:
> Targets not specified. Assuming:
node14-linux-x64, node14-macos-x64, node14-win-x64
> Fetching base Node.js binaries to PKG_CACHE_PATH
fetched-v14.4.0-linux-x64 [ ] 0%
这种情况下可以手动下载,进入页面手动下载对应的基础包: 地址 然后拷贝到全局目录PKG_CACHE_PATH 内,PKG_CACHE_PATH 可以通过设置系统变量来设置:
export PKG_CACHE_PATH=/usr/local/node/pkg_cache_path
运行完毕后把这行命令建议放到 /etc/profile文件尾部实现长久生效。 运行过 pkg命令后,PKG_CACHE_PATH 目录下有个以版本号命名的目录,将刚才下载的文件重命名为所需文件拷贝到这个目录即可。例如上面命令提示的文件名是 fetched-v16.15.0-win-x64 ,则拷贝时重命名为该名称。 再次运行pkg命令就不会下载了,打包速度很快!
esm注意事项
注意chalk版本必须是4 https://github.com/raineorshine/npm-check-updates/blob/main/package.json 使用esmpackage https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
#查看依赖树
npm ls --depth=1
#查看glob这个包被谁引用了
npm explain glob
原因https://github.com/chalk/chalk/releases/tag/v5.0.0
替代旧的语法
旧的
var re = /([0-9])([0-9])([0-9])/;
re.test("345");
var three = RegExp.$1;
var four = RegExp.$2;
var five = RegExp.$3;
新的
var re = /([0-9])([0-9])([0-9])/;
var [, three, four, five] = "345".match(re);
编译为esm的语法见https://github.com/antfu/taze
unbuild
import { defineBuildConfig } from "unbuild";
export default defineBuildConfig({
entries: ["src/"],//也可以不写,会打包为一个单文件
rollup: {
inlineDependencies: true,
},
clean: true,
declaration: true,
});
关于不生成.mjs文件名的问题
见issuehttps://github.com/unjs/unbuild/issues/83
需要在.prettierrc写下"printWidth": 1000
{
"htmlWhitespaceSensitivity": "ignore",
"useTabs": false,
"endOfLine": "auto",
"proseWrap": "preserve",
"quoteProps": "as-needed",
"embeddedLanguageFormatting": "off",
"insertPragma": false,
"printWidth": 1000
}
node 开发命令行
node link
npm link 是将当前目录包链接到全局 npm remove -g pka-name 是将全局的链接的 pkg-name 包移除 npm unlink pka-name 是将当前项目中软链接的包移除
删除全局包
npm rm -g <name>
对于pnpm
要先设置 pnpm_home
然后安装全局包
pnpm link -g
卸载的话
pnpm un -g