1--- 2title: npm-init 3section: 1 4description: Create a package.json file 5--- 6 7### Synopsis 8 9```bash 10npm init <package-spec> (same as `npx <package-spec>`) 11npm init <@scope> (same as `npx <@scope>/create`) 12 13aliases: create, innit 14``` 15 16### Description 17 18`npm init <initializer>` can be used to set up a new or existing npm 19package. 20 21`initializer` in this case is an npm package named `create-<initializer>`, 22which will be installed by [`npm-exec`](/commands/npm-exec), and then have its 23main bin executed -- presumably creating or updating `package.json` and 24running any other initialization-related operations. 25 26The init command is transformed to a corresponding `npm exec` operation as 27follows: 28 29* `npm init foo` -> `npm exec create-foo` 30* `npm init @usr/foo` -> `npm exec @usr/create-foo` 31* `npm init @usr` -> `npm exec @usr/create` 32* `npm init @usr@2.0.0` -> `npm exec @usr/create@2.0.0` 33* `npm init @usr/foo@2.0.0` -> `npm exec @usr/create-foo@2.0.0` 34 35If the initializer is omitted (by just calling `npm init`), init will fall 36back to legacy init behavior. It will ask you a bunch of questions, and 37then write a package.json for you. It will attempt to make reasonable 38guesses based on existing fields, dependencies, and options selected. It is 39strictly additive, so it will keep any fields and values that were already 40set. You can also use `-y`/`--yes` to skip the questionnaire altogether. If 41you pass `--scope`, it will create a scoped package. 42 43*Note:* if a user already has the `create-<initializer>` package 44globally installed, that will be what `npm init` uses. If you want npm 45to use the latest version, or another specific version you must specify 46it: 47 48* `npm init foo@latest` # fetches and runs the latest `create-foo` from 49 the registry 50* `npm init foo@1.2.3` # runs `create-foo@1.2.3` specifically 51 52#### Forwarding additional options 53 54Any additional options will be passed directly to the command, so `npm init 55foo -- --hello` will map to `npm exec -- create-foo --hello`. 56 57To better illustrate how options are forwarded, here's a more evolved 58example showing options passed to both the **npm cli** and a create package, 59both following commands are equivalent: 60 61- `npm init foo -y --registry=<url> -- --hello -a` 62- `npm exec -y --registry=<url> -- create-foo --hello -a` 63 64### Examples 65 66Create a new React-based project using 67[`create-react-app`](https://npm.im/create-react-app): 68 69```bash 70$ npm init react-app ./my-react-app 71``` 72 73Create a new `esm`-compatible package using 74[`create-esm`](https://npm.im/create-esm): 75 76```bash 77$ mkdir my-esm-lib && cd my-esm-lib 78$ npm init esm --yes 79``` 80 81Generate a plain old package.json using legacy init: 82 83```bash 84$ mkdir my-npm-pkg && cd my-npm-pkg 85$ git init 86$ npm init 87``` 88 89Generate it without having it ask any questions: 90 91```bash 92$ npm init -y 93``` 94 95### Workspaces support 96 97It's possible to create a new workspace within your project by using the 98`workspace` config option. When using `npm init -w <dir>` the cli will 99create the folders and boilerplate expected while also adding a reference 100to your project `package.json` `"workspaces": []` property in order to make 101sure that new generated **workspace** is properly set up as such. 102 103Given a project with no workspaces, e.g: 104 105``` 106. 107+-- package.json 108``` 109 110You may generate a new workspace using the legacy init: 111 112```bash 113$ npm init -w packages/a 114``` 115 116That will generate a new folder and `package.json` file, while also updating 117your top-level `package.json` to add the reference to this new workspace: 118 119``` 120. 121+-- package.json 122`-- packages 123 `-- a 124 `-- package.json 125``` 126 127The workspaces init also supports the `npm init <initializer> -w <dir>` 128syntax, following the same set of rules explained earlier in the initial 129**Description** section of this page. Similar to the previous example of 130creating a new React-based project using 131[`create-react-app`](https://npm.im/create-react-app), the following syntax 132will make sure to create the new react app as a nested **workspace** within your 133project and configure your `package.json` to recognize it as such: 134 135```bash 136npm init -w packages/my-react-app react-app . 137``` 138 139This will make sure to generate your react app as expected, one important 140consideration to have in mind is that `npm exec` is going to be run in the 141context of the newly created folder for that workspace, and that's the reason 142why in this example the initializer uses the initializer name followed with a 143dot to represent the current directory in that context, e.g: `react-app .`: 144 145``` 146. 147+-- package.json 148`-- packages 149 +-- a 150 | `-- package.json 151 `-- my-react-app 152 +-- README 153 +-- package.json 154 `-- ... 155``` 156 157### Configuration 158 159#### `init-author-name` 160 161* Default: "" 162* Type: String 163 164The value `npm init` should use by default for the package author's name. 165 166 167 168#### `init-author-url` 169 170* Default: "" 171* Type: "" or URL 172 173The value `npm init` should use by default for the package author's 174homepage. 175 176 177 178#### `init-license` 179 180* Default: "ISC" 181* Type: String 182 183The value `npm init` should use by default for the package license. 184 185 186 187#### `init-module` 188 189* Default: "~/.npm-init.js" 190* Type: Path 191 192A module that will be loaded by the `npm init` command. See the 193documentation for the 194[init-package-json](https://github.com/npm/init-package-json) module for 195more information, or [npm init](/commands/npm-init). 196 197 198 199#### `init-version` 200 201* Default: "1.0.0" 202* Type: SemVer string 203 204The value that `npm init` should use by default for the package version 205number, if not already set in package.json. 206 207 208 209#### `yes` 210 211* Default: null 212* Type: null or Boolean 213 214Automatically answer "yes" to any prompts that npm might print on the 215command line. 216 217 218 219#### `force` 220 221* Default: false 222* Type: Boolean 223 224Removes various protections against unfortunate side effects, common 225mistakes, unnecessary performance degradation, and malicious input. 226 227* Allow clobbering non-npm files in global installs. 228* Allow the `npm version` command to work on an unclean git repository. 229* Allow deleting the cache folder with `npm cache clean`. 230* Allow installing packages that have an `engines` declaration requiring a 231 different version of npm. 232* Allow installing packages that have an `engines` declaration requiring a 233 different version of `node`, even if `--engine-strict` is enabled. 234* Allow `npm audit fix` to install modules outside your stated dependency 235 range (including SemVer-major changes). 236* Allow unpublishing all versions of a published package. 237* Allow conflicting peerDependencies to be installed in the root project. 238* Implicitly set `--yes` during `npm init`. 239* Allow clobbering existing values in `npm pkg` 240* Allow unpublishing of entire packages (not just a single version). 241 242If you don't have a clear idea of what you want to do, it is strongly 243recommended that you do not use this option! 244 245 246 247#### `scope` 248 249* Default: the scope of the current project, if any, or "" 250* Type: String 251 252Associate an operation with a scope for a scoped registry. 253 254Useful when logging in to or out of a private registry: 255 256``` 257# log in, linking the scope to the custom registry 258npm login --scope=@mycorp --registry=https://registry.mycorp.com 259 260# log out, removing the link and the auth token 261npm logout --scope=@mycorp 262``` 263 264This will cause `@mycorp` to be mapped to the registry for future 265installation of packages specified according to the pattern 266`@mycorp/package`. 267 268This will also cause `npm init` to create a scoped package. 269 270``` 271# accept all defaults, and create a package named "@foo/whatever", 272# instead of just named "whatever" 273npm init --scope=@foo --yes 274``` 275 276 277 278#### `workspace` 279 280* Default: 281* Type: String (can be set multiple times) 282 283Enable running a command in the context of the configured workspaces of the 284current project while filtering by running only the workspaces defined by 285this configuration option. 286 287Valid values for the `workspace` config are either: 288 289* Workspace names 290* Path to a workspace directory 291* Path to a parent workspace directory (will result in selecting all 292 workspaces within that folder) 293 294When set for the `npm init` command, this may be set to the folder of a 295workspace which does not yet exist, to create the folder and set it up as a 296brand new workspace within the project. 297 298This value is not exported to the environment for child processes. 299 300#### `workspaces` 301 302* Default: null 303* Type: null or Boolean 304 305Set to true to run the command in the context of **all** configured 306workspaces. 307 308Explicitly setting this to false will cause commands like `install` to 309ignore workspaces altogether. When not set explicitly: 310 311- Commands that operate on the `node_modules` tree (install, update, etc.) 312will link workspaces into the `node_modules` folder. - Commands that do 313other things (test, exec, publish, etc.) will operate on the root project, 314_unless_ one or more workspaces are specified in the `workspace` config. 315 316This value is not exported to the environment for child processes. 317 318#### `workspaces-update` 319 320* Default: true 321* Type: Boolean 322 323If set to true, the npm cli will run an update after operations that may 324possibly change the workspaces installed to the `node_modules` folder. 325 326 327 328#### `include-workspace-root` 329 330* Default: false 331* Type: Boolean 332 333Include the workspace root when workspaces are enabled for a command. 334 335When false, specifying individual workspaces via the `workspace` config, or 336all workspaces via the `workspaces` flag, will cause npm to operate only on 337the specified workspaces, and not on the root project. 338 339This value is not exported to the environment for child processes. 340 341### See Also 342 343* [package spec](/using-npm/package-spec) 344* [init-package-json module](http://npm.im/init-package-json) 345* [package.json](/configuring-npm/package-json) 346* [npm version](/commands/npm-version) 347* [npm scope](/using-npm/scope) 348* [npm exec](/commands/npm-exec) 349* [npm workspaces](/using-npm/workspaces) 350