Man page - npm-init(1)
Packages contains this manual
- npm-root(1)
- npm-explain(1)
- npm-whoami(1)
- npm-pack(1)
- npm-bugs(1)
- npm-view(1)
- npm-outdated(1)
- npm-update(1)
- npm-query(1)
- npm-logout(1)
- npm-deprecate(1)
- npm-exec(1)
- npm-ping(1)
- npm-shrinkwrap-json(5)
- npm-help(1)
- npm-prefix(1)
- npm-diff(1)
- npm-owner(1)
- npm-install(1)
- npmrc(5)
- npm-uninstall(1)
- npm-hook(1)
- npm-unstar(1)
- npm-docs(1)
- pacote(1)
- npm-restart(1)
- npm-star(1)
- npm-link(1)
- npm-test(1)
- npm-token(1)
- npm-arborist(1)
- npm-profile(1)
- npm-team(1)
- npx(1)
- npm-stars(1)
- npm-adduser(1)
- npm-cache(1)
- npm-edit(1)
- npm-doctor(1)
- npm-help-search(1)
- arborist(1)
- npm-rebuild(1)
- npm-repo(1)
- npm-publish(1)
- npm-start(1)
- npm-unpublish(1)
- npm-dedupe(1)
- npm-init(1)
- npm-dist-tag(1)
- npm-login(1)
- npm-config(1)
- npm(1)
- npm-search(1)
- npm-version(1)
- npm-org(1)
- npm-shrinkwrap(1)
- npm-ls(1)
- package-json(5)
- npm-stop(1)
- npm-install-test(1)
- npm-completion(1)
- npm-run-script(1)
- npm-prune(1)
- npm-access(1)
- npm-ci(1)
- npm-audit(1)
- npm-find-dupes(1)
- npm-install-ci-test(1)
- package-lock-json(5)
- npm-fund(1)
- npm-explore(1)
- npm-pkg(1)
- node-pacote
- node-npmcli-mock-registry
- node-npmcli-node-gyp
- node-libnpmorg
- npm
- node-libnpmteam
- node-npmcli-promise-spawn
- node-libnpmdiff
- node-npmcli-run-script
- node-npmcli-metavuln-calculator
- node-npmcli-smoke-tests
- node-libnpmpublish
- node-qrcode-terminal
- node-npmcli-config
- node-npmcli-git
- node-libnpmaccess
- node-npm-packlist
- node-npmcli-disparity-colors
- node-libnpmexec
- node-npmcli-installed-package-contents
- node-libnpmpack
- node-npmcli-map-workspaces
- node-npmcli-query
- node-npmcli-arborist
- node-npmcli-package-json
- node-libnpmhook
- node-npmcli-name-from-folder
- node-libnpmversion
- node-libnpmfund
- node-libnpmsearch
apt-get install npm
Manual
NPM-INIT
NAMESynopsis
Description
Forwarding additional options
Examples
Workspaces support
Configuration
See Also
NAME
npm-init
Synopsis
<!-- AUTOGENERATED USAGE DESCRIPTIONS -->
Description
npm init
<initializer>
can be used to set up a new or
existing npm
package.
initializer
in this case is an npm package named
create-<initializer>
,
which will be installed by
npm-exec
, and then have
its
main bin executed -- presumably creating or updating
package.json
and
running any other initialization-related operations.
The init command
is transformed to a corresponding
npm exec
operation
as
follows:
|
β’ |
npm init foo -> npm exec create-foo |
|||
|
β’ |
npm init @usr/foo -> npm exec @usr/create-foo |
|||
|
β’ |
npm init @usr -> npm exec @usr/create |
|||
|
β’ |
npm init @usr@2.0.0 -> npm exec @usr/create@2.0.0 |
|||
|
β’ |
npm init @usr/foo@2.0.0 -> npm exec @usr/create-foo@2.0.0 |
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.
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:
|
β’ |
npm init foo@latest # fetches and runs the latest create-foo from |
the registry
|
β’ |
npm init foo@1.2.3 # runs create-foo@1.2.3 specifically |
Forwarding additional options
Any additional options will be passed directly to the command, so npm init foo -- --hello will map to npm exec -- create-foo --hello .
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:
|
β’ |
npm init foo -y --registry=<url> -- --hello -a |
|||
|
β’ |
npm exec -y --registry=<url> -- create-foo --hello -a |
Examples
Create a new
React-based project using
create-react-app
:
$ npm init react-app ./my-react-app
Create a new
esm
-compatible package using
create-esm
:
$ mkdir my-esm-lib &&
cd my-esm-lib
$ npm init esm --yes
Generate a plain old package.json using legacy init:
$ mkdir my-npm-pkg &&
cd my-npm-pkg
$ git init
$ npm init
Generate it without having it ask any questions:
$ npm init -y
Workspaces support
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.
Given a project with no workspaces, e.g:
.
+-- package.json
You may generate a new workspace using the legacy init:
$ npm init -w packages/a
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:
.
+-- package.json
β-- packages
β-- a
β-- package.json
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
, 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:
npm init -w packages/my-react-app react-app .
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 .
:
.
+-- package.json
β-- packages
+-- a
| β-- package.json
β-- my-react-app
+-- README
+-- package.json
β-- ...
Configuration
<!-- AUTOGENERATED CONFIG DESCRIPTIONS -->
See Also
|
β’ |
package spec |
|||
|
β’ |
init-package-json module |
|||
|
β’ |
package.json |
|||
|
β’ |
npm version |
|||
|
β’ |
npm scope |
|||
|
β’ |
npm exec |
|||
|
β’ |
npm workspaces |