目录
选择命令行版本:
目录
概要
¥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>
可用于设置新的或现有的 npm 包。
¥npm init <initializer>
can be used to set up a new or existing npm package.
在这种情况下,initializer
是一个名为 create-<initializer>
的 npm 包,它将由 npm-exec
安装,然后执行其主 bin —— 大概是创建或更新 package.json
并运行任何其他与初始化相关的操作。
¥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.
init 命令转化为对应的 npm exec
操作如下:
¥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
如果初始化器被省略(通过调用 npm init
),init 将回退到旧的 init 行为。它会问你一堆问题,然后为你写一个 package.json。它将尝试根据现有字段、依赖和选择的选项进行合理的猜测。它是严格附加的,因此它将保留已设置的所有字段和值。你也可以使用 -y
/--yes
完全跳过问卷。如果你通过 --scope
,它将创建一个范围包。
¥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.
注意:如果用户已经全局安装了 create-<initializer>
软件包,那么 npm init
将使用该软件包。如果你希望 npm 使用最新版本或其他特定版本,你必须指定它:
¥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
# 从注册表中获取并运行最新的create-foo
¥
npm init foo@latest
# fetches and runs the latestcreate-foo
from the registry -
npm init foo@1.2.3
# 专门运行create-foo@1.2.3
¥
npm init foo@1.2.3
# runscreate-foo@1.2.3
specifically
转发附加选项
¥Forwarding additional options
任何附加选项都将直接传递给命令,因此 npm init foo -- --hello
将映射到 npm exec -- create-foo --hello
。
¥Any additional options will be passed directly to the command, so npm init foo -- --hello
will map to npm exec -- create-foo --hello
.
为了更好地说明如何转发选项,这里有一个更完善的示例,显示传递给 npm cli 和 create package 的选项,以下两个命令是等效的:
¥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-react-app
创建一个新的基于 React 的项目:
¥Create a new React-based project using create-react-app
:
$ npm init react-app ./my-react-app
使用 create-esm
创建一个新的 esm
兼容包:
¥Create a new esm
-compatible package using create-esm
:
$ mkdir my-esm-lib && cd my-esm-lib$ npm init esm --yes
使用旧版 init 生成一个普通的旧 package.json:
¥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
可以使用 workspace
配置选项在项目中创建新工作区。使用 npm init -w <dir>
时,cli 将创建所需的文件夹和样板,同时添加对项目 package.json
"workspaces": []
属性的引用,以确保新生成的工作区正确设置。
¥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
你可以使用旧版 init 生成新工作区:
¥You may generate a new workspace using the legacy init:
$ npm init -w packages/a
这将生成一个新文件夹和 package.json
文件,同时更新你的顶层 package.json
以添加对这个新工作区的引用:
¥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
工作区 init 还支持 npm init <initializer> -w <dir>
语法,遵循本页初始描述部分前面解释的同一组规则。与前面使用 create-react-app
创建新的基于 React 的项目的示例类似,以下语法将确保将新的 React 应用创建为项目中的嵌套工作区,并配置 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 .
这将确保按预期生成你的 React 应用,要牢记的一个重要考虑因素是 npm exec
将在为该工作区新创建的文件夹的上下文中运行,这就是为什么在此示例中初始化程序使用 初始化程序名称后跟一个点以表示该上下文中的当前目录,例如: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
yes
-
默认值:null
¥Default: null
-
类型:空值或布尔值
¥Type: null or Boolean
对 npm 可能在命令行上打印的任何提示自动回答 "yes"。
¥Automatically answer "yes" to any prompts that npm might print on the command line.
force
-
默认值:false
¥Default: false
-
类型:布尔值
¥Type: Boolean
删除了针对不幸的副作用、常见错误、不必要的性能下降和恶意输入的各种保护。
¥Removes various protections against unfortunate side effects, common mistakes, unnecessary performance degradation, and malicious input.
-
允许在全局安装中破坏非 npm 文件。
¥Allow clobbering non-npm files in global installs.
-
允许
npm version
命令在不干净的 git 存储库上工作。¥Allow the
npm version
command to work on an unclean git repository. -
允许使用
npm cache clean
删除缓存文件夹。¥Allow deleting the cache folder with
npm cache clean
. -
允许安装具有
engines
声明需要不同版本的 npm 的包。¥Allow installing packages that have an
engines
declaration requiring a different version of npm. -
允许安装具有
engines
声明需要不同版本node
的包,即使启用了--engine-strict
。¥Allow installing packages that have an
engines
declaration requiring a different version ofnode
, even if--engine-strict
is enabled. -
允许
npm audit fix
安装超出你声明的依赖范围的模块(包括 SemVer 的主要更改)。¥Allow
npm audit fix
to install modules outside your stated dependency range (including SemVer-major changes). -
允许取消发布已发布包的所有版本。
¥Allow unpublishing all versions of a published package.
-
允许在根项目中安装冲突的 peerDependencies。
¥Allow conflicting peerDependencies to be installed in the root project.
-
在
npm init
时隐式设置--yes
。¥Implicitly set
--yes
duringnpm init
. -
允许破坏
npm pkg
中的现有值¥Allow clobbering existing values in
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
这将导致 @mycorp
映射到注册表,以便将来安装根据模式 @mycorp/package
指定的包。
¥This will cause @mycorp
to be mapped to the registry for future installation of packages specified according to the pattern @mycorp/package
.
这也将导致 npm init
创建一个范围包。
¥This will also cause npm 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
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.
workspace
配置的有效值为:
¥Valid values for the workspace
config are either:
-
工作区名称
¥Workspace names
-
工作区目录的路径
¥Path to a workspace directory
-
父工作区目录的路径(将导致选择该文件夹中的所有工作区)
¥Path to a parent workspace directory (will result in selecting all workspaces within that folder)
为 npm init
命令设置时,可以将其设置为尚不存在的工作区的文件夹,以创建文件夹并将其设置为项目中的全新工作区。
¥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.
此值不会导出到子进程的环境中。
¥This value is not exported to the environment for child processes.
workspaces
-
默认值:null
¥Default: null
-
类型:空值或布尔值
¥Type: null or Boolean
设置为 true 可在所有已配置工作区的上下文中运行该命令。
¥Set to true to run the command in the context of all configured workspaces.
显式将此设置为 false 将导致像 install
这样的命令完全忽略工作区。未明确设置时:
¥Explicitly setting this to false will cause commands like install
to ignore workspaces altogether. When not set explicitly:
-
在
node_modules
树上运行的命令(安装、更新等)会将工作区链接到node_modules
文件夹。* 执行其他操作(测试、执行、发布等)的命令将在根项目上运行,除非在workspace
配置中指定了一个或多个工作区。¥Commands that operate on the
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-update
-
默认值:true
¥Default: true
-
类型:布尔值
¥Type: Boolean
如果设置为 true,npm cli 将在可能更改安装到 node_modules
文件夹的工作区的操作之后运行更新。
¥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.
include-workspace-root
-
默认值:false
¥Default: false
-
类型:布尔值
¥Type: Boolean
为命令启用工作区时包括工作区根。
¥Include the workspace root when workspaces are enabled for a command.
当为 false 时,通过 workspace
配置指定单个工作区,或通过 workspaces
标志指定所有工作区,将导致 npm 仅在指定的工作区上运行,而不是在根项目上运行。
¥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.
此值不会导出到子进程的环境中。
¥This value is not exported to the environment for child processes.
也可以看看
¥See Also