.TH "NPM\-INIT" "1" "November 2023" "9.2.0" .SH "NAME" \fBnpm-init\fR .SH Synopsis .SH Description .P \fBnpm init \fP can be used to set up a new or existing npm .br package\. .P \fBinitializer\fP in this case is an npm package named \fBcreate\-\fP, .br which will be installed by \fBnpm\-exec\fP, and then have its .br main bin executed \-\- presumably creating or updating \fBpackage\.json\fP and .br running any other initialization\-related operations\. .P The init command is transformed to a corresponding \fBnpm exec\fP operation as .br follows: .RS 1 .IP \(bu 2 \fBnpm init foo\fP \-> \fBnpm exec create\-foo\fP .IP \(bu 2 \fBnpm init @usr/foo\fP \-> \fBnpm exec @usr/create\-foo\fP .IP \(bu 2 \fBnpm init @usr\fP \-> \fBnpm exec @usr/create\fP .IP \(bu 2 \fBnpm init @usr@2\.0\.0\fP \-> \fBnpm exec @usr/create@2\.0\.0\fP .IP \(bu 2 \fBnpm init @usr/foo@2\.0\.0\fP \-> \fBnpm exec @usr/create\-foo@2\.0\.0\fP .RE .P If the initializer is omitted (by just calling \fBnpm init\fP), init will fall .br back to legacy init behavior\. It will ask you a bunch of questions, and .br then write a package\.json for you\. It will attempt to make reasonable .br guesses based on existing fields, dependencies, and options selected\. It is .br strictly additive, so it will keep any fields and values that were already .br set\. You can also use \fB\-y\fP/\fB\-\-yes\fP to skip the questionnaire altogether\. If .br you pass \fB\-\-scope\fP, it will create a scoped package\. .P \fINote:\fR if a user already has the \fBcreate\-\fP package .br globally installed, that will be what \fBnpm init\fP uses\. If you want npm .br to use the latest version, or another specific version you must specify .br it: .RS 1 .IP \(bu 2 \fBnpm init foo@latest\fP # fetches and runs the latest \fBcreate\-foo\fP from .br the registry .IP \(bu 2 \fBnpm init foo@1\.2\.3\fP # runs \fBcreate\-foo@1\.2\.3\fP specifically .RE .SS Forwarding additional options .P Any additional options will be passed directly to the command, so \fBnpm init foo \-\- \-\-hello\fP will map to \fBnpm exec \-\- create\-foo \-\-hello\fP\|\. .P To better illustrate how options are forwarded, here's a more evolved .br example showing options passed to both the \fBnpm cli\fR and a create package, .br both following commands are equivalent: .RS 1 .IP \(bu 2 \fBnpm init foo \-y \-\-registry= \-\- \-\-hello \-a\fP .IP \(bu 2 \fBnpm exec \-y \-\-registry= \-\- create\-foo \-\-hello \-a\fP .RE .SH Examples .P Create a new React\-based project using .br \fBcreate\-react\-app\fP: .RS 2 .nf $ npm init react\-app \./my\-react\-app .fi .RE .P Create a new \fBesm\fP\-compatible package using .br \fBcreate\-esm\fP: .RS 2 .nf $ mkdir my\-esm\-lib && cd my\-esm\-lib $ npm init esm \-\-yes .fi .RE .P Generate a plain old package\.json using legacy init: .RS 2 .nf $ mkdir my\-npm\-pkg && cd my\-npm\-pkg $ git init $ npm init .fi .RE .P Generate it without having it ask any questions: .RS 2 .nf $ npm init \-y .fi .RE .SH Workspaces support .P It's possible to create a new workspace within your project by using the .br \fBworkspace\fP config option\. When using \fBnpm init \-w \fP the cli will .br create the folders and boilerplate expected while also adding a reference .br to your project \fBpackage\.json\fP \fB"workspaces": []\fP property in order to make .br sure that new generated \fBworkspace\fR is properly set up as such\. .P Given a project with no workspaces, e\.g: .RS 2 .nf \|\. +\-\- package\.json .fi .RE .P You may generate a new workspace using the legacy init: .RS 2 .nf $ npm init \-w packages/a .fi .RE .P That will generate a new folder and \fBpackage\.json\fP file, while also updating .br your top\-level \fBpackage\.json\fP to add the reference to this new workspace: .RS 2 .nf \|\. +\-\- package\.json `\-\- packages `\-\- a `\-\- package\.json .fi .RE .P The workspaces init also supports the \fBnpm init \-w \fP .br syntax, following the same set of rules explained earlier in the initial .br \fBDescription\fR section of this page\. Similar to the previous example of .br creating a new React\-based project using .br \fBcreate\-react\-app\fP, the following syntax .br will make sure to create the new react app as a nested \fBworkspace\fR within your .br project and configure your \fBpackage\.json\fP to recognize it as such: .RS 2 .nf npm init \-w packages/my\-react\-app react\-app \. .fi .RE .P This will make sure to generate your react app as expected, one important .br consideration to have in mind is that \fBnpm exec\fP is going to be run in the .br context of the newly created folder for that workspace, and that's the reason .br why in this example the initializer uses the initializer name followed with a .br dot to represent the current directory in that context, e\.g: \fBreact\-app \.\fP: .RS 2 .nf \|\. +\-\- package\.json `\-\- packages +\-\- a | `\-\- package\.json `\-\- my\-react\-app +\-\- README +\-\- package\.json `\-\- \.\.\. .fi .RE .SH Configuration .SH See Also .RS 1 .IP \(bu 2 package spec .IP \(bu 2 init\-package\-json module .IP \(bu 2 package\.json .IP \(bu 2 npm version .IP \(bu 2 npm scope .IP \(bu 2 npm exec .IP \(bu 2 npm workspaces .RE