CodeRoadMap
首页路线题库资源库文章社区
CodeRoadMap

程序员的学习成长路线图。内容免费开放,登录后可同步路线图与课时进度。

由 程序员柏风 独立维护 · 关于作者

学习

  • 路线图
  • 课程
  • 面试题
  • 文章
  • 我

更多

  • 资源
  • 社区
  • 关于作者
  • 内容说明

© 2026 CodeRoadMap

津ICP备2026012044号-1·津公网安备12011002026155号·coderoadmap@126.com
Node.js 进阶/课时/命令行应用
RawBlame
课时目录12 节

Node.js 进阶

  1. 01Node 进阶概览
  2. 02EventEmitter
  3. 03错误类型与处理
  4. 04语义化版本
  5. 05创建与发布包
  6. 06命令行应用
  7. 07Inquirer 与 prompts
  8. 08chokidar 与热重载
  9. 09模板引擎
  10. 10Node 与数据库
  11. 11glob 与文件模式
  12. 12综合练习

命令行应用

command-line-apps、stdin/stdout。

35 分钟

命令行应用

Node 擅长 一次性脚本与内部 CLI:读参数、处理 stdin、写 stdout/stderr、返回退出码。本课对应 roadmap command-line-apps——在 node-runtime 的 process 基础上做完整工具。

学习目标

  • 解析 argv 与子命令结构
  • 处理 stdin/stdout 管道
  • 包装 async main 与错误退出(衔接 nva-errors)

参数解析

轻量:手写 process.argv 或使用 Node 20+ 内置 parseArgs:

import { parseArgs } from "node:util";

const { values, positionals } = parseArgs({
  options: {
    dryRun: { type: "boolean", short: "n" },
    file: { type: "string", short: "f" },
  },
  allowPositionals: true,
});

复杂子命令(import run、import validate)可引入 commander 或 yargs——nva-practice 会用到。

stdin / stdout 管道

import { createInterface } from "node:readline";

const rl = createInterface({ input: process.stdin });
for await (const line of rl) {
  process.stdout.write(line.toUpperCase() + "\n");
}

Unix 哲学:CLI 只做一件事,用管道组合——例如 cat ids.txt | my-import --dry-run。

标准流与日志

流用途
stdout正常输出(可被管道消费)
stderr日志、警告、错误 message
exit codeCI 判断成败

用户-facing 进度条写 stderr,结构化结果写 stdout,便于 my-tool > out.json。

main 包装模板

export async function main(argv = process.argv.slice(2)) {
  // ...
}

if (import.meta.url === `file://${process.argv[1]}`) {
  main().catch((err) => {
    console.error(err.message);
    process.exitCode = 1;
  });
}

可测试:测试里 main(['--file', 'x.json']) 不断言 process.exit(见 test-intro 单元测试分层)。

与 Spring Boot 对照

Node CLI
Spring Boot
启动毫秒级进程JVM + 容器
适用批处理、迁移、生成长驻 API
配置env、flagapplication.yml

数据导入脚本常用 Node CLI + postgresql-essentials 驱动,API 仍走 Nest。

练习

  • 实现 echo-lines:读 stdin 行数并 stderr 打印计数
  • --help 输出用法(哪怕手写字符串)
  • 为 main 写一个不 spawn 进程的单元测试

小结

argv 解析、stdin 管道、stdout 结构化、stderr 说人话。 下一课 Inquirer 与 prompts 做交互式输入。

加载本节练习…
← 上一课创建与发布包下一课 →Inquirer 与 prompts