Select CLI Version:
npm init<package-spec>(same as`npx create-<package-spec>`)npm init<@scope>(same as`npx<@scope>/create`)aliases: create, innit
npm init <initializer> can be used to set up a new or existing npm package.
initializer in this case is an npm package namedcreate-<initializer>, which will be installed bynpm-exec, and then have its main bin executed -- presumably creating or updatingpackage.json and running any other initialization-related operations.
The init command is transformed to a correspondingnpm exec operation as follows:
npm init foo ->npm exec create-foonpm init @usr/foo ->npm exec @usr/create-foonpm init @usr ->npm exec @usr/createnpm init @usr@2.0.0 ->npm exec @usr/create@2.0.0npm init @usr/foo@2.0.0 ->npm exec @usr/create-foo@2.0.0If the initializer is omitted (by just callingnpm 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 thecreate-<initializer> package globally installed, that will be whatnpm 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 latestcreate-foo from the registrynpm init foo@1.2.3 # runscreate-foo@1.2.3 specificallyAny additional options will be passed directly to the command, sonpm init foo -- --hello will map tonpm exec -- create-foo --hello.
To better illustrate how options are forwarded, here's a more evolved example showing options passed to both thenpm cli and a create package, both following commands are equivalent:
npm init foo -y --registry=<url> -- --hello -anpm exec -y --registry=<url> -- create-foo --hello -aCreate a new React-based project usingcreate-react-app:
$npm init react-app ./my-react-app
Create a newesm-compatible package usingcreate-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
Set the private flag totrue in package.json:
$npm init --init-private-y
It's possible to create a new workspace within your project by using theworkspace config option. When usingnpm init -w <dir> the cli will create the folders and boilerplate expected while also adding a reference to your projectpackage.json"workspaces": [] property in order to make sure that new generatedworkspace 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 andpackage.json file, while also updating your top-levelpackage.json to add the reference to this new workspace:
.+-- package.json`-- packages`-- a`-- package.json
The workspaces init also supports thenpm init <initializer> -w <dir> syntax, following the same set of rules explained earlier in the initialDescription section of this page. Similar to the previous example of creating a new React-based project usingcreate-react-app, the following syntax will make sure to create the new react app as a nestedworkspace within your project and configure yourpackage.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 thatnpm 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`--...
init-author-nameThe valuenpm init should use by default for the package author's name.
init-author-urlThe valuenpm init should use by default for the package author's homepage.
init-licenseThe valuenpm init should use by default for the package license.
init-moduleA module that will be loaded by thenpm init command. See the documentation for theinit-package-json module for more information, ornpm init.
init-typeThe value thatnpm init should use by default for the package.json type field.
init-versionThe value thatnpm init should use by default for the package version number, if not already set in package.json.
init-privateThe valuenpm init should use by default for the package's private flag.
yesAutomatically answer "yes" to any prompts that npm might print on the command line.
forceRemoves various protections against unfortunate side effects, common mistakes, unnecessary performance degradation, and malicious input.
npm version command to work on an unclean git repository.npm cache clean.engines declaration requiring a different version of npm.engines declaration requiring a different version ofnode, even if--engine-strict is enabled.npm audit fix to install modules outside your stated dependency range (including SemVer-major changes).--yes duringnpm init.npm pkgIf you don't have a clear idea of what you want to do, it is strongly recommended that you do not use this option!
scopeAssociate an operation with a scope for a scoped registry.
Useful when logging in to or out of a private registry:
# log in, linking the scope to the custom registrynpm login--scope=@mycorp--registry=https://registry.mycorp.com# log out, removing the link and the auth tokennpmlogout--scope=@mycorp
This will cause@mycorp to be mapped to the registry for future installation of packages specified according to the pattern@mycorp/package.
This will also causenpm init to create a scoped package.
# accept all defaults, and create a package named "@foo/whatever",# instead of just named "whatever"npm init--scope=@foo--yes
workspaceEnable 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.
Valid values for theworkspace config are either:
When set for thenpm 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.
This value is not exported to the environment for child processes.
workspacesSet to true to run the command in the context ofall configured workspaces.
Explicitly setting this to false will cause commands likeinstall to ignore workspaces altogether. When not set explicitly:
node_modules tree (install, update, etc.) will link workspaces into thenode_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 theworkspace config.This value is not exported to the environment for child processes.
workspaces-updateIf set to true, the npm cli will run an update after operations that may possibly change the workspaces installed to thenode_modules folder.
include-workspace-rootInclude the workspace root when workspaces are enabled for a command.
When false, specifying individual workspaces via theworkspace config, or all workspaces via theworkspaces flag, will cause npm to operate only on the specified workspaces, and not on the root project.
This value is not exported to the environment for child processes.