At the root of each project, there is typically a package.json file that defines the various templates and configuration information needed for the project. Based on this configuration file, the NPM install command automatically downloads the required modules and configures the runtime and development environment required in the project.

{
  "name": "first_electron"."version": "0.1.0 from"."description": "My first Electron project."."main": "main.js"."scripts": {
    "start": "electron ."
  },
  "author": "samrt crane"."license": "ISC"."devDependencies": {
    "electron": "^ 9.2.0"}}Copy the code

Using the NPM init command, you can initialize a package.json file by asking the user to enter name, version, etc. (you can also press enter and use the default Settings), which will generate the package.json configuration file as shown above.

The package.json file above contains the project name, version number, description, entry file, execution script, author, open source protocol, etc. However, package.json is much more than that.


Detailed introduction

Name: indicates the package name

The name cannot start with a dot (.) or an underscore (_), and cannot contain uppercase letters. In business code, the corresponding package is introduced by require(${name}).

Version: indicates the version of the package

It’s usually a big version. Minor version, for example, 12.1.0. In a released project, the name and version together determine the single piece of code.

Description: Indicates the package description

Briefly introduce the user to what the library is for. When developing component libraries, it doesn’t matter for corporate business projects.

Keywords:

This section describes the NPM package, facilitating users to search in the NPM. It doesn’t matter for corporate business items.

License: open source protocol

Open source protocol (ISC) is similar to BSD.

[Img -fpowvkbg-1597991039238] [Images/license.png] [Img -fpowvkbg-1597991039238]

Author: the author

Ficolin-3: Contributors

Main: indicates the code entry

This is very important, especially for component libraries. When you want to change the code of a component library you are using in node_modules, first look at the node_modules library, first look at the main file, find the component library entry file, and then modify.

Scripts: NPM command line abbreviation for running scripts

Let’s start with an example:

"scripts": {
	"start": "electron ."."dev": "electron . --debug"."test": "mocha && standard",},Copy the code

Enter NPM run XXX on the command line, and the corresponding command will be executed. For example, NPM run dev or NPM run start. When NPM run XXX is executed, the node_modules/.bin/ directory is added to the system’s Path variable at runtime.

In the example above, when we execute NPM run dev, we are actually executing

node_modules/.bin/electron . --debug
Copy the code

The repository: warehouse

Let the user find the address of your code base, and this configuration will take effect directly on the NPM home page of the component library.

Dependencies: The dependencies on which the project runs

Packages installed via NPM install –save will appear here.

Here’s an example:

"dependencies": {
	"electron": "^" 7.2.4."electron-log": "^ 2.2.14"."electron-settings": "^ 3.0.7"."electron-shortcut-normalizer": "^ 1.0.0"."glob": "^ 7.1.0"."highlight.js": "^ 9.3.0"."update-electron-app": "^ 1.1.1"
},
Copy the code

The rules for setting the version are as follows:

"dependencies": {
	"electron": "7.2.4".// Specify to install version 7.2.4
	"electron": "~ 7.2.4".// Install the latest version of 7.2.x, which is not earlier than 7.2.4
	"electron": "^" 7.2.4.// The installation version is the latest 7.x.x version, which is 7.2.4 or higher
	"electron": "latest".// Install the latest version
},
Copy the code

DevDependencies: Dependencies for project development

Packages installed via NPM install — save-dev will appear here. It relies on some tools during development, similar to dependencies.

Here’s an example:

"devDependencies": {
	"@octokit/rest": "^ 16.3.2"."chai": "^ 3.4.1 track"."chai-as-promised": "^ 6.0.0"."check-for-leaks": "^ 1.2.1." "."devtron": "^ 1.3.0"."electron-packager": "^ 12.1.0"."electron-winstaller": "^ 2.2.0." "."husky": "^ 0.14.3"."mocha": "^ 5.2.0." "."npm-run-all": "^ 4.0.2." "."request": "^ 2.70.0"."rimraf": "^ 2.5.2." "."signcode": "^ 0.5.0"."spectron": "^ 5.0.0"."standard": "^ 8.2.0"."tap": "^ 14.10.6"
},
Copy the code

conclusion

For package.json, you usually only need to focus on scripts, dependencies, and devDependencies.

Reference documentation

All configuration items for package.json and their usage