1 ---
2 title: npm-init
3 section: 1
4 description: Create a package.json file
5 github_repo: npm/cli
6 github_branch: release/v7
7 github_path: docs/content/commands/npm-init.md
8 redirect_from:
9 - /cli-documentation/v7/cli-commands/init
10 - /cli-documentation/v7/cli-commands/npm-init
11 - /cli-documentation/v7/commands/init
12 - /cli-documentation/v7/commands/npm-init
13 - /cli-documentation/v7/init
14 - /cli-documentation/v7/npm-init
15 - /cli/v7/cli-commands/init
16 - /cli/v7/cli-commands/npm-init
17 - /cli/v7/commands/init
18 - /cli/v7/init
19 - /cli/v7/npm-init
20 ---
21
22 ### Synopsis
23
24 ```bash
25 npm init [--yes|-y|--scope]
26 npm init <@scope> (same as `npm exec <@scope>/create`)
27 npm init [<@scope>/]<name> (same as `npm exec [<@scope>/]create-<name>`)
28 npm init [-w <dir>] [args...]
29 ```
30
31 ### Description
32
33 `npm init <initializer>` can be used to set up a new or existing npm package.
34
35 `initializer` in this case is an npm package named `create-<initializer>`, which will be installed by [`npm-exec`](/cli/v7/commands/npm-exec), and then have its main bin executed -- presumably creating or updating `package.json` and running any other initialization-related operations.
36
37 The init command is transformed to a corresponding `npm exec` operation as follows:
38
39 - `npm init foo` -> `npm exec create-foo`
40 - `npm init @usr/foo` -> `npm exec @usr/create-foo`
41 - `npm init @usr` -> `npm exec @usr/create`
42
43 If the initializer is omitted (by just calling `npm init`), init will fall back to legacy init behavior. It will ask you a bunch of questions, and then write a package.json for you. It will attempt to make reasonable guesses based on existing fields, dependencies, and options selected. It is strictly additive, so it will keep any fields and values that were already set. You can also use `-y`/`--yes` to skip the questionnaire altogether. If you pass `--scope`, it will create a scoped package.
44
45 #### Forwarding additional options
46
47 Any additional options will be passed directly to the command, so `npm init foo -- --hello` will map to `npm exec -- create-foo --hello`.
48
49 To better illustrate how options are forwarded, here's a more evolved example showing options passed to both the **npm cli** and a create package, both following commands are equivalent:
50
51 - `npm init foo -y --registry=<url> -- --hello -a`
52 - `npm exec -y --registry=<url> -- create-foo --hello -a`
53
54 ### Examples
55
56 Create a new React-based project using [`create-react-app`](https://npm.im/create-react-app):
57
58 ```bash
59 $ npm init react-app ./my-react-app
60 ```
61
62 Create a new `esm`-compatible package using [`create-esm`](https://npm.im/create-esm):
63
64 ```bash
65 $ mkdir my-esm-lib && cd my-esm-lib
66 $ npm init esm --yes
67 ```
68
69 Generate a plain old package.json using legacy init:
70
71 ```bash
72 $ mkdir my-npm-pkg && cd my-npm-pkg
73 $ git init
74 $ npm init
75 ```
76
77 Generate it without having it ask any questions:
78
79 ```bash
80 $ npm init -y
81 ```
82
83 ### Workspaces support
84
85 It's possible to create a new workspace within your project by using the `workspace` config option. When using `npm init -w <dir>` the cli will create the folders and boilerplate expected while also adding a reference to your project `package.json` `"workspaces": []` property in order to make sure that new generated **workspace** is properly set up as such.
86
87 Given a project with no workspaces, e.g:
88
89 ```
90 .
91 +-- package.json
92 ```
93
94 You may generate a new workspace using the legacy init:
95
96 ```bash
97 $ npm init -w packages/a
98 ```
99
100 That will generate a new folder and `package.json` file, while also updating your top-level `package.json` to add the reference to this new workspace:
101
102 ```
103 .
104 +-- package.json
105 `-- packages
106 `-- a
107 `-- package.json
108 ```
109
110 The workspaces init also supports the `npm init <initializer> -w <dir>` syntax, following the same set of rules explained earlier in the initial **Description** section of this page. Similar to the previous example of creating a new React-based project using [`create-react-app`](https://npm.im/create-react-app), the following syntax will make sure to create the new react app as a nested **workspace** within your project and configure your `package.json` to recognize it as such:
111
112 ```bash
113 npm init -w packages/my-react-app react-app .
114 ```
115
116 This will make sure to generate your react app as expected, one important consideration to have in mind is that `npm exec` is going to be run in the context of the newly created folder for that workspace, and that's the reason why in this example the initializer uses the initializer name followed with a dot to represent the current directory in that context, e.g: `react-app .`:
117
118 ```
119 .
120 +-- package.json
121 `-- packages
122 +-- a
123 | `-- package.json
124 `-- my-react-app
125 +-- README
126 +-- package.json
127 `-- ...
128 ```
129
130 ### Configuration
131
132 #### `yes`
133
134 - Default: null
135 - Type: null or Boolean
136
137 Automatically answer "yes" to any prompts that npm might print on the command line.
138
139 #### `force`
140
141 - Default: false
142 - Type: Boolean
143
144 Removes various protections against unfortunate side effects, common mistakes, unnecessary performance degradation, and malicious input.
145
146 - Allow clobbering non-npm files in global installs.
147 - Allow the `npm version` command to work on an unclean git repository.
148 - Allow deleting the cache folder with `npm cache clean`.
149 - Allow installing packages that have an `engines` declaration requiring a different version of npm.
150 - Allow installing packages that have an `engines` declaration requiring a different version of `node`, even if `--engine-strict` is enabled.
151 - Allow `npm audit fix` to install modules outside your stated dependency range (including SemVer-major changes).
152 - Allow unpublishing all versions of a published package.
153 - Allow conflicting peerDependencies to be installed in the root project.
154 - Implicitly set `--yes` during `npm init`.
155 - Allow clobbering existing values in `npm pkg`
156
157 If you don't have a clear idea of what you want to do, it is strongly recommended that you do not use this option!
158
159 #### `workspace`
160
161 - Default:
162 - Type: String (can be set multiple times)
163
164 Enable running a command in the context of the configured workspaces of the current project while filtering by running only the workspaces defined by this configuration option.
165
166 Valid values for the `workspace` config are either:
167
168 - Workspace names
169 - Path to a workspace directory
170 - Path to a parent workspace directory (will result to selecting all of the nested workspaces)
171
172 When set for the `npm init` command, this may be set to the folder of a workspace which does not yet exist, to create the folder and set it up as a brand new workspace within the project.
173
174 This value is not exported to the environment for child processes.
175
176 #### `workspaces`
177
178 - Default: false
179 - Type: Boolean
180
181 Enable running a command in the context of **all** the configured workspaces.
182
183 This value is not exported to the environment for child processes.
184
185 ### See Also
186
187 - [init-package-json module](http://npm.im/init-package-json)
188 - [package.json](/cli/v7/configuring-npm/package-json)
189 - [npm version](/cli/v7/commands/npm-version)
190 - [npm scope](/cli/v7/using-npm/scope)
191 - [npm exec](/cli/v7/commands/npm-exec)
192 - [npm workspaces](/cli/v7/using-npm/workspaces)