11cb0ef41Sopenharmony_ci--- 21cb0ef41Sopenharmony_cititle: npm-init 31cb0ef41Sopenharmony_cisection: 1 41cb0ef41Sopenharmony_cidescription: Create a package.json file 51cb0ef41Sopenharmony_ci--- 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ci### Synopsis 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ci```bash 101cb0ef41Sopenharmony_cinpm init <package-spec> (same as `npx <package-spec>`) 111cb0ef41Sopenharmony_cinpm init <@scope> (same as `npx <@scope>/create`) 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_cialiases: create, innit 141cb0ef41Sopenharmony_ci``` 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ci### Description 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_ci`npm init <initializer>` can be used to set up a new or existing npm 191cb0ef41Sopenharmony_cipackage. 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_ci`initializer` in this case is an npm package named `create-<initializer>`, 221cb0ef41Sopenharmony_ciwhich will be installed by [`npm-exec`](/commands/npm-exec), and then have its 231cb0ef41Sopenharmony_cimain bin executed -- presumably creating or updating `package.json` and 241cb0ef41Sopenharmony_cirunning any other initialization-related operations. 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ciThe init command is transformed to a corresponding `npm exec` operation as 271cb0ef41Sopenharmony_cifollows: 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ci* `npm init foo` -> `npm exec create-foo` 301cb0ef41Sopenharmony_ci* `npm init @usr/foo` -> `npm exec @usr/create-foo` 311cb0ef41Sopenharmony_ci* `npm init @usr` -> `npm exec @usr/create` 321cb0ef41Sopenharmony_ci* `npm init @usr@2.0.0` -> `npm exec @usr/create@2.0.0` 331cb0ef41Sopenharmony_ci* `npm init @usr/foo@2.0.0` -> `npm exec @usr/create-foo@2.0.0` 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ciIf the initializer is omitted (by just calling `npm init`), init will fall 361cb0ef41Sopenharmony_ciback to legacy init behavior. It will ask you a bunch of questions, and 371cb0ef41Sopenharmony_cithen write a package.json for you. It will attempt to make reasonable 381cb0ef41Sopenharmony_ciguesses based on existing fields, dependencies, and options selected. It is 391cb0ef41Sopenharmony_cistrictly additive, so it will keep any fields and values that were already 401cb0ef41Sopenharmony_ciset. You can also use `-y`/`--yes` to skip the questionnaire altogether. If 411cb0ef41Sopenharmony_ciyou pass `--scope`, it will create a scoped package. 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ci*Note:* if a user already has the `create-<initializer>` package 441cb0ef41Sopenharmony_ciglobally installed, that will be what `npm init` uses. If you want npm 451cb0ef41Sopenharmony_cito use the latest version, or another specific version you must specify 461cb0ef41Sopenharmony_ciit: 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ci* `npm init foo@latest` # fetches and runs the latest `create-foo` from 491cb0ef41Sopenharmony_ci the registry 501cb0ef41Sopenharmony_ci* `npm init foo@1.2.3` # runs `create-foo@1.2.3` specifically 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_ci#### Forwarding additional options 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_ciAny additional options will be passed directly to the command, so `npm init 551cb0ef41Sopenharmony_cifoo -- --hello` will map to `npm exec -- create-foo --hello`. 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_ciTo better illustrate how options are forwarded, here's a more evolved 581cb0ef41Sopenharmony_ciexample showing options passed to both the **npm cli** and a create package, 591cb0ef41Sopenharmony_ciboth following commands are equivalent: 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ci- `npm init foo -y --registry=<url> -- --hello -a` 621cb0ef41Sopenharmony_ci- `npm exec -y --registry=<url> -- create-foo --hello -a` 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_ci### Examples 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ciCreate a new React-based project using 671cb0ef41Sopenharmony_ci[`create-react-app`](https://npm.im/create-react-app): 681cb0ef41Sopenharmony_ci 691cb0ef41Sopenharmony_ci```bash 701cb0ef41Sopenharmony_ci$ npm init react-app ./my-react-app 711cb0ef41Sopenharmony_ci``` 721cb0ef41Sopenharmony_ci 731cb0ef41Sopenharmony_ciCreate a new `esm`-compatible package using 741cb0ef41Sopenharmony_ci[`create-esm`](https://npm.im/create-esm): 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ci```bash 771cb0ef41Sopenharmony_ci$ mkdir my-esm-lib && cd my-esm-lib 781cb0ef41Sopenharmony_ci$ npm init esm --yes 791cb0ef41Sopenharmony_ci``` 801cb0ef41Sopenharmony_ci 811cb0ef41Sopenharmony_ciGenerate a plain old package.json using legacy init: 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_ci```bash 841cb0ef41Sopenharmony_ci$ mkdir my-npm-pkg && cd my-npm-pkg 851cb0ef41Sopenharmony_ci$ git init 861cb0ef41Sopenharmony_ci$ npm init 871cb0ef41Sopenharmony_ci``` 881cb0ef41Sopenharmony_ci 891cb0ef41Sopenharmony_ciGenerate it without having it ask any questions: 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_ci```bash 921cb0ef41Sopenharmony_ci$ npm init -y 931cb0ef41Sopenharmony_ci``` 941cb0ef41Sopenharmony_ci 951cb0ef41Sopenharmony_ci### Workspaces support 961cb0ef41Sopenharmony_ci 971cb0ef41Sopenharmony_ciIt's possible to create a new workspace within your project by using the 981cb0ef41Sopenharmony_ci`workspace` config option. When using `npm init -w <dir>` the cli will 991cb0ef41Sopenharmony_cicreate the folders and boilerplate expected while also adding a reference 1001cb0ef41Sopenharmony_cito your project `package.json` `"workspaces": []` property in order to make 1011cb0ef41Sopenharmony_cisure that new generated **workspace** is properly set up as such. 1021cb0ef41Sopenharmony_ci 1031cb0ef41Sopenharmony_ciGiven a project with no workspaces, e.g: 1041cb0ef41Sopenharmony_ci 1051cb0ef41Sopenharmony_ci``` 1061cb0ef41Sopenharmony_ci. 1071cb0ef41Sopenharmony_ci+-- package.json 1081cb0ef41Sopenharmony_ci``` 1091cb0ef41Sopenharmony_ci 1101cb0ef41Sopenharmony_ciYou may generate a new workspace using the legacy init: 1111cb0ef41Sopenharmony_ci 1121cb0ef41Sopenharmony_ci```bash 1131cb0ef41Sopenharmony_ci$ npm init -w packages/a 1141cb0ef41Sopenharmony_ci``` 1151cb0ef41Sopenharmony_ci 1161cb0ef41Sopenharmony_ciThat will generate a new folder and `package.json` file, while also updating 1171cb0ef41Sopenharmony_ciyour top-level `package.json` to add the reference to this new workspace: 1181cb0ef41Sopenharmony_ci 1191cb0ef41Sopenharmony_ci``` 1201cb0ef41Sopenharmony_ci. 1211cb0ef41Sopenharmony_ci+-- package.json 1221cb0ef41Sopenharmony_ci`-- packages 1231cb0ef41Sopenharmony_ci `-- a 1241cb0ef41Sopenharmony_ci `-- package.json 1251cb0ef41Sopenharmony_ci``` 1261cb0ef41Sopenharmony_ci 1271cb0ef41Sopenharmony_ciThe workspaces init also supports the `npm init <initializer> -w <dir>` 1281cb0ef41Sopenharmony_cisyntax, following the same set of rules explained earlier in the initial 1291cb0ef41Sopenharmony_ci**Description** section of this page. Similar to the previous example of 1301cb0ef41Sopenharmony_cicreating a new React-based project using 1311cb0ef41Sopenharmony_ci[`create-react-app`](https://npm.im/create-react-app), the following syntax 1321cb0ef41Sopenharmony_ciwill make sure to create the new react app as a nested **workspace** within your 1331cb0ef41Sopenharmony_ciproject and configure your `package.json` to recognize it as such: 1341cb0ef41Sopenharmony_ci 1351cb0ef41Sopenharmony_ci```bash 1361cb0ef41Sopenharmony_cinpm init -w packages/my-react-app react-app . 1371cb0ef41Sopenharmony_ci``` 1381cb0ef41Sopenharmony_ci 1391cb0ef41Sopenharmony_ciThis will make sure to generate your react app as expected, one important 1401cb0ef41Sopenharmony_ciconsideration to have in mind is that `npm exec` is going to be run in the 1411cb0ef41Sopenharmony_cicontext of the newly created folder for that workspace, and that's the reason 1421cb0ef41Sopenharmony_ciwhy in this example the initializer uses the initializer name followed with a 1431cb0ef41Sopenharmony_cidot to represent the current directory in that context, e.g: `react-app .`: 1441cb0ef41Sopenharmony_ci 1451cb0ef41Sopenharmony_ci``` 1461cb0ef41Sopenharmony_ci. 1471cb0ef41Sopenharmony_ci+-- package.json 1481cb0ef41Sopenharmony_ci`-- packages 1491cb0ef41Sopenharmony_ci +-- a 1501cb0ef41Sopenharmony_ci | `-- package.json 1511cb0ef41Sopenharmony_ci `-- my-react-app 1521cb0ef41Sopenharmony_ci +-- README 1531cb0ef41Sopenharmony_ci +-- package.json 1541cb0ef41Sopenharmony_ci `-- ... 1551cb0ef41Sopenharmony_ci``` 1561cb0ef41Sopenharmony_ci 1571cb0ef41Sopenharmony_ci### Configuration 1581cb0ef41Sopenharmony_ci 1591cb0ef41Sopenharmony_ci#### `init-author-name` 1601cb0ef41Sopenharmony_ci 1611cb0ef41Sopenharmony_ci* Default: "" 1621cb0ef41Sopenharmony_ci* Type: String 1631cb0ef41Sopenharmony_ci 1641cb0ef41Sopenharmony_ciThe value `npm init` should use by default for the package author's name. 1651cb0ef41Sopenharmony_ci 1661cb0ef41Sopenharmony_ci 1671cb0ef41Sopenharmony_ci 1681cb0ef41Sopenharmony_ci#### `init-author-url` 1691cb0ef41Sopenharmony_ci 1701cb0ef41Sopenharmony_ci* Default: "" 1711cb0ef41Sopenharmony_ci* Type: "" or URL 1721cb0ef41Sopenharmony_ci 1731cb0ef41Sopenharmony_ciThe value `npm init` should use by default for the package author's 1741cb0ef41Sopenharmony_cihomepage. 1751cb0ef41Sopenharmony_ci 1761cb0ef41Sopenharmony_ci 1771cb0ef41Sopenharmony_ci 1781cb0ef41Sopenharmony_ci#### `init-license` 1791cb0ef41Sopenharmony_ci 1801cb0ef41Sopenharmony_ci* Default: "ISC" 1811cb0ef41Sopenharmony_ci* Type: String 1821cb0ef41Sopenharmony_ci 1831cb0ef41Sopenharmony_ciThe value `npm init` should use by default for the package license. 1841cb0ef41Sopenharmony_ci 1851cb0ef41Sopenharmony_ci 1861cb0ef41Sopenharmony_ci 1871cb0ef41Sopenharmony_ci#### `init-module` 1881cb0ef41Sopenharmony_ci 1891cb0ef41Sopenharmony_ci* Default: "~/.npm-init.js" 1901cb0ef41Sopenharmony_ci* Type: Path 1911cb0ef41Sopenharmony_ci 1921cb0ef41Sopenharmony_ciA module that will be loaded by the `npm init` command. See the 1931cb0ef41Sopenharmony_cidocumentation for the 1941cb0ef41Sopenharmony_ci[init-package-json](https://github.com/npm/init-package-json) module for 1951cb0ef41Sopenharmony_cimore information, or [npm init](/commands/npm-init). 1961cb0ef41Sopenharmony_ci 1971cb0ef41Sopenharmony_ci 1981cb0ef41Sopenharmony_ci 1991cb0ef41Sopenharmony_ci#### `init-version` 2001cb0ef41Sopenharmony_ci 2011cb0ef41Sopenharmony_ci* Default: "1.0.0" 2021cb0ef41Sopenharmony_ci* Type: SemVer string 2031cb0ef41Sopenharmony_ci 2041cb0ef41Sopenharmony_ciThe value that `npm init` should use by default for the package version 2051cb0ef41Sopenharmony_cinumber, if not already set in package.json. 2061cb0ef41Sopenharmony_ci 2071cb0ef41Sopenharmony_ci 2081cb0ef41Sopenharmony_ci 2091cb0ef41Sopenharmony_ci#### `yes` 2101cb0ef41Sopenharmony_ci 2111cb0ef41Sopenharmony_ci* Default: null 2121cb0ef41Sopenharmony_ci* Type: null or Boolean 2131cb0ef41Sopenharmony_ci 2141cb0ef41Sopenharmony_ciAutomatically answer "yes" to any prompts that npm might print on the 2151cb0ef41Sopenharmony_cicommand line. 2161cb0ef41Sopenharmony_ci 2171cb0ef41Sopenharmony_ci 2181cb0ef41Sopenharmony_ci 2191cb0ef41Sopenharmony_ci#### `force` 2201cb0ef41Sopenharmony_ci 2211cb0ef41Sopenharmony_ci* Default: false 2221cb0ef41Sopenharmony_ci* Type: Boolean 2231cb0ef41Sopenharmony_ci 2241cb0ef41Sopenharmony_ciRemoves various protections against unfortunate side effects, common 2251cb0ef41Sopenharmony_cimistakes, unnecessary performance degradation, and malicious input. 2261cb0ef41Sopenharmony_ci 2271cb0ef41Sopenharmony_ci* Allow clobbering non-npm files in global installs. 2281cb0ef41Sopenharmony_ci* Allow the `npm version` command to work on an unclean git repository. 2291cb0ef41Sopenharmony_ci* Allow deleting the cache folder with `npm cache clean`. 2301cb0ef41Sopenharmony_ci* Allow installing packages that have an `engines` declaration requiring a 2311cb0ef41Sopenharmony_ci different version of npm. 2321cb0ef41Sopenharmony_ci* Allow installing packages that have an `engines` declaration requiring a 2331cb0ef41Sopenharmony_ci different version of `node`, even if `--engine-strict` is enabled. 2341cb0ef41Sopenharmony_ci* Allow `npm audit fix` to install modules outside your stated dependency 2351cb0ef41Sopenharmony_ci range (including SemVer-major changes). 2361cb0ef41Sopenharmony_ci* Allow unpublishing all versions of a published package. 2371cb0ef41Sopenharmony_ci* Allow conflicting peerDependencies to be installed in the root project. 2381cb0ef41Sopenharmony_ci* Implicitly set `--yes` during `npm init`. 2391cb0ef41Sopenharmony_ci* Allow clobbering existing values in `npm pkg` 2401cb0ef41Sopenharmony_ci* Allow unpublishing of entire packages (not just a single version). 2411cb0ef41Sopenharmony_ci 2421cb0ef41Sopenharmony_ciIf you don't have a clear idea of what you want to do, it is strongly 2431cb0ef41Sopenharmony_cirecommended that you do not use this option! 2441cb0ef41Sopenharmony_ci 2451cb0ef41Sopenharmony_ci 2461cb0ef41Sopenharmony_ci 2471cb0ef41Sopenharmony_ci#### `scope` 2481cb0ef41Sopenharmony_ci 2491cb0ef41Sopenharmony_ci* Default: the scope of the current project, if any, or "" 2501cb0ef41Sopenharmony_ci* Type: String 2511cb0ef41Sopenharmony_ci 2521cb0ef41Sopenharmony_ciAssociate an operation with a scope for a scoped registry. 2531cb0ef41Sopenharmony_ci 2541cb0ef41Sopenharmony_ciUseful when logging in to or out of a private registry: 2551cb0ef41Sopenharmony_ci 2561cb0ef41Sopenharmony_ci``` 2571cb0ef41Sopenharmony_ci# log in, linking the scope to the custom registry 2581cb0ef41Sopenharmony_cinpm login --scope=@mycorp --registry=https://registry.mycorp.com 2591cb0ef41Sopenharmony_ci 2601cb0ef41Sopenharmony_ci# log out, removing the link and the auth token 2611cb0ef41Sopenharmony_cinpm logout --scope=@mycorp 2621cb0ef41Sopenharmony_ci``` 2631cb0ef41Sopenharmony_ci 2641cb0ef41Sopenharmony_ciThis will cause `@mycorp` to be mapped to the registry for future 2651cb0ef41Sopenharmony_ciinstallation of packages specified according to the pattern 2661cb0ef41Sopenharmony_ci`@mycorp/package`. 2671cb0ef41Sopenharmony_ci 2681cb0ef41Sopenharmony_ciThis will also cause `npm init` to create a scoped package. 2691cb0ef41Sopenharmony_ci 2701cb0ef41Sopenharmony_ci``` 2711cb0ef41Sopenharmony_ci# accept all defaults, and create a package named "@foo/whatever", 2721cb0ef41Sopenharmony_ci# instead of just named "whatever" 2731cb0ef41Sopenharmony_cinpm init --scope=@foo --yes 2741cb0ef41Sopenharmony_ci``` 2751cb0ef41Sopenharmony_ci 2761cb0ef41Sopenharmony_ci 2771cb0ef41Sopenharmony_ci 2781cb0ef41Sopenharmony_ci#### `workspace` 2791cb0ef41Sopenharmony_ci 2801cb0ef41Sopenharmony_ci* Default: 2811cb0ef41Sopenharmony_ci* Type: String (can be set multiple times) 2821cb0ef41Sopenharmony_ci 2831cb0ef41Sopenharmony_ciEnable running a command in the context of the configured workspaces of the 2841cb0ef41Sopenharmony_cicurrent project while filtering by running only the workspaces defined by 2851cb0ef41Sopenharmony_cithis configuration option. 2861cb0ef41Sopenharmony_ci 2871cb0ef41Sopenharmony_ciValid values for the `workspace` config are either: 2881cb0ef41Sopenharmony_ci 2891cb0ef41Sopenharmony_ci* Workspace names 2901cb0ef41Sopenharmony_ci* Path to a workspace directory 2911cb0ef41Sopenharmony_ci* Path to a parent workspace directory (will result in selecting all 2921cb0ef41Sopenharmony_ci workspaces within that folder) 2931cb0ef41Sopenharmony_ci 2941cb0ef41Sopenharmony_ciWhen set for the `npm init` command, this may be set to the folder of a 2951cb0ef41Sopenharmony_ciworkspace which does not yet exist, to create the folder and set it up as a 2961cb0ef41Sopenharmony_cibrand new workspace within the project. 2971cb0ef41Sopenharmony_ci 2981cb0ef41Sopenharmony_ciThis value is not exported to the environment for child processes. 2991cb0ef41Sopenharmony_ci 3001cb0ef41Sopenharmony_ci#### `workspaces` 3011cb0ef41Sopenharmony_ci 3021cb0ef41Sopenharmony_ci* Default: null 3031cb0ef41Sopenharmony_ci* Type: null or Boolean 3041cb0ef41Sopenharmony_ci 3051cb0ef41Sopenharmony_ciSet to true to run the command in the context of **all** configured 3061cb0ef41Sopenharmony_ciworkspaces. 3071cb0ef41Sopenharmony_ci 3081cb0ef41Sopenharmony_ciExplicitly setting this to false will cause commands like `install` to 3091cb0ef41Sopenharmony_ciignore workspaces altogether. When not set explicitly: 3101cb0ef41Sopenharmony_ci 3111cb0ef41Sopenharmony_ci- Commands that operate on the `node_modules` tree (install, update, etc.) 3121cb0ef41Sopenharmony_ciwill link workspaces into the `node_modules` folder. - Commands that do 3131cb0ef41Sopenharmony_ciother things (test, exec, publish, etc.) will operate on the root project, 3141cb0ef41Sopenharmony_ci_unless_ one or more workspaces are specified in the `workspace` config. 3151cb0ef41Sopenharmony_ci 3161cb0ef41Sopenharmony_ciThis value is not exported to the environment for child processes. 3171cb0ef41Sopenharmony_ci 3181cb0ef41Sopenharmony_ci#### `workspaces-update` 3191cb0ef41Sopenharmony_ci 3201cb0ef41Sopenharmony_ci* Default: true 3211cb0ef41Sopenharmony_ci* Type: Boolean 3221cb0ef41Sopenharmony_ci 3231cb0ef41Sopenharmony_ciIf set to true, the npm cli will run an update after operations that may 3241cb0ef41Sopenharmony_cipossibly change the workspaces installed to the `node_modules` folder. 3251cb0ef41Sopenharmony_ci 3261cb0ef41Sopenharmony_ci 3271cb0ef41Sopenharmony_ci 3281cb0ef41Sopenharmony_ci#### `include-workspace-root` 3291cb0ef41Sopenharmony_ci 3301cb0ef41Sopenharmony_ci* Default: false 3311cb0ef41Sopenharmony_ci* Type: Boolean 3321cb0ef41Sopenharmony_ci 3331cb0ef41Sopenharmony_ciInclude the workspace root when workspaces are enabled for a command. 3341cb0ef41Sopenharmony_ci 3351cb0ef41Sopenharmony_ciWhen false, specifying individual workspaces via the `workspace` config, or 3361cb0ef41Sopenharmony_ciall workspaces via the `workspaces` flag, will cause npm to operate only on 3371cb0ef41Sopenharmony_cithe specified workspaces, and not on the root project. 3381cb0ef41Sopenharmony_ci 3391cb0ef41Sopenharmony_ciThis value is not exported to the environment for child processes. 3401cb0ef41Sopenharmony_ci 3411cb0ef41Sopenharmony_ci### See Also 3421cb0ef41Sopenharmony_ci 3431cb0ef41Sopenharmony_ci* [package spec](/using-npm/package-spec) 3441cb0ef41Sopenharmony_ci* [init-package-json module](http://npm.im/init-package-json) 3451cb0ef41Sopenharmony_ci* [package.json](/configuring-npm/package-json) 3461cb0ef41Sopenharmony_ci* [npm version](/commands/npm-version) 3471cb0ef41Sopenharmony_ci* [npm scope](/using-npm/scope) 3481cb0ef41Sopenharmony_ci* [npm exec](/commands/npm-exec) 3491cb0ef41Sopenharmony_ci* [npm workspaces](/using-npm/workspaces) 350