阿里云主机折上折
  • 微信号
Current Site:Index > Detailed explanation of the package.json file

Detailed explanation of the package.json file

Author:Chuan Chen 阅读数:41263人阅读 分类: Node.js

The package.json is the core configuration file for Node.js projects, defining the project's metadata, dependencies, script commands, and more. It uses JSON format and is typically located in the project's root directory. It serves as the foundation for operations by package managers like npm or yarn.

Basic Structure of package.json

A typical package.json file includes the following main fields:

{
  "name": "my-project",
  "version": "1.0.0",
  "description": "A sample Node.js project",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "dependencies": {
    "express": "^4.17.1"
  },
  "devDependencies": {
    "eslint": "^7.32.0"
  }
}

Detailed Explanation of Core Fields

name and version

name and version are required fields that together form the unique identifier for the project.

  • Rules for name:
    • Length ≤ 214 characters
    • Cannot start with a dot or underscore
    • Cannot contain uppercase letters
    • Cannot conflict with official package names
{
  "name": "my-awesome-project",
  "version": "1.0.0-alpha.1"
}

Version numbers follow Semantic Versioning (SemVer):

  • MAJOR.MINOR.PATCH (Major.Minor.Patch)
  • Pre-release versions can include suffixes like -alpha or -beta

description and keywords

These fields improve package discoverability:

{
  "description": "A lightweight utility library for data validation",
  "keywords": ["validation", "utility", "javascript"]
}

main and module

Define the entry files for the package:

{
  "main": "lib/index.js",        // CommonJS entry
  "module": "esm/index.js"       // ES Module entry
}

scripts

Define executable npm scripts:

{
  "scripts": {
    "start": "node server.js",
    "test": "jest",
    "build": "webpack --mode production",
    "lint": "eslint .",
    "prepublishOnly": "npm test && npm run build"
  }
}

Common script hooks:

  • pre/post prefixes can create pre/post scripts
  • Lifecycle hooks like prepublish, prepare, and prepublishOnly

Dependency Management

dependencies and devDependencies

{
  "dependencies": {
    "react": "^17.0.2",
    "lodash": "~4.17.21"
  },
  "devDependencies": {
    "webpack": "^5.0.0",
    "@types/node": "14.14.37"
  }
}

Version prefix meanings:

  • ^: Allows minor and patch updates
  • ~: Allows only patch updates
  • No prefix: Exact version

peerDependencies

Declare dependencies that the host environment must provide:

{
  "peerDependencies": {
    "react": ">=16.8.0"
  }
}

bundledDependencies

Specify dependencies to be bundled in the published package:

{
  "bundledDependencies": ["package-a", "package-b"]
}

Advanced Configuration

engines

Specify runtime environment requirements:

{
  "engines": {
    "node": ">=12.0.0",
    "npm": ">=6.0.0"
  }
}

os and cpu

Restrict the operating platforms:

{
  "os": ["darwin", "linux"],
  "cpu": ["x64", "arm64"]
}

files

Control which files are included in the published package:

{
  "files": [
    "dist/",
    "lib/",
    "README.md"
  ]
}

repository

Specify the code repository information:

{
  "repository": {
    "type": "git",
    "url": "https://github.com/user/repo.git"
  }
}

Private Package Configuration

private

Prevent accidental publishing:

{
  "private": true
}

publishConfig

Customize publishing settings:

{
  "publishConfig": {
    "registry": "https://registry.npmjs.org/",
    "access": "public"
  }
}

Workspace Configuration

workspaces

Monorepo project configuration:

{
  "workspaces": [
    "packages/*",
    "shared/*"
  ]
}

Custom Fields

Add project-specific configurations:

{
  "config": {
    "port": 8080
  },
  "myCustomConfig": {
    "featureFlags": {
      "newUI": true
    }
  }
}

Complete Example

Comprehensive example:

{
  "name": "full-featured-project",
  "version": "1.2.3",
  "description": "A comprehensive example package.json",
  "main": "dist/index.js",
  "module": "dist/index.esm.js",
  "types": "dist/index.d.ts",
  "files": ["dist"],
  "scripts": {
    "build": "rollup -c",
    "test": "jest",
    "lint": "eslint src",
    "prepublishOnly": "npm run build && npm test"
  },
  "dependencies": {
    "axios": "^0.21.1"
  },
  "devDependencies": {
    "@types/jest": "^26.0.20",
    "jest": "^26.6.3"
  },
  "peerDependencies": {
    "react": ">=16.8.0"
  },
  "engines": {
    "node": ">=12.0.0"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/user/repo.git"
  },
  "keywords": ["example", "demo"],
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/user/repo/issues"
  },
  "homepage": "https://github.com/user/repo#readme",
  "config": {
    "port": 3000
  }
}

本站部分内容来自互联网,一切版权均归源网站或源作者所有。

如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn

上一篇:包与NPM的基本概念

下一篇:NPM常用命令

Front End Chuan

Front End Chuan, Chen Chuan's Code Teahouse 🍵, specializing in exorcising all kinds of stubborn bugs 💻. Daily serving baldness-warning-level development insights 🛠️, with a bonus of one-liners that'll make you laugh for ten years 🐟. Occasionally drops pixel-perfect romance brewed in a coffee cup ☕.