npm-init
Create a package.json file
Synopsis
npm init <package-spec> (same as `npx <package-spec>`)npm init <@scope> (same as `npx <@scope>/create`)aliases: create, innit

Description描述
npm init <initializer>
can be used to set up a new or existing npm package.可以用于设置新的或现有的npm包。
本例中的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.initializer
是一个名为create-<initializer>
的npm包,它将由npm-exec安装,然后执行其主bin——大概是创建或更新package.json
,并运行任何其他与初始化相关的操作。
The init command is transformed to a corresponding init命令转换为相应的npm exec
operation as follows:npm exec
操作,如下所示:
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. npm init
),init将返回到遗留的init行为。It will ask you a bunch of questions, and then write a package.json for you. 它会问你一堆问题,然后为你写一个package.json。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. -y
/--yes
来完全跳过问卷。If you pass 如果您传递--scope
, it will create a scoped package.--scope
,它将创建一个有作用域的包。
Note: if a user already has the 如果用户已经全局安装了create-<initializer>
package globally installed, that will be what npm init
uses. create-<initializer>
包,那么npm init
就会使用该包。 If you want npm to use the latest version, or another specific version you must specify it:如果您希望npm使用最新版本或其他特定版本,则必须指定它:
npm init foo@latest
#fetches and runs the latest从注册表中获取并运行最新的create-foo
from the registrycreate-foo
npm init foo@1.2.3
# runscreate-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
.npm init foo -- --hello
将映射到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 cli和创建包的选项,以下两个命令是等效的:
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:使用create-react-app创建一个新的基于React的项目:
$ npm init react-app ./my-react-app
Create a new 使用create-esm创建新的esm
-compatible package using create-esm:esm
兼容包:
$ mkdir my-esm-lib && cd my-esm-lib$ npm init esm --yes
Generate a plain old package.json using legacy init:使用遗留init生成一个普通的旧包json:
$ 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. workspace
。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.npm init -w <dir>
时,cli将创建所需的文件夹和样板文件,同时添加对项目package.json
"workspaces": []
属性的引用,以确保新生成的工作区正确设置。
Given a project with no workspaces, e.g:给定一个没有工作区的项目,例如:
.+-- package.json
You may generate a new workspace using the legacy init:您可以使用遗留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
文件,同时更新您的顶级package.json
,以添加对这个新工作区的引用:
.+-- package.json`-- packages`-- a`-- package.json
The workspaces init also supports the 工作区init还支持npm init <initializer> -w <dir>
syntax, following the same set of rules explained earlier in the initial Description section of this page. npm init <initializer> -w <dir>
语法,遵循本页前面初始描述部分中解释的相同规则集。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 与前面使用create-react-app创建新的基于React的项目的示例类似,以下语法将确保在项目中创建新的React应用程序作为嵌套工作区,并配置您的package.json
to recognize it as such:package.json
以识别它:
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 这将确保按预期生成react应用程序,需要考虑的一个重要因素是,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 .
:npm exec
将在该工作区新创建的文件夹的上下文中运行,这就是为什么在本例中,初始值设定项使用后跟句点的初始值设定器名称来表示该上下文中的当前目录,例如:react-app .
:
.+-- package.json`-- packages+-- a| `-- package.json`-- my-react-app+-- README+-- package.json`-- ...
Configuration

yes
- Default: null
- Type: null or Boolean
Automatically answer "yes" to any prompts that npm might print on the command line.对于npm可能在命令行上打印的任何提示,自动回答“是”。

force
- Default: false
Type: Boolean类型:布尔
Removes various protections against unfortunate side effects, common mistakes, unnecessary performance degradation, and malicious input.删除针对不幸的副作用、常见错误、不必要的性能下降和恶意输入的各种保护。
Allow clobbering non-npm files in global installs.允许在全局安装中删除非npm文件。Allow the允许npm version
command to work on an unclean git repository.npm version
命令在不干净的git存储库上工作。Allow deleting the cache folder with允许使用npm cache clean
.npm cache clean
删除缓存文件夹。Allow installing packages that have an允许安装具有需要不同版本npm的engines
declaration requiring a different version of npm.engines
声明的程序包。Allow installing packages that have an允许安装具有需要不同版本engines
declaration requiring a different version ofnode
, even if--engine-strict
is enabled.node
的engines
声明的包,即使启用了--engine-strict
。Allow允许npm audit fix
to install modules outside your stated dependency range (including SemVer-major changes).npm audit fix
程序安装您声明的依赖关系范围之外的模块(包括SemVer的主要更改)。Allow unpublishing all versions of a published package.允许取消发布已发布包的所有版本。Allow conflicting peerDependencies to be installed in the root project.允许在根项目中安装冲突的对等依赖项。Implicitly set在--yes
duringnpm init
.npm init
期间隐含地设置--yes
。Allow clobbering existing values in允许破坏npm pkg
npm pkg
中的现有值Allow unpublishing of entire packages (not just a single version).允许取消发布整个包(而不仅仅是单个版本)。
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!如果您不清楚自己想做什么,强烈建议您不要使用此选项!

scope
Default: the scope of the current project, if any, or ""默认值:当前项目的范围(如果有),或“”Type: String类型:字符串
Associate 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 tokennpm logout --scope=@mycorp
This will cause 这将导致@mycorp
to be mapped to the registry for future installation of packages specified according to the pattern @mycorp/package
.@mycorp
映射到注册表,以便将来安装根据@mycorp/package
模式指定的包。
This will also cause 这也将导致npm init
to create a scoped package.npm init
创建一个作用域的包。
# accept all defaults, and create a package named "@foo/whatever",# instead of just named "whatever"npm init --scope=@foo --yes

workspace
Default:默认值:Type: String (can be set multiple times)类型:字符串(可以多次设置)
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.启用在当前项目的已配置工作区的上下文中运行命令,同时通过仅运行此配置选项定义的工作区进行筛选。
Valid values for the workspace
config are either:workspace
配置的有效值为:
Workspace names工作区名称Path to a workspace directory工作区目录的路径Path to a parent workspace directory (will result in selecting all workspaces within that folder)父工作区目录的路径(将导致选择该文件夹中的所有工作区)
When set for the 当为npm 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.npm init
命令设置时,可以将其设置为尚未存在的工作空间的文件夹,以创建文件夹并将其设置成项目中全新的工作空间。
This value is not exported to the environment for child processes.此值不会导出到子进程的环境中。

workspaces
- Default: null
Type: null or Boolean类型:null
或Boolean
Set to true to run the command in the context of all configured workspaces.设置为true
可在所有配置的工作空间的上下文中运行该命令。
Explicitly setting this to false will cause commands like 将其明确设置为install
to ignore workspaces altogether. false
将导致install
等命令完全忽略工作区。When not set explicitly:如果未明确设置:
Commands that operate on the在node_modules
tree (install, update, etc.) will link workspaces into thenode_modules
folder.node_modules
树上操作的命令(安装、更新等)将工作区链接到node_modules
文件夹中。- Commands that do other things (test, exec, publish, etc.) will operate on the root project, unless one or more workspaces are specified in the-执行其他操作(测试、执行、发布等)的命令将在根项目上运行,除非在workspace
config.workspace
配置中指定了一个或多个工作区。
This value is not exported to the environment for child processes.此值不会导出到子进程的环境中。

workspaces-update
- Default: true
- Type: Boolean
If set to true, the npm cli will run an update after operations that may possibly change the workspaces installed to the 如果设置为node_modules
folder.true
,则npm-cli将在可能更改安装到node_modules
文件夹中的工作区的操作之后运行更新。

include-workspace-root
- Default: false
- Type: Boolean
Include the workspace root when workspaces are enabled for a command.为命令启用工作空间时,包括工作空间根。
When false, specifying individual workspaces via the 如果为workspace
config, or all workspaces via the workspaces
flag, will cause npm to operate only on the specified workspaces, and not on the root project.false
,则通过workspace
配置指定单个工作区,或通过workspace
标志指定所有工作区,将导致npm仅在指定的工作区上操作,而不在根项目上操作。
This value is not exported to the environment for child processes.此值不会导出到子进程的环境中。