Do you understand every field in package.json
Do you really understand package.json? Take a look, this is probably the most complete package parsing
Companies often say that they want someone with project experience, but that experience is just about code and function module development. Understanding package.json files in depth can make you look impressive, too.
By default, there is a package.json file at the root of each project, which defines the modules and dependencies required by the current project and also contains configuration information such as name, version, license, etc.
When we get a project and run NPM install, the required modules are automatically downloaded according to the configuration in package.json, generating node_modules (the folder used to store the packages downloaded and installed with the package management tool).
What is package.json?
The package.json file is a JSON object, and each member of the object is a setting for the current project, for example, name is the project name and version is the project version number.
{"name": "vue-project", "version": "0.1.0",}Copy the code
The above code defines the project name and version number
Package.json is an object that contains several properties. In enterprise projects, we pay more attention to devDependencies and Dependencies fields, which are the modules that the project runs and the development environment depends on
What about the other fields? What’s the significance?
2. Detailed analysis of configuration fields
1, the name,
The most important fields in package.json are the name and Version fields, which are mandatory. Together, the name and version form an identifier that is considered completely unique. Changes to packages should be made in conjunction with changes to versions.
Name must be a string. Or _, cannot have uppercase letters, because the name ends up being part of the URL and therefore cannot contain any non-URL-safe characters. NPM officially advises against using the same name as the core node module. Do not include JS or node in the name. Use Engines to specify the running environment if desired.
The name is passed to require as an argument, so it should be short, but it also needs to be reasonably descriptive.
2, version
Version The project Version number is in the X.X.X format. It complies with the semantic Version rules. Minor version. Format of minor version), a new and unique version number should be submitted each time the project is released
3, the description
Description is a string for users to search for our project on NPM.
"description": "vue-elementui",
Copy the code
4, keywords
Keywords are an array of strings that allow users to search for our items on NPM
"keywords":["node.js","vue", "element"],
Copy the code
5, homepage
Homepage Specifies the home address of the homepage project.
"homepage": "https://github.com/owner"
Copy the code
6, bugs
Url to report project problems or email address to report problems.
"bugs":{"url":"https://github.com/owner","email":"[email protected]"},
Copy the code
If you just set urls, bugs can use strings instead of objects.
"bugs":"https://github.com/owner",
Copy the code
7, license
A project license that lets users know how to use the project, what permissions they have to use our module, and what restrictions they have on using the module
"license" : "BSD-3-Clause"
Copy the code
8, the author, contributors
Author represents a Person object
That’s an array of persons that represent a bunch of persons
Includes authors and contributors (our module developers and many contributors)
An object contains name, URL, and email
"author": {
"name": "fangfang",
"email": "[email protected]",
"url": " http://www.itiedu.cn"
}
Copy the code
"contributors":[{"name":" fangfang","email":" [email protected] ","url": " http://www.itiedu.cn }]
Copy the code
9 files.
Files is an array of files that describes the entries to include when installing a package as a dependency. If you declare a folder in an array, it will also contain the files in that folder. Some special files and directories are also included or excluded, whether or not they exist in the file array.
In short, download the files contained in the dependency package
10, the main
The main file, which is also the entry file for the project, defaults to index.js in the project root directory.
"main": "./index.js",
Copy the code
11, browser
If you want to use modules on the client side, use the Browser field instead of the main field.
12, bin
The bin command is used to specify the location of executable files corresponding to internal commands.
"bin": {
"mybuild": "./bin/mybuild.js"
}
Copy the code
The above code specifies that the executable file corresponding to the mybuild command is mybuild.js in the bin subdirectory. When myApp is installed locally, Npm looks for this file and makes a symbolic link (shortcut) in the./node_modules/.bin/ directory. In the example above, mybuild.js creates symbolic links./node_modules/.bin/mybuild. The./node_modules/. Bin/’ directory adds the system’s PATH variable at runtime, so you can invoke these scripts directly with commands without paths when running NPM.
npx mybuild
Copy the code
All the commands in node_modules/.bin/ can be run in the NPM run or NPX format. Files referenced in bin must start with #! The/usr/bin/envnode beginning
13, man
Used to specify the location of the man document for the current module
"man" :[ "./doc/doc.1" ]
Copy the code
14, directories,
The method used to identify the structure of a module is similar to that described in the CommonJS package specification. If you want to view package.json in NPM, you can see the doc, lib, and man directories and locations
15 and the repository
The address where the code is stored
"repository": {
"type" : "git",
"url" : "https://github.com/npm/XXX.git"
}
Copy the code
16, scripts,
Scripts specifies the NPM command line abbreviation to run the script command
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build"
},
Copy the code
For example, serve specifies the project run command NPM run serve
NPM run Build stands for project packaging
17, the config
Use to add command line environment variables
"config":{
"name" : "fang",
"config" : { "port" : "8080" },
"scripts" : { "serve": "vue-cli-service serve"}
},
Copy the code
You can change the value by entering the command NPM config set
npm config set fang:port 8088
Dependencies and dependencies
These installation packages are packages that the program depends on and need to be published to the production environment.
Dev dev dev dev dev dev dev dev dev dev
The –save argument writes the module to the dependencies property, and –save-dev writes the module to the devDependencies property
For example, install Axios
Install to the development environment NPM axios –save-dev
Install to production environment NPM AXIos –save
The difference between:
The plug-in in devDependencies is only for development, not production, whereas Dependencies is published to production
. For example, Babel’s dependency on conversion es6 to ES5 is only for conversion in the development environment, not in production, so just write
In devDependencies, where actual operations like vue or element-UI call, need to be written.
You can add various restrictions before the version, including the following: Specify the version: for example, 1.2.2, follow the “major version”. Minor version. Minor version specifies that only the specified version will be installed during installation.
Tilde + specified version: for example, ~1.2.2 means that the latest version of 1.2.x is installed (not less than 1.2.2), but 1.3.x is not installed, that is, the major and minor versions are not changed during installation.
We repeat that we install the latest version of 1.x.x (at least 1.2.2), but we do not install 2.x.x, that is, we do not change the larger version number when we install. It is important to note that if the major version number is 0, the behavior of the insertion number is the same as that of the tilde. This is because at this stage of development, even minor version number changes can cause incompatibilities in the program.
Latest: Installs the latest version.
For example:
>1.0.2 Is greater than the current version
>=1.0.2 Must be greater than or equal to the current version
<1.0.2 Is smaller than the current version
<=1.0.2 Is less than or equal to the current version
~1.0.2 not less than 1.0.2, but do not change the major and minor version numbers
^1.0.2 not less than 1.0,2, but do not change the large version number
1.2.x indicates that versions 1.2.3 and 1.2.4 are supported
19, peerDependencies
When we develop a module, we have a problem if the current module and the dependent module depend on a third party module, and rely on two incompatible versions.
For example, your project relies on version 1.0 of module A and module B, and module A itself relies on version 2.0 of module B.
In most cases this is not a problem and both versions of the B module can coexist and run at the same time. There is a situation, however, where the problem is that this dependency is exposed to the user.
The most typical scenario is A plug-in, such as module A being A plug-in for module B. The user installed module B is version 1.0, but plug-in A can only be used with module B of version 2.0. At this point, users will have problems if they pass an instance of version 1.0 B to A. Therefore, A mechanism is needed to remind the user when the template is installed that if A and B are installed together, then B must be A 2.0 module.
The peerDependencies field is used by the plug-in to specify the version of the main tool that it needs
Starting with NPM 3.0, peerDependencies are no longer installed by default
20, bundledDependencies
BundledDependencies is an array that specifies that modules defined are packaged together when they are published
{"name": "vue-project", "version": "1.0.0", "bundledDependencies": ["elementui", "echarts"]}Copy the code
21, optionaldependencies
You can place the package in the optionalDependencies object if the package is not found or the installation fails, but NPM continues to run.
"OptionalDependencies ": {"echarts": "^4.9.0"}Copy the code
NPM install will not fail if the module fails to be installed
22, engines
The Engines field specifies the platform on which the module is running, such as a version of Node, or a version of NPM or a browser.
"Engines" : {" node ":" > = 8.9.0 < 12. X ", "NPM" : "~ 6.14.12"}Copy the code
23, OS
Specify what operating system your project will run on
"os" : [ "win32", "darwin", "linux" ],
Copy the code
24, CPU,
Specify on what CPU architecture your project will run
"cpu" : [ "x64", "ia32" ]
Copy the code
25, private
Determine if our project will be published, if set to true, then NPM will refuse to publish
"private": true
Copy the code
26, publishConfig
Sets a collection of values to take effect when the module is published
Normally publishConfig is used in conjunction with private if you only want the module to be published to a specific NPM repository, such as an internal repository
"Private" : true, "publishConfig" : {" tag ":" 1.0.0 ", "the registry" : "https://registry.npmjs.org/", "access" : "public"}Copy the code
27, preferGlobal
Displays a warning if the installation is not global
This article is dedicated to the need of the people, write in a hurry & personal ability is limited, there is a mistake hope forgive! Common progress