1 ---
2 title: npm-init
3 section: 1
4 description: Create a package.json file
5 github_repo: npm/cli
6 github_branch: release/v8
7 github_path: docs/lib/content/commands/npm-init.md
8 redirect_from:
9 - /cli-documentation/v8/cli-commands/init
10 - /cli-documentation/v8/cli-commands/npm-init
11 - /cli-documentation/v8/commands/init
12 - /cli-documentation/v8/commands/npm-init
13 - /cli-documentation/v8/init
14 - /cli-documentation/v8/npm-init
15 - /cli/v8/cli-commands/init
16 - /cli/v8/cli-commands/npm-init
17 - /cli/v8/commands/init
18 - /cli/v8/init
19 - /cli/v8/npm-init
20 ---
21
22 ### Synopsis
23
24 ```bash
25 npm init <package-spec> (same as `npx <package-spec>)
26 npm init <@scope> (same as `npx <@scope>/create`)
27
28 aliases: create, innit
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/v8/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 - `npm init @usr@2.0.0` -> `npm exec @usr/create@2.0.0`
43 - `npm init @usr/foo@2.0.0` -> `npm exec @usr/create-foo@2.0.0`
44
45 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.
46
47 _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:
48
49 - `npm init foo@latest` # fetches and runs the latest `create-foo` from the registry
50 - `npm init foo@1.2.3` # runs `create-foo@1.2.3` specifically
51
52 #### Forwarding additional options
53
54 Any additional options will be passed directly to the command, so `npm init foo -- --hello` will map to `npm exec -- create-foo --hello`.
55
56 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:
57
58 - `npm init foo -y --registry=<url> -- --hello -a`
59 - `npm exec -y --registry=<url> -- create-foo --hello -a`
60
61 ### Examples
62
63 Create a new React-based project using [`create-react-app`](https://npm.im/create-react-app):
64
65 ```bash
66 $ npm init react-app ./my-react-app
67 ```
68
69 Create a new `esm`-compatible package using [`create-esm`](https://npm.im/create-esm):
70
71 ```bash
72 $ mkdir my-esm-lib && cd my-esm-lib
73 $ npm init esm --yes
74 ```
75
76 Generate a plain old package.json using legacy init:
77
78 ```bash
79 $ mkdir my-npm-pkg && cd my-npm-pkg
80 $ git init
81 $ npm init
82 ```
83
84 Generate it without having it ask any questions:
85
86 ```bash
87 $ npm init -y
88 ```
89
90 ### Workspaces support
91
92 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.
93
94 Given a project with no workspaces, e.g:
95
96 ```
97 .
98 +-- package.json
99 ```
100
101 You may generate a new workspace using the legacy init:
102
103 ```bash
104 $ npm init -w packages/a
105 ```
106
107 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:
108
109 ```
110 .
111 +-- package.json
112 `-- packages
113 `-- a
114 `-- package.json
115 ```
116
117 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:
118
119 ```bash
120 npm init -w packages/my-react-app react-app .
121 ```
122
123 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 .`:
124
125 ```
126 .
127 +-- package.json
128 `-- packages
129 +-- a
130 | `-- package.json
131 `-- my-react-app
132 +-- README
133 +-- package.json
134 `-- ...
135 ```
136
137 ### Configuration
138
139 #### `yes`
140
141 - Default: null
142 - Type: null or Boolean
143
144 Automatically answer "yes" to any prompts that npm might print on the command line.
145
146 #### `force`
147
148 - Default: false
149 - Type: Boolean
150
151 Removes various protections against unfortunate side effects, common mistakes, unnecessary performance degradation, and malicious input.
152
153 - Allow clobbering non-npm files in global installs.
154 - Allow the `npm version` command to work on an unclean git repository.
155 - Allow deleting the cache folder with `npm cache clean`.
156 - Allow installing packages that have an `engines` declaration requiring a different version of npm.
157 - Allow installing packages that have an `engines` declaration requiring a different version of `node`, even if `--engine-strict` is enabled.
158 - Allow `npm audit fix` to install modules outside your stated dependency range (including SemVer-major changes).
159 - Allow unpublishing all versions of a published package.
160 - Allow conflicting peerDependencies to be installed in the root project.
161 - Implicitly set `--yes` during `npm init`.
162 - Allow clobbering existing values in `npm pkg`
163 - Allow unpublishing of entire packages (not just a single version).
164
165 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!
166
167 #### `scope`
168
169 - Default: the scope of the current project, if any, or ""
170 - Type: String
171
172 Associate an operation with a scope for a scoped registry.
173
174 Useful when logging in to or out of a private registry:
175
176 ```
177 # log in, linking the scope to the custom registry
178 npm login --scope=@mycorp --registry=https://registry.mycorp.com
179
180 # log out, removing the link and the auth token
181 npm logout --scope=@mycorp
182 ```
183
184 This will cause `@mycorp` to be mapped to the registry for future installation of packages specified according to the pattern `@mycorp/package`.
185
186 This will also cause `npm init` to create a scoped package.
187
188 ```
189 # accept all defaults, and create a package named "@foo/whatever",
190 # instead of just named "whatever"
191 npm init --scope=@foo --yes
192 ```
193
194 #### `workspace`
195
196 - Default:
197 - Type: String (can be set multiple times)
198
199 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.
200
201 Valid values for the `workspace` config are either:
202
203 - Workspace names
204 - Path to a workspace directory
205 - Path to a parent workspace directory (will result in selecting all workspaces within that folder)
206
207 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.
208
209 This value is not exported to the environment for child processes.
210
211 #### `workspaces`
212
213 - Default: null
214 - Type: null or Boolean
215
216 Set to true to run the command in the context of **all** configured workspaces.
217
218 Explicitly setting this to false will cause commands like `install` to ignore workspaces altogether. When not set explicitly:
219
220 - 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.
221
222 This value is not exported to the environment for child processes.
223
224 #### `workspaces-update`
225
226 - Default: true
227 - Type: Boolean
228
229 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.
230
231 #### `include-workspace-root`
232
233 - Default: false
234 - Type: Boolean
235
236 Include the workspace root when workspaces are enabled for a command.
237
238 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.
239
240 This value is not exported to the environment for child processes.
241
242 ### See Also
243
244 - [package spec](/cli/v8/using-npm/package-spec)
245 - [init-package-json module](http://npm.im/init-package-json)
246 - [package.json](/cli/v8/configuring-npm/package-json)
247 - [npm version](/cli/v8/commands/npm-version)
248 - [npm scope](/cli/v8/using-npm/scope)
249 - [npm exec](/cli/v8/commands/npm-exec)
250 - [npm workspaces](/cli/v8/using-npm/workspaces)