This is the fourth day of my participation in the August More text Challenge. For details, see:August is more challenging
preface
In the root directory of every node project or module, there is usually a package.json file, which defines the module information and version of the project, and the configuration information of the entire project, such as project name, version, description, and so on…
Most developers only know about package.json when:
- Project name, build version, and license Settings
- Optimize running scripts in script
- Project dependency record
Json is just the tip of the iceberg. There are many more properties that can be configured to achieve much more functionality. We’ll take a closer look at package.json.
Simple package. Json
Let’s initialize a node module with the NPM init command and look at its package.json file. (Package. json files can be hand-written)
{# Project name"name": "island", # project version"version": "1.0.0", # Project description"description": "", # entry file"main": "index.js", # script"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"#}, the author"author": "QC2168", # license"license": "ISC"
}
Copy the code
Attributes that
Essential attribute
There are a number of properties that can be configured in package.json. The name and version properties are essential, and they are the unique identification of the NPM module.
name
Sets the name of the module, which is used to tell the name of the module.
- Naming rules
- The name contains a maximum of 214 characters and cannot contain Spaces. The name can contain only lowercase letters and hyphens (
-
) or underlined (_
). - You can’t start with the point (
.
) or underlined (_
) at the beginning. - The name must not contain uppercase letters.
- Only URL security characters must be used.
- The name contains a maximum of 214 characters and cannot contain Spaces. The name can contain only lowercase letters and hyphens (
{
"name": "island"
}
Copy the code
version
Set your module version number. This property follows the version’s semantic versioning notation, which means that the version is always represented by three digits: X.X.X.
- Version format: Indicates the major version number. Revision number. The increment rule of version number is as follows:
- Major version number: When you make an incompatible API change
- Sub version number: When you make backwards compatible functionality additions
- Revision number: When you make a backwards compatibility issue fix
- Prior version number and version compilation information can be added to “Major version number”. Revision number “, as an extension
For details, see the semantic version 2.0.0: semver.org/lang/zh-CN/
{
"author": "QC2168"
}
Copy the code
About Description
keywords
This attribute is to set keywords in the module. When we use NPM to retrieve the module, we will match description field and keywords field in the module. Writing description and keywords in package.json will help increase the exposure of our module. If you do not intend to publish to NPM, you can ignore this property
{
"keywords": ["server"."osiolabs"."express"."compression"]}Copy the code
description
This attribute specifies the description of the module, which helps users understand the module. At the same time, the package manager will also use this value as the search keyword
{
"description": "Thank you for reading. Move your hand."
}
Copy the code
About the agreement
license
This property sets the license of the module and lets the user know under what license they are using the package and what other restrictions there are. For example, MIT Open Source, a very loose agreement that basically allows you to do anything, as long as you keep the author’s copyright.
{
"license": "MIT"
}
Copy the code
engines
This property tells the developer which version of Node.js or other commands the project/module is running.
"engines": {
"node": "> = 6.0.0"."npm": "> = 3.0.0"."yarn": "^ 0.13.0"
}
Copy the code
About maintenance/development
author
Set the author name of the module, which you can fill in as a string/object. Fill in as an object to fill in more details about the author.
{
"author": "QC2168"
}
or
{
"name": "QC2168"."GitHub": "https://github.com/QC2168"
}
Copy the code
contributors
You can also configure other contributors information in your modules. That’s an array-type property that lets you store contributor information as strings/objects.
{
"contributors": ["QC2168"]
}
or
{
"contributors": [{"name": "QC2168"."GitHub": "https://github.com/QC2168"}}]Copy the code
About the program
main
This property sets the module entry location where the application searches for module exports when a project imports a module.
This is usually the index.js file in the project root directory
{
"main": "src/index.js"
}
Copy the code
About Publishing Configuration
private
This property sets whether or not the module can be published to NPM. We usually set this property to true for non-open source projects to prevent accidental release of the module.
{
"private": "true"
}
Copy the code
files
This property is used when we publish NPM Publish only the directory or file (in this case, only files in the dist directory).
"files": [
"dist/"].Copy the code
About scripting Tasks
scripts
Define a set of NPM scripts. Each time you run NPM run, a new shell is created and the specified script commands are executed inside.
"scripts": {
"watch": "webpack --watch"."build": "webpack --config webpack.build.config.ts"."start": "webpack serve --config webpack.dev.config.ts"
},
Copy the code
config
This property is used to configure environment variables used in scripts. It can be used in script commands, for example, process.env.npm_package_config_port.
{
"config" : { "port" : "8080"}}Copy the code
About dependency Packages
dependencies
Sets the list of NPM modules installed as dependencies.
NPM install < module name > yarn add < module name >Copy the code
When you install software modules using NPM/yarn, the module name is automatically inserted into the package.json dependencies attribute.
"dependencies": {
"react": "^ 17.0.0"."react-dom": "^ 17.0.0"
},
Copy the code
devDependencies
Sets the list of NPM packages installed as development dependencies.
They differ from dependencies in that they only need to be installed on the development machine and do not need to run the code in production.
NPM install --save-dev < module name > yarn add --dev < module name >Copy the code
When you install software modules using NPM/yarn, the module name is automatically inserted into the devDependencies attribute of package.json.
"devDependencies": {
"webpack": "^ 5.38.1"."webpack-cli": "^ 4.7.0"."webpack-dev-server": "^ 3.11.2"
}
Copy the code
For more information
repository
This property refers to where the module code is managed. This is helpful for users who want to understand the source code and contribute. “, usually with the GitHub repository address
{
"repository": {
"type": "git"."url": "https://github.com/QC2168/webpack-template.git"}}Copy the code
bugs
This property fills in the module’s problem report page, which is a great way for people who are experiencing problems to come back to you with feedback.
"bugs": {
"url": "https://github.com/QC2168/webpack-template/issues"
},
Copy the code
homepage
This property fills in the module’s home page/document so that users can get a better idea of how to use your module.
{
"homepage": "https://github.com/QC2168/webpack-template#readme",}Copy the code
conclusion
This article has introduced most of the properties and functions in package.json and categorized them into categories. At the same time, package.json is the core file of a Node project. It records important information about the entire Node project, and is also an essential file in a Node project.