template-ts-node
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTypeScript Node.js Project Setup
TypeScript Node.js 项目搭建
Scaffold production-ready TypeScript Node.js projects with modern tooling and best practices. Default configuration targets single-repository, non-UI projects (libraries, CLI tools, backend services). Uses Bun for package management and Vitest for testing.
使用现代化工具和最佳实践搭建可用于生产环境的TypeScript Node.js项目。默认配置针对单仓库、非UI项目(库、CLI工具、后端服务),采用Bun进行包管理,Vitest进行测试。
Quick Reference: Configuration Files
快速参考:配置文件
| File | Purpose |
|---|---|
| Project metadata, dependencies, and scripts |
| TypeScript compiler configuration |
| Linting and formatting rules |
| Task automation and project commands |
| Test framework configuration |
| Version control exclusions |
| Git line-ending and merge behavior |
| Git hooks for pre-commit automation |
| Dependency lock file (Bun) |
| 文件名称 | 用途 |
|---|---|
| 项目元数据、依赖项和脚本 |
| TypeScript编译器配置 |
| 代码检查与格式化规则 |
| 任务自动化与项目命令 |
| 测试框架配置 |
| 版本控制忽略文件配置 |
| Git行尾格式与合并行为配置 |
| 用于提交前自动化的Git钩子 |
| Bun的依赖锁定文件 |
Setup Workflow
搭建流程
1. Initialize Project Structure
1. 初始化项目结构
Create the project directory and initialize version control.
bash
mkdir project-name
cd project-name
git init创建项目目录并初始化版本控制。
bash
mkdir project-name
cd project-name
git init2. Copy Configuration Files
2. 复制配置文件
Copy all configuration files from directory to the project root. These files are pre-configured to extend and follow established patterns.
resources/@sablier/devkitRequired files:
- - Adapt name, description, version, and author fields
package.json - - Extends
tsconfig.json@sablier/devkit/tsconfig/base.json - - Extends
biome.jsoncandultracite/core@sablier/devkit/biome/base - - Imports
justfile@sablier/devkit/just/base.just - - Test configuration
vitest.config.ts - - Standard Node.js exclusions
.gitignore - - Line endings and diff behavior
.gitattributes
将目录下的所有配置文件复制到项目根目录。这些文件已预配置为继承并遵循既定规范。
resources/@sablier/devkit必填文件:
- - 调整名称、描述、版本和作者字段
package.json - - 继承
tsconfig.json@sablier/devkit/tsconfig/base.json - - 继承
biome.jsonc和ultracite/core@sablier/devkit/biome/base - - 导入
justfile@sablier/devkit/just/base.just - - 测试框架配置
vitest.config.ts - - 标准Node.js忽略规则
.gitignore - - 行尾格式与差异对比行为配置
.gitattributes
3. Install Dependencies
3. 安装依赖项
Use Bun to install project dependencies.
bash
bun installCore dependencies:
- (from GitHub:
@sablier/devkit)github:sablier-labs/devkit typescript- (testing)
vitest - (linting/formatting)
@biomejs/biome - (git hooks)
husky - (task runner, if not globally installed)
just
使用Bun安装项目依赖。
bash
bun install核心依赖:
- (GitHub地址:
@sablier/devkit)github:sablier-labs/devkit typescript- (测试工具)
vitest - (代码检查/格式化工具)
@biomejs/biome - (Git钩子工具)
husky - (任务运行器,若未全局安装)
just
4. Initialize Git Hooks
4. 初始化Git钩子
Set up Husky for pre-commit automation.
bash
bun run prepareThis installs git hooks that run linting and formatting checks before commits.
设置Husky以实现提交前自动化检查。
bash
bun run prepare此命令会安装Git钩子,在提交前自动运行代码检查和格式化。
5. Create Source Structure
5. 创建源码结构
Establish the core source directory and entry point.
bash
mkdir src
touch src/index.tsAdd initial content to :
src/index.tstypescript
// -------------------------------------------------------------------------- //
// EXPORTS //
// -------------------------------------------------------------------------- //
export const VERSION = "0.1.0";建立核心源码目录和入口文件。
bash
mkdir src
touch src/index.ts在中添加初始内容:
src/index.tstypescript
// -------------------------------------------------------------------------- //
// EXPORTS //
// -------------------------------------------------------------------------- //
export const VERSION = "0.1.0";6. Verify Setup
6. 验证搭建结果
Run full project checks to ensure configuration is correct.
bash
just full-checkThis executes TypeScript compilation, linting, formatting checks, and tests.
运行完整项目检查以确保配置正确。
bash
just full-check此命令会执行TypeScript编译、代码检查、格式化验证和测试。
Key Configuration Choices
关键配置选择
Shared Configuration via @sablier/devkit
通过@sablier/devkit共享配置
All configuration files extend the shared devkit to maintain consistency across projects. This approach centralizes common patterns and reduces per-project configuration burden.
GitHub location:
github:sablier-labs/devkit所有配置文件都继承自共享的devkit,以保持项目间的一致性。这种方式集中了通用规范,减少了每个项目的配置工作量。
GitHub地址:
github:sablier-labs/devkitTypeScript Configuration
TypeScript配置
Extend the base TypeScript configuration from devkit. Customize only when project-specific requirements demand it.
compilerOptionsExample :
tsconfig.jsonjson
{
"extends": "@sablier/devkit/tsconfig/base.json",
"compilerOptions": {
"outDir": "./dist",
"rootDir": "./src"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}Key settings (inherited from devkit):
- - Maximum type safety
strict: true - - CommonJS/ESM compatibility
esModuleInterop: true - - Faster compilation
skipLibCheck: true - - Modern module system
module: "ESNext" - - Recent language features
target: "ES2022"
继承devkit中的基础TypeScript配置。仅当项目有特定需求时,才自定义。
compilerOptions示例:
tsconfig.jsonjson
{
"extends": "@sablier/devkit/tsconfig/base.json",
"compilerOptions": {
"outDir": "./dist",
"rootDir": "./src"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}从devkit继承的关键设置:
- - 最高级别类型安全
strict: true - - CommonJS/ESM兼容性
esModuleInterop: true - - 更快的编译速度
skipLibCheck: true - - 现代化模块系统
module: "ESNext" - - 支持最新语言特性
target: "ES2022"
Biome Configuration
Biome配置
Extend both Ultracite core rules and Sablier devkit overrides. This provides opinionated linting and formatting with project-specific adjustments.
Example :
biome.jsoncjsonc
{
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
"extends": ["ultracite/core", "@sablier/devkit/biome/base"],
"formatter": {
"indentStyle": "space",
"lineWidth": 120
}
}Inherited behaviors:
- Sorted imports and exports
- Consistent naming conventions
- No unused variables
- Explicit function return types
同时继承Ultracite核心规则和Sablier devkit的覆盖规则。这提供了带有项目特定调整的代码检查和格式化规范。
示例:
biome.jsoncjsonc
{
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
"extends": ["ultracite/core", "@sablier/devkit/biome/base"],
"formatter": {
"indentStyle": "space",
"lineWidth": 120
}
}继承的行为:
- 导入和导出语句排序
- 一致的命名规范
- 禁止未使用的变量
- 显式函数返回类型
Just Task Runner
Just任务运行器
Import base recipes from devkit and add only project-specific tasks. The devkit provides common recipes—do not duplicate them.
Example :
justfilejust
import "./node_modules/@sablier/devkit/just/base.just"从devkit导入基础任务,仅添加项目特定的任务。devkit已提供通用任务,请勿重复定义。
示例:
justfilejust
import "./node_modules/@sablier/devkit/just/base.just"Default recipe
默认任务
default:
@just --list
default:
@just --list
Project-specific recipes only
仅添加项目特定任务
[group("app")]
build:
na tsc
**For advanced Just CLI usage** (modules, attributes, inline scripts, conditional execution), consult the `just-cli` skill which provides comprehensive guidance on Just's advanced features.[group("app")]
build:
na tsc
**关于Just CLI的高级用法**(模块、属性、内联脚本、条件执行),请参考`just-cli`技能,该技能提供了Just高级功能的全面指导。Vitest Configuration
Vitest配置
Configure test framework with TypeScript support and coverage reporting.
Example :
vitest.config.tstypescript
import { defineConfig } from "vitest/config";
export default defineConfig({
test: {
globals: true,
environment: "node",
coverage: {
provider: "v8",
reporter: ["text", "json", "html"],
},
},
});配置支持TypeScript和覆盖率报告的测试框架。
示例:
vitest.config.tstypescript
import { defineConfig } from "vitest/config";
export default defineConfig({
test: {
globals: true,
environment: "node",
coverage: {
provider: "v8",
reporter: ["text", "json", "html"],
},
},
});Common Just Recipes
常用Just任务
Execute these commands from the project root using the command.
just在项目根目录使用命令执行以下任务。
justRecipes from @sablier/devkit
来自@sablier/devkit的任务
The devkit provides these recipes—do not redefine them:
| Recipe | Alias | Description |
|---|---|---|
| | Run all code checks (biome + prettier + types) |
| | Run all code fixes |
| | Run Biome linting and formatting checks |
| | Apply Biome fixes |
| | Run Biome linter only |
| | TypeScript type checking (tsgo/tsc) |
| | Build with TypeScript |
| | Check Prettier formatting |
| | Apply Prettier formatting |
| | Check for unused exports/dependencies |
| | Fix unused exports/dependencies |
| Clean .DS_Store files | |
| Remove node_modules recursively | |
| Install dependencies with ni |
devkit已提供这些任务,请勿重新定义:
| 任务名称 | 别名 | 描述 |
|---|---|---|
| | 运行所有代码检查(biome + prettier + 类型检查) |
| | 运行所有代码修复 |
| | 运行Biome代码检查和格式化验证 |
| | 应用Biome修复 |
| | 仅运行Biome代码检查 |
| | TypeScript类型检查(tsgo/tsc) |
| | 使用TypeScript编译 |
| | 检查Prettier格式化 |
| | 应用Prettier格式化 |
| | 检查未使用的导出/依赖项 |
| | 修复未使用的导出/依赖项 |
| 清理.DS_Store文件 | |
| 递归删除node_modules | |
| 使用ni安装依赖项 |
Project-Specific Recipes
项目特定任务
Define only what the devkit doesn't provide:
bash
just build # Compile TypeScript (project entry point)
just dev # Start development mode with file watching
just test # Run tests with Vitest
just test-ui # Run tests with Vitest UI仅定义devkit未提供的任务:
bash
just build # 编译TypeScript(项目入口)
just dev # 启动开发模式并监听文件变化
just test # 使用Vitest运行测试
just test-ui # 使用Vitest UI运行测试Package.json Scripts
Package.json脚本
Leave empty besides the husky setup. The is used to run commands.
justfileEssential scripts:
json
{
"scripts": {
"prepare": "husky install"
}
}除了husky设置外,其余留空。使用运行命令。
justfile必要脚本:
json
{
"scripts": {
"prepare": "husky install"
}
}Directory Structure
目录结构
Organize project files following these conventions:
project-name/
├── src/
│ ├── index.ts # Main entry point
│ ├── lib/ # Library code
│ ├── utils/ # Utility functions
│ └── types/ # Type definitions
├── test/
│ └── index.test.ts # Test files
├── dist/ # Compiled output (gitignored)
├── coverage/ # Test coverage reports (gitignored)
├── node_modules/ # Dependencies (gitignored)
├── package.json
├── tsconfig.json
├── biome.jsonc
├── justfile
├── vitest.config.ts
├── .gitignore
├── .gitattributes
├── .husky/
│ └── pre-commit
├── bun.lockb
└── README.md按照以下规范组织项目文件:
project-name/
├── src/
│ ├── index.ts # 主入口文件
│ ├── lib/ # 库代码
│ ├── utils/ # 工具函数
│ └── types/ # 类型定义
├── test/
│ └── index.test.ts # 测试文件
├── dist/ # 编译输出(已加入git忽略)
├── coverage/ # 测试覆盖率报告(已加入git忽略)
├── node_modules/ # 依赖项(已加入git忽略)
├── package.json
├── tsconfig.json
├── biome.jsonc
├── justfile
├── vitest.config.ts
├── .gitignore
├── .gitattributes
├── .husky/
│ └── pre-commit
├── bun.lockb
└── README.mdResource Files
资源文件
Copy these pre-configured files from directory to bootstrap new projects:
resources/| File | Purpose |
|---|---|
| Base package.json with devDependencies (adapt name, description) |
| TypeScript configuration extending devkit |
| Linting and formatting rules |
| Project-specific recipes only (imports devkit base) |
| Test framework setup for single repos |
| Shared test config for monorepos |
| Prettier config extending devkit |
| Prettier ignore patterns |
| Pre-commit hook configuration |
| Standard Node.js exclusions |
Usage: Copy files to project root, then adapt fields (name, description, author, version).
package.jsonImportant: The imports which provides common recipes. Only add project-specific recipes that aren't in the devkit.
justfile@sablier/devkit/just/base.just从目录复制以下预配置文件,以快速启动新项目:
resources/| 文件名称 | 用途 |
|---|---|
| 包含开发依赖的基础package.json(需调整名称、描述等字段) |
| 继承devkit的TypeScript配置 |
| 代码检查和格式化规则 |
| 仅包含项目特定任务(导入devkit基础任务) |
| 单仓库项目的测试框架设置 |
| 单仓库项目的共享测试配置 |
| 继承devkit的Prettier配置 |
| Prettier忽略规则 |
| 提交前钩子配置 |
| 标准Node.js忽略规则 |
使用方法: 将文件复制到项目根目录,然后调整的字段(名称、描述、作者、版本)。
package.json注意: 会导入,该文件提供了通用任务。仅添加devkit未包含的项目特定任务。
justfile@sablier/devkit/just/base.justBest Practices
最佳实践
Dependency Management
依赖管理
Pin major versions but allow minor and patch updates:
json
{
"dependencies": {
"@sablier/devkit": "github:sablier-labs/devkit",
"zod": "^3.22.0"
}
}Separate dev and production dependencies clearly:
- Runtime code →
dependencies - Build tools, linters, test frameworks →
devDependencies
固定主版本号,但允许小版本和补丁版本更新:
json
{
"dependencies": {
"@sablier/devkit": "github:sablier-labs/devkit",
"zod": "^3.22.0"
}
}明确区分开发依赖和生产依赖:
- 运行时代码 →
dependencies - 构建工具、代码检查工具、测试框架 →
devDependencies
TypeScript Configuration
TypeScript配置
Enable strict mode for maximum type safety. Override specific checks only when absolutely necessary.
Use path aliases for cleaner imports:
json
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["src/*"]
}
}
}启用严格模式以获得最高类型安全性。仅在绝对必要时,才覆盖特定检查规则。
使用路径别名以简化导入:
json
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["src/*"]
}
}
}Testing Strategy
测试策略
Co-locate tests with source files or use parallel directory structure.
test/Name test files with or suffix.
.test.ts.spec.tsWrite tests first for complex logic (TDD approach).
Aim for high coverage on critical paths, but don't chase 100% arbitrarily.
将测试文件与源码文件放在一起,或使用平行的目录结构。
test/测试文件命名使用或后缀。
.test.ts.spec.ts复杂逻辑先写测试(TDD方法)。
关键路径追求高覆盖率,但不要为了100%覆盖率而盲目编写测试。
Git Workflow
Git工作流
Commit often with descriptive messages.
Use conventional commits format: , , , , etc.
feat:fix:docs:refactor:Let Husky hooks catch issues before they reach the repository.
Tag releases with semantic versioning: , , etc.
v1.0.0v1.1.0频繁提交,并使用描述性的提交信息。
使用约定式提交格式:、、、等。
feat:fix:docs:refactor:让Husky钩子在问题提交到仓库前捕获它们。
使用语义化版本标记发布:、等。
v1.0.0v1.1.0Code Organization
代码组织
Group by feature rather than by file type when projects grow beyond simple structure.
Export from index files to create clean public APIs:
typescript
// src/lib/index.ts
export { functionA } from "./moduleA.js";
export { functionB } from "./moduleB.js";Use barrel exports sparingly - they can impact tree-shaking.
当项目规模超出简单结构时,按功能分组,而非按文件类型。
从索引文件导出以创建清晰的公共API:
typescript
// src/lib/index.ts
export { functionA } from "./moduleA.js";
export { functionB } from "./moduleB.js";谨慎使用桶式导出——它们可能影响Tree-shaking优化。
Next Steps After Setup
搭建完成后的下一步
- Write initial code in
src/index.ts - Add first test in
test/index.test.ts - Run to verify everything works
just full-check - Create initial commit with all scaffolding
- Set up remote repository and push
- Configure CI/CD using GitHub Actions or similar
- Write README.md with project-specific documentation
- Add LICENSE file appropriate for your use case
- 在中编写初始代码
src/index.ts - 在中添加第一个测试
test/index.test.ts - 运行以验证所有功能正常
just full-check - 提交所有脚手架文件的初始版本
- 设置远程仓库并推送代码
- 使用GitHub Actions或类似工具配置CI/CD
- 编写README.md,添加项目特定文档
- 添加适合项目的LICENSE文件
Reference Documentation
参考文档
For detailed guidance on specific topics, consult these reference files:
- - CLI tools, libraries, and backend services setup
./references/adapting-project-types.md - - Common issues and solutions
./references/troubleshooting.md - - VSCode, GitHub Actions, and pre-commit setup
./references/ide-ci-integration.md - - React/Next.js UI projects
./references/next-ui.md - - Workspace and monorepo configuration
./references/monorepo.md
如需特定主题的详细指导,请参考以下文档:
- - CLI工具、库和后端服务的搭建指南
./references/adapting-project-types.md - - 常见问题与解决方案
./references/troubleshooting.md - - VSCode、GitHub Actions和提交前钩子的设置
./references/ide-ci-integration.md - - React/Next.js UI项目搭建
./references/next-ui.md - - 工作区和单仓库多项目配置
./references/monorepo.md
Related Skills
相关技能
- - Comprehensive Just task runner patterns, modules, and advanced features
just-cli - - TypeScript-specific rules, patterns, and best practices
typescript - - Biome configuration and lint rule customization
biome - - Dependency update strategies with Taze
node-deps
- - Just任务运行器的模式、模块和高级功能的全面指南
just-cli - - TypeScript特定规则、模式和最佳实践
typescript - - Biome配置和检查规则自定义
biome - - 使用Taze进行依赖更新的策略
node-deps
Summary
总结
This skill provides a streamlined path to creating production-ready TypeScript Node.js projects. By leveraging shared configuration from and modern tooling (Bun, Vitest, Biome, Just), new projects achieve consistency and quality from day one. Adapt the base configuration for CLI tools, libraries, or backend services as needed. Consult reference documentation for UI projects or monorepo setups when requirements extend beyond single-repository, non-UI applications.
@sablier/devkit本技能提供了一条搭建可用于生产环境的TypeScript Node.js项目的简化路径。通过使用的共享配置和现代化工具(Bun、Vitest、Biome、Just),新项目从第一天起就能保持一致性和高质量。可根据需要调整基础配置,以适配CLI工具、库或后端服务。当需求超出单仓库、非UI应用时,请参考相关文档以搭建UI项目或单仓库多项目架构。
@sablier/devkit