Creating a package.json file
See Details
Table of contents
You can add apackage.json file to your package to make it easy for others to manage and install. Packages published to the registry must contain apackage.json file.
Apackage.json file:
- lists the packages your project depends on
- specifies versions of a package that your project can use usingsemantic versioning rules
- makes your build reproducible, and therefore easier to share with other developers
Note: To make your package easier to find on the npm website, we recommend including a customdescription in yourpackage.json file.
package.json fields
Requiredname andversion fields
Apackage.json file must contain"name" and"version" fields.
The"name" field contains your package's name andmust be lowercasewithout any spaces. May containhyphens,dots, andunderscores.
The"version" field must be in the formx.x.x and follow thesemantic versioning guidelines.
Author field
If you want inclusive package author information, in the"author" field use the following format (email and website are both optional):
Your Name <email@example.com> (https://example.com)Example
{"name":"my-awesome-package","version":"1.0.0","author":"Your Name <email@example.com> (https://example.com)"}
Creating a newpackage.json file
You can create apackage.json file by running a CLI questionnaire or creating a defaultpackage.json file.
Running a CLI questionnaire
To create apackage.json file with values that you supply, use thenpm init command.
On the command line, navigate to the root directory of your package.
cd /path/to/packageRun the following command:
npm initAnswer the questions in the command line questionnaire.
Customizing thepackage.json questionnaire
If you expect to create manypackage.json files, you can customize the questions asked and fields created during theinit process so all thepackage.json files contain a standard set of information.
In your home directory, create a file called
.npm-init.js.To add custom questions, using a text editor, add questions with the
promptfunction:module.exports = prompt("what's your favorite flavor of ice cream, buddy?", "I LIKE THEM ALL");To add custom fields, using a text editor, add desired fields to the
.npm-init.jsfile:module.exports={customField:'Example custom field',otherCustomField:'This example field is really cool'}
To learn more about creating advancednpm init customizations, see theinit-package-json GitHub repository.
Creating a defaultpackage.json file
To create a defaultpackage.json using information extracted from the current directory, use thenpm init command with the--yes or-y flag. For a list of default values, see "Default values extracted from the current directory".
On the command line, navigate to the root directory of your package.
cd /path/to/packageRun the following command:
npm init --yes
Example
>npm init--yesWrote to /home/monatheoctocat/my_package/package.json:{"name":"my_package","description":"make your package easier to find on the npm website","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"}
Default values extracted from the current directory
name: the current directory nameversion: always1.0.0description: info about the package, or an empty string""scripts: by default creates an emptytestscriptkeywords: emptyauthor: emptylicense:ISCbugs: information from the current directory, if presenthomepage: information from the current directory, if present
Setting config options for the init command
You can set default config options for thenpm init command. For example, to set the default author email, author name, and license, on the command line, run the following commands:
>npmset init-author-email"example-user@example.com">npmset init-author-name"example_user">npmset init-license"MIT"





