reggi/cache-based-on-version
@reggi/path-to-regexp
dependabot/npm_and_yarn/main/copy-to-clipboard-4.0.2
dependabot/npm_and_yarn/main/eslint-10.4.0
dependabot/npm_and_yarn/main/npmcli/eslint-config-7.0.0
dependabot/npm_and_yarn/main/proc-log-7.0.0
dependabot/npm_and_yarn/npm_and_yarn-826852524d
dependabot/npm_and_yarn/npm_and_yarn-ab9a7f4bc2
deprecate-totp-2fa
dhei/classic-tokens
gat-bypass-2fa-docs
jpg619/fix-accessibility-content-flow
jpg619/version-bump-tar-2
kartykp/gat-bypass-2fa-docs
kartykp/upgrade-path-to-regex
main
maitxn/version-bump-tar
patch-1
reggi/cache-based-on-version
reggi/dev-engines
reggi/fix-transform-prettier
reggi/overrides
update-search-sensitivity
| 1 | --- |
| 2 | title: Creating a package.json file |
| 3 | redirect_from: |
| 4 | - /about-package-json-and-package-lock-json-files |
| 5 | - /getting-started/using-a-package.json |
| 6 | --- |
| 7 | |
| 8 | You can add a `package.json` file to your package to make it easy for others to manage and install. Packages published to the registry must contain a `package.json` file. |
| 9 | |
| 10 | A `package.json` file: |
| 11 | |
| 12 | - lists the packages your project depends on |
| 13 | - specifies versions of a package that your project can use using [semantic versioning rules][semver] |
| 14 | - makes your build reproducible, and therefore easier to share with other developers |
| 15 | |
| 16 | <Note> |
| 17 | |
| 18 | **Note:** To make your package easier to find on the npm website, we recommend including a custom `description` in your `package.json` file. |
| 19 | |
| 20 | </Note> |
| 21 | |
| 22 | ## `package.json` fields |
| 23 | |
| 24 | ### Required `name` and `version` fields |
| 25 | |
| 26 | A `package.json` file must contain `"name"` and `"version"` fields. |
| 27 | |
| 28 | The `"name"` field contains your package's name and _must_ be lowercase _without_ any spaces. May contain _hyphens_, _dots_, and _underscores_. |
| 29 | |
| 30 | The `"version"` field must be in the form `x.x.x` and follow the [semantic versioning guidelines][semver]. |
| 31 | |
| 32 | ### Author field |
| 33 | |
| 34 | If you want inclusive package author information, in the `"author"` field use the following format (email and website are both optional): |
| 35 | |
| 36 | ``` |
| 37 | Your Name <email@example.com> (https://example.com) |
| 38 | ``` |
| 39 | |
| 40 | ### Example |
| 41 | |
| 42 | ``` |
| 43 | { |
| 44 | "name": "my-awesome-package", |
| 45 | "version": "1.0.0", |
| 46 | "author": "Your Name <email@example.com> (https://example.com)" |
| 47 | } |
| 48 | ``` |
| 49 | |
| 50 | ## Creating a new `package.json` file |
| 51 | |
| 52 | You can create a `package.json` file by running a CLI questionnaire or creating a default `package.json` file. |
| 53 | |
| 54 | ### Running a CLI questionnaire |
| 55 | |
| 56 | To create a `package.json` file with values that you supply, use the `npm init` command. |
| 57 | |
| 58 | 1. On the command line, navigate to the root directory of your package. |
| 59 | |
| 60 | ``` |
| 61 | cd /path/to/package |
| 62 | ``` |
| 63 | |
| 64 | 2. Run the following command: |
| 65 | |
| 66 | ``` |
| 67 | npm init |
| 68 | ``` |
| 69 | |
| 70 | 3. Answer the questions in the command line questionnaire. |
| 71 | |
| 72 | #### Customizing the `package.json` questionnaire |
| 73 | |
| 74 | If you expect to create many `package.json` files, you can customize the questions asked and fields created during the `init` process so all the `package.json` files contain a standard set of information. |
| 75 | |
| 76 | 1. In your home directory, create a file called `.npm-init.js`. |
| 77 | |
| 78 | 2. To add custom questions, using a text editor, add questions with the `prompt` function: |
| 79 | |
| 80 | ``` |
| 81 | module.exports = prompt("what's your favorite flavor of ice cream, buddy?", "I LIKE THEM ALL"); |
| 82 | ``` |
| 83 | |
| 84 | 3. To add custom fields, using a text editor, add desired fields to the `.npm-init.js` file: |
| 85 | |
| 86 | ``` |
| 87 | module.exports = { |
| 88 | customField: 'Example custom field', |
| 89 | otherCustomField: 'This example field is really cool' |
| 90 | } |
| 91 | ``` |
| 92 | |
| 93 | To learn more about creating advanced `npm init` customizations, see the [init-package-json GitHub repository][init-pkg-json GitHub repository]. |
| 94 | |
| 95 | ### Creating a default `package.json` file |
| 96 | |
| 97 | To create a default `package.json` using information extracted from the current directory, use the `npm init` command with the `--yes` or `-y` flag. For a list of default values, see "[Default values extracted from the current directory](#default-values-extracted-from-the-current-directory)". |
| 98 | |
| 99 | 1. On the command line, navigate to the root directory of your package. |
| 100 | |
| 101 | ``` |
| 102 | cd /path/to/package |
| 103 | ``` |
| 104 | |
| 105 | 2. Run the following command: |
| 106 | |
| 107 | ``` |
| 108 | npm init --yes |
| 109 | ``` |
| 110 | |
| 111 | #### Example |
| 112 | |
| 113 | ``` |
| 114 | > npm init --yes |
| 115 | Wrote to /home/monatheoctocat/my_package/package.json: |
| 116 | |
| 117 | { |
| 118 | "name": "my_package", |
| 119 | "description": "make your package easier to find on the npm website", |
| 120 | "version": "1.0.0", |
| 121 | "scripts": { |
| 122 | "test": "echo \"Error: no test specified\" && exit 1" |
| 123 | }, |
| 124 | "repository": { |
| 125 | "type": "git", |
| 126 | "url": "https://github.com/monatheoctocat/my_package.git" |
| 127 | }, |
| 128 | "keywords": [], |
| 129 | "author": "", |
| 130 | "license": "ISC", |
| 131 | "bugs": { |
| 132 | "url": "https://github.com/monatheoctocat/my_package/issues" |
| 133 | }, |
| 134 | "homepage": "https://github.com/monatheoctocat/my_package" |
| 135 | } |
| 136 | ``` |
| 137 | |
| 138 | #### Default values extracted from the current directory |
| 139 | |
| 140 | - `name`: the current directory name |
| 141 | - `version`: always `1.0.0` |
| 142 | - `description`: info about the package, or an empty string `""` |
| 143 | - `scripts`: by default creates an empty `test` script |
| 144 | - `keywords`: empty |
| 145 | - `author`: empty |
| 146 | - `license`: [`ISC`][isc-license] |
| 147 | - `bugs`: information from the current directory, if present |
| 148 | - `homepage`: information from the current directory, if present |
| 149 | |
| 150 | ### Setting config options for the init command |
| 151 | |
| 152 | You can set default config options for the `npm init` command. For example, to set the default author email, author name, and license, on the command line, run the following commands: |
| 153 | |
| 154 | ``` |
| 155 | > npm set init-author-email "example-user@example.com" |
| 156 | > npm set init-author-name "example_user" |
| 157 | > npm set init-license "MIT" |
| 158 | ``` |
| 159 | |
| 160 | [semver]: about-semantic-versioning |
| 161 | [init-pkg-json Github repository]: https://github.com/npm/init-package-json |
| 162 | [isc-license]: https://opensource.org/licenses/ISC |