npm-init
选择命令行版本:
See Details
目录
概要
¥Synopsis
npm init <package-spec> (same as `npx create-<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
init-author-name
-
默认值:""
¥Default: ""
-
类型:字符串
¥Type: String
默认情况下,应该使用值 npm init
作为包作者的名称。
¥The value npm init
should use by default for the package author's name.
init-author-url
-
默认值:""
¥Default: ""
-
类型:"" 或网址
¥Type: "" or URL
默认情况下,包作者的主页应使用值 npm init
。
¥The value npm init
should use by default for the package author's homepage.
init-license
-
默认值:"ISC"
¥Default: "ISC"
-
类型:字符串
¥Type: String
默认情况下,值 npm init
应用于包许可证。
¥The value npm init
should use by default for the package license.
init-module
-
默认值:"~/.npm-init.js"
¥Default: "~/.npm-init.js"
-
类型:路径
¥Type: Path
将由 npm init
命令加载的模块。有关详细信息,请参阅 init-package-json 模块的文档或 npm init。
¥A module that will be loaded by the npm init
command. See the documentation for the init-package-json module for more information, or npm init.
init-version
-
默认值:"1.0.0"
¥Default: "1.0.0"
-
类型:SemVer 字符串
¥Type: SemVer string
如果 package.json 中尚未设置,npm init
应该默认使用的值作为包版本号。
¥The value that npm init
should use by default for the package version number, if not already set in package.json.
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