2021-05-10 17:31:02 -04:00
|
|
|
.TH "NPM\-INIT" "1" "May 2021" "" ""
|
2011-11-26 09:21:03 -08:00
|
|
|
.SH "NAME"
|
2021-03-23 14:58:11 -04:00
|
|
|
\fBnpm-init\fR \- Create a package\.json file
|
2019-11-05 14:55:08 -05:00
|
|
|
.SS Synopsis
|
2014-09-24 14:41:07 -07:00
|
|
|
.P
|
|
|
|
.RS 2
|
2014-11-04 15:08:12 -08:00
|
|
|
.nf
|
2018-04-20 18:26:37 -07:00
|
|
|
npm init [\-\-force|\-f|\-\-yes|\-y|\-\-scope]
|
2021-04-29 16:30:06 -04:00
|
|
|
npm init <@scope> (same as `npm exec <@scope>/create`)
|
|
|
|
npm init [<@scope>/]<name> (same as `npm exec [<@scope>/]create\-<name>`)
|
|
|
|
npm init [\-w <dir>] [args\.\.\.]
|
2018-04-20 18:26:37 -07:00
|
|
|
.fi
|
|
|
|
.RE
|
2020-11-01 07:54:36 +01:00
|
|
|
.SS Description
|
|
|
|
.P
|
|
|
|
\fBnpm init <initializer>\fP can be used to set up a new or existing npm
|
|
|
|
package\.
|
|
|
|
.P
|
|
|
|
\fBinitializer\fP in this case is an npm package named \fBcreate\-<initializer>\fP,
|
2021-04-29 16:30:06 -04:00
|
|
|
which will be installed by npm help \fBnpm\-exec\fP, and then have its
|
2020-11-01 07:54:36 +01:00
|
|
|
main bin executed \-\- presumably creating or updating \fBpackage\.json\fP and
|
|
|
|
running any other initialization\-related operations\.
|
|
|
|
.P
|
2021-04-29 16:30:06 -04:00
|
|
|
The init command is transformed to a corresponding \fBnpm exec\fP operation as
|
2020-11-01 07:54:36 +01:00
|
|
|
follows:
|
|
|
|
.RS 0
|
|
|
|
.IP \(bu 2
|
2021-04-29 16:30:06 -04:00
|
|
|
\fBnpm init foo\fP \-> \fBnpm exec create\-foo\fP
|
2020-11-01 07:54:36 +01:00
|
|
|
.IP \(bu 2
|
2021-04-29 16:30:06 -04:00
|
|
|
\fBnpm init @usr/foo\fP \-> \fBnpm exec @usr/create\-foo\fP
|
2020-11-01 07:54:36 +01:00
|
|
|
.IP \(bu 2
|
2021-04-29 16:30:06 -04:00
|
|
|
\fBnpm init @usr\fP \-> \fBnpm exec @usr/create\fP
|
2020-11-01 07:54:36 +01:00
|
|
|
|
|
|
|
.RE
|
|
|
|
.P
|
|
|
|
If the initializer is omitted (by just calling \fBnpm init\fP), 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\fP/\fB\-\-yes\fP to skip the questionnaire altogether\. If
|
|
|
|
you pass \fB\-\-scope\fP, it will create a scoped package\.
|
2021-04-29 16:30:06 -04:00
|
|
|
.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
|
|
|
|
example showing options passed to both the \fBnpm cli\fR and a create package,
|
|
|
|
both following commands are equivalent:
|
|
|
|
.RS 0
|
|
|
|
.IP \(bu 2
|
|
|
|
\fBnpm init foo \-y \-\-registry=<url> \-\- \-\-hello \-a\fP
|
|
|
|
.IP \(bu 2
|
|
|
|
\fBnpm exec \-y \-\-registry=<url> \-\- create\-foo \-\-hello \-a\fP
|
|
|
|
|
|
|
|
.RE
|
2019-11-05 14:55:08 -05:00
|
|
|
.SS Examples
|
2018-04-20 18:26:37 -07:00
|
|
|
.P
|
2020-11-01 07:54:36 +01:00
|
|
|
Create a new React\-based project using
|
|
|
|
\fBcreate\-react\-app\fP \fIhttps://npm\.im/create\-react\-app\fR:
|
2018-04-20 18:26:37 -07:00
|
|
|
.P
|
|
|
|
.RS 2
|
|
|
|
.nf
|
|
|
|
$ npm init react\-app \./my\-react\-app
|
|
|
|
.fi
|
|
|
|
.RE
|
|
|
|
.P
|
2020-11-01 07:54:36 +01:00
|
|
|
Create a new \fBesm\fP\-compatible package using
|
|
|
|
\fBcreate\-esm\fP \fIhttps://npm\.im/create\-esm\fR:
|
2018-04-20 18:26:37 -07:00
|
|
|
.P
|
|
|
|
.RS 2
|
|
|
|
.nf
|
|
|
|
$ mkdir my\-esm\-lib && cd my\-esm\-lib
|
|
|
|
$ npm init esm \-\-yes
|
2014-11-04 15:08:12 -08:00
|
|
|
.fi
|
2014-09-24 14:41:07 -07:00
|
|
|
.RE
|
|
|
|
.P
|
2018-04-20 18:26:37 -07:00
|
|
|
Generate a plain old package\.json using legacy init:
|
2011-11-26 09:21:03 -08:00
|
|
|
.P
|
2018-04-20 18:26:37 -07:00
|
|
|
.RS 2
|
|
|
|
.nf
|
|
|
|
$ mkdir my\-npm\-pkg && cd my\-npm\-pkg
|
|
|
|
$ git init
|
|
|
|
$ npm init
|
|
|
|
.fi
|
|
|
|
.RE
|
2011-11-26 09:21:03 -08:00
|
|
|
.P
|
2018-04-20 18:26:37 -07:00
|
|
|
Generate it without having it ask any questions:
|
2011-11-26 09:21:03 -08:00
|
|
|
.P
|
2018-04-20 18:26:37 -07:00
|
|
|
.RS 2
|
|
|
|
.nf
|
|
|
|
$ npm init \-y
|
|
|
|
.fi
|
|
|
|
.RE
|
2021-04-29 16:30:06 -04:00
|
|
|
.SS Workspaces support
|
|
|
|
.P
|
|
|
|
It's possible to create a new workspace within your project by using the
|
|
|
|
\fBworkspace\fP config option\. When using \fBnpm init \-w <dir>\fP the cli will
|
|
|
|
create the folders and boilerplate expected while also adding a reference
|
|
|
|
to your project \fBpackage\.json\fP \fB"workspaces": []\fP property in order to make
|
|
|
|
sure that new generated \fBworkspace\fR is properly set up as such\.
|
|
|
|
.P
|
|
|
|
Given a project with no workspaces, e\.g:
|
|
|
|
.P
|
|
|
|
.RS 2
|
|
|
|
.nf
|
|
|
|
\|\.
|
|
|
|
+\-\- package\.json
|
|
|
|
.fi
|
|
|
|
.RE
|
|
|
|
.P
|
|
|
|
You may generate a new workspace using the legacy init:
|
|
|
|
.P
|
|
|
|
.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
|
|
|
|
your top\-level \fBpackage\.json\fP to add the reference to this new workspace:
|
|
|
|
.P
|
|
|
|
.RS 2
|
|
|
|
.nf
|
|
|
|
\|\.
|
|
|
|
+\-\- package\.json
|
|
|
|
`\-\- packages
|
|
|
|
`\-\- a
|
|
|
|
`\-\- package\.json
|
|
|
|
.fi
|
|
|
|
.RE
|
|
|
|
.P
|
|
|
|
The workspaces init also supports the \fBnpm init <initializer> \-w <dir>\fP
|
|
|
|
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
|
|
|
|
\fBcreate\-react\-app\fP \fIhttps://npm\.im/create\-react\-app\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\fP to recognize it as such:
|
|
|
|
.P
|
|
|
|
.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
|
|
|
|
consideration to have in mind is that \fBnpm exec\fP 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 \.\fP:
|
|
|
|
.P
|
|
|
|
.RS 2
|
|
|
|
.nf
|
|
|
|
\|\.
|
|
|
|
+\-\- package\.json
|
|
|
|
`\-\- packages
|
|
|
|
+\-\- a
|
|
|
|
| `\-\- package\.json
|
|
|
|
`\-\- my\-react\-app
|
|
|
|
+\-\- README
|
|
|
|
+\-\- package\.json
|
|
|
|
`\-\- \.\.\.
|
|
|
|
.fi
|
|
|
|
.RE
|
2021-01-28 17:38:39 -05:00
|
|
|
.SS A note on caching
|
|
|
|
.P
|
|
|
|
The npm cli utilizes its internal package cache when using the package
|
|
|
|
name specified\. You can use the following to change how and when the
|
|
|
|
cli uses this cache\. See npm help \fBcache\fP for more on
|
|
|
|
how the cache works\.
|
|
|
|
.SS prefer\-online
|
|
|
|
.P
|
|
|
|
Forces staleness checks for packages, making the cli look for updates
|
|
|
|
immediately even if the package is already in the cache\.
|
|
|
|
.SS prefer\-offline
|
|
|
|
.P
|
|
|
|
Bypasses staleness checks for packages\. Missing data will still be
|
|
|
|
requested from the server\. To force full offline mode, use \fBoffline\fP\|\.
|
|
|
|
.SS offline
|
|
|
|
.P
|
|
|
|
Forces full offline mode\. Any packages not locally cached will result in
|
|
|
|
an error\.
|
2021-04-29 16:30:06 -04:00
|
|
|
.SS workspace
|
|
|
|
.RS 0
|
|
|
|
.IP \(bu 2
|
|
|
|
Alias: \fB\-w\fP
|
|
|
|
.IP \(bu 2
|
|
|
|
Type: Array
|
|
|
|
.IP \(bu 2
|
|
|
|
Default: \fB[]\fP
|
|
|
|
|
|
|
|
.RE
|
|
|
|
.P
|
|
|
|
Enable running \fBnpm init\fP in the context of workspaces, creating any missing
|
|
|
|
folders, generating files and adding/updating the \fB"workspaces"\fP property of
|
|
|
|
the project \fBpackage\.json\fP\|\.
|
|
|
|
.P
|
|
|
|
the provided names or paths provided\.
|
|
|
|
.P
|
|
|
|
Valid values for the \fBworkspace\fP config are either:
|
|
|
|
.RS 0
|
|
|
|
.IP \(bu 2
|
|
|
|
Workspace names
|
|
|
|
.IP \(bu 2
|
|
|
|
Path to a workspace directory
|
|
|
|
.IP \(bu 2
|
|
|
|
Path to a parent workspace directory (will result to selecting all of the
|
|
|
|
children workspaces)
|
|
|
|
|
|
|
|
.RE
|
|
|
|
.SS workspaces
|
|
|
|
.RS 0
|
|
|
|
.IP \(bu 2
|
|
|
|
Alias: \fB\-ws\fP
|
|
|
|
.IP \(bu 2
|
|
|
|
Type: Boolean
|
|
|
|
.IP \(bu 2
|
|
|
|
Default: \fBfalse\fP
|
|
|
|
|
|
|
|
.RE
|
|
|
|
.P
|
|
|
|
Run \fBnpm init\fP in the context of all configured workspaces for the
|
|
|
|
current project\.
|
2019-11-05 14:55:08 -05:00
|
|
|
.SS See Also
|
2014-09-24 14:41:07 -07:00
|
|
|
.RS 0
|
|
|
|
.IP \(bu 2
|
2020-11-01 07:54:36 +01:00
|
|
|
init\-package\-json module \fIhttp://npm\.im/init\-package\-json\fR
|
2014-09-24 14:41:07 -07:00
|
|
|
.IP \(bu 2
|
2019-11-18 21:01:39 +02:00
|
|
|
npm help package\.json
|
2014-09-24 14:41:07 -07:00
|
|
|
.IP \(bu 2
|
2019-11-18 21:01:39 +02:00
|
|
|
npm help version
|
2015-03-06 02:57:32 -06:00
|
|
|
.IP \(bu 2
|
2019-11-05 14:55:08 -05:00
|
|
|
npm help scope
|
2021-01-28 17:38:39 -05:00
|
|
|
.IP \(bu 2
|
|
|
|
npm help exec
|
2021-04-29 16:30:06 -04:00
|
|
|
.IP \(bu 2
|
|
|
|
npm help workspaces
|
2014-09-24 14:41:07 -07:00
|
|
|
|
|
|
|
.RE
|