JSON 配置文件在现代开发中无处不在。了解它们的模式有助于更有效地使用技术栈中的工具。
常见 JSON 配置文件
每个 JavaScript 项目都使用多个 JSON 配置文件:
package.json — 项目元数据和依赖:
{
"name": "my-project",
"version": "1.0.0",
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"dependencies": {
"next": "16.0.0",
"react": "19.0.0"
}
}
tsconfig.json — TypeScript 配置:
{
"compilerOptions": {
"target": "ES2022",
"module": "ESNext",
"strict": true,
"jsx": "preserve"
},
"include": ["src/**/*"]
}
JSON 配置与其他格式对比
| 格式 | 优点 | 缺点 |
|------|------|------|
| JSON | 通用、解析快 | 无注释、严格语法 |
| YAML | 支持注释、可读 | 缩进容易出错 |
| TOML | 简单、清晰 | 不太常见 |
| .env | 简单键值 | 不支持嵌套 |
JSON 配置技巧
$schema 获得 IDE 自动补全添加 $schema 获取自动补全
{
"$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": {
"target": "ES2022"
}
}