{ "name": "dangan", "version": "1.0.0", "description": "", "main": "main.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "electron main.js", "pack": "electron-builder --dir", "dist": "node script/replace-config.js && electron-builder" }, "author": "", "license": "ISC", "devDependencies": { "electron": "^23.2.1", "electron-builder": "^24.13.3" }, "build": { "electronDownload": { "mirror": "https://npmmirror.com/mirrors/electron/" }, "appId": "com.example.websiteapp", "productName": "档案管理222", "win": { "icon": "favicon1.png" }, "extraResources": [ { "from": "config.json", "to": "." } ] }, "dependencies": { "dangan": "file:", "electron-is-dev": "^3.0.1" } }
时间: 2025-03-20 16:10:10 浏览: 41
### Electron 中 `package.json` 的配置说明
#### 1. 基本字段解释
在 Electron 应用中,`package.json` 是项目的核心配置文件之一。以下是常见的字段及其作用:
- **name**: 定义项目的名称,通常用于标识应用[^1]。
- **version**: 表示当前版本号,遵循语义化版本控制(SemVer),如 `major.minor.patch` 格式。
- **description**: 描述项目的用途或功能。
- **main**: 指定应用程序的入口文件,默认为 `index.js` 或 `main.js`。此字段定义了启动时加载的第一个脚本。
```json
{
"name": "my-electron-app",
"version": "1.0.0",
"description": "A simple Electron application.",
"main": "main.js"
}
```
---
#### 2. 脚本字段 (`scripts`)
通过 `scripts` 字段可以定义常用的命令快捷方式。例如:
- **start**: 启动开发环境下的应用。
- **dev**: 使用调试模式运行应用。
- **build**/**pack**/**dist**: 打包和分发应用。
具体实现如下所示[^2][^3]:
```json
"scripts": {
"start": "electron .",
"dev": "electron . --debug",
"pack": "electron-builder --dir",
"dist": "electron-builder"
}
```
执行这些脚本的方式是通过 `npm run <script-name>` 来调用对应的命令。
---
#### 3. 开发依赖项 (`devDependencies`) 和生产依赖项 (`dependencies`)
- **devDependencies**: 列出了仅在开发环境中使用的库,比如 `electron` 自身作为开发依赖项被安装。
- **dependencies**: 生产环境下所需的第三方模块列表。
示例配置如下:
```json
"devDependencies": {
"electron": "^27.0.0",
"electron-builder": "^24.0.0"
},
"dependencies": {}
```
---
#### 4. 构建工具配置 (`build`)
如果使用 `electron-builder` 进行打包,则可以在 `build` 字段中指定详细的构建选项[^4]。
常见子字段包括但不限于以下几个方面:
- **appId**: 设置应用 ID,在 Windows 上对应于注册表中的唯一键名。
- **productName**: 显示的应用程序名称。
- **directories**: 定义资源目录结构。
- **files**: 控制哪些文件会被包含到最终的 `.asar` 文件中。
- **win/mac/linux**: 平台特定的设置。
完整的 `build` 配置可能看起来像这样:
```json
"build": {
"appId": "com.example.myapp",
"productName": "MyApp",
"directories": {
"output": "out"
},
"files": [
"package.json",
"src/**/*",
"!node_modules"
],
"mac": {
"icon": "assets/icons/icon.icns"
},
"win": {
"target": ["nsis"]
}
}
```
---
#### 5. 其他重要字段
- **repository**: 提供源码托管地址的信息,便于开发者追踪代码仓库位置[^5]。
```json
"repository": {
"type": "git",
"url": "https://github.com/example/my-electron-repo.git"
}
```
- **keywords**: 关键字帮助其他人在 NPM 注册表上更容易找到你的项目。
- **author**: 记录作者信息。
- **license**: 指明软件许可协议类型。
---
### 示例完整配置
以下是一个典型的 `package.json` 文件内容展示:
```json
{
"name": "example-electron-app",
"version": "1.0.0",
"description": "An example of an Electron-based desktop app.",
"main": "main.js",
"scripts": {
"start": "electron .",
"dev": "electron . --debug",
"pack": "electron-builder --dir",
"dist": "electron-builder"
},
"build": {
"appId": "com.example.electronapp",
"productName": "Example App",
"directories": {
"output": "release-builds"
},
"files": [
"**/*",
"!test"
]
},
"repository": {
"type": "git",
"url": "https://github.com/example/repo.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"electron": "^27.0.0",
"electron-builder": "^24.0.0"
}
}
```
---
阅读全文
相关推荐


















