npm-init.1 11 KB

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