You can add package.json files to packages for others to manage and install. Packages published to the registry must contain package.json files.

A package.json file:

  • Lists the packages that depend on in your project
  • Your project can use semantic versioning rules to specify package versions
  • Make your build replicable and therefore easier to share with other developers
Note: To make your packages easier to find on the NPM website, we recommend including custom descriptions in the package.json file.Copy the code

Package. The json fields

1. Required name and Version fields

The package.json file must contain the name and Version fields. The name field contains the name of the package, which must be lowercase and a single word, possibly including hyphens and underscores. The version field must be in X.X.X format and follow semantic versioning guidelines.

2. The author field

If you want to include package author information in the Author field, use the following format (both mail and website are optional) :

Your Name <[email protected]> (http://example.com)
Copy the code

Example:

{
    "name": "my-awesome-package"."version": "1.0.0"
}
Copy the code

Create a new package.json file

You can create package.json files by running the CLI questionnaire or by creating a default package.json file.

1. Run the CLI poll

Create a package.json file with the values you provide, using the NPM init command. (1) On the cli, navigate to the root directory of the package

cd /path/to/package
Copy the code

(2) Run the following command:

npm init
Copy the code

(3) Answer the questions in the command line questionnaire

Customize package.json questionnaire

If you want to create many package.json files, you can customize the questions and fields raised during initialization so that all package.json contains a standard set of information.

  • In your home directory, create the.npm-init.js file
  • To add a custom question, use the text editor, use the prompt function to add a question:
  module.exports = prompt("what's your favorite flavor of ice cream, buddy?"."I LIKE THEM ALL");
Copy the code
  • To add custom fields, use a text editor to add the required fields to the.npm-init.js file.
module.exports = {
  customField: 'Example custom field'.otherCustomField: 'This example field is really cool'
}
Copy the code

For information on creating advanced NPM init customizations, see the “init-package-json”Github repository.

2. Create a default package.json file

Create a default package.json file with the information extracted from the current directory, using the NPM init command with the –yes or –y flags. For a list of Default values, see “Default values extracted from the current Directory “.

(1) At the command, navigate to the root directory of the package.

cd /path/to/package
Copy the code

(2) Run the following command

npm init --yes
Copy the code

example

  > npm init --yes
Wrote to /home/monatheoctocat/my_package/package.json:

{
  "name": "my_package"."description": ""."version": "1.0.0"."scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git"."url": "https://github.com/monatheoctocat/my_package.git"
  },
  "keywords": []."author": ""."license": "ISC"."bugs": {
    "url": "https://github.com/monatheoctocat/my_package/issues"
  },
  "homepage": "https://github.com/monatheoctocat/my_package"
}
Copy the code
Taken from the default value of the current directory
  • Name: indicates the name of the current directory
  • Version:1.0.0
  • Description: Information in the README, or an empty string “”
  • Scripts: Creates an empty test script by default
  • Keywords: empty
  • Author: empty
  • license: ISC
  • Bugs: Information in the current directory (if it exists)
  • Homepage: information in the current directory (if present)

Sets configuration options for the init command

You can set the default configuration options for the init command. For example, to set the default author email, author name, or license, on the command line, run the following command:

> npm set init.author.email "[email protected]"
> npm set init.author.name "example_user"
> npm set init.license "MIT"
Copy the code