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