npm-init

创建一个 package.json 文件

选择命令行版本:

概要

🌐 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 包。

在这种情况下,initializer 是一个名为 create-<initializer> 的 npm 包,它将由 npm-exec 安装,然后执行其主要的二进制文件——大概会创建或更新 package.json 并运行任何其他与初始化相关的操作。

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 将回退到旧的初始化行为。它会问你一堆问题,然后为你写一个 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@1.2.3 # 特别运行 create-foo@1.2.3

转发附加选项

🌐 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 和一个创建包的,下面两个命令是等效的:

🌐 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

工作区初始化同样支持 npm init <initializer> -w <dir> 语法,遵循本页最初 描述 部分中解释的一套规则。类似于之前使用 create-react-app 创建基于 React 的新项目的示例,以下语法将确保在你的项目中将新的 React 应用创建为嵌套的 工作区,并配置你的 package.json 以识别它:

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

  • 默认值:
  • 类型:字符串

默认情况下,应该使用 npm init 作为软件包作者的名称。

🌐 The value npm init should use by default for the package author's name.

init-author-url

  • 默认值:
  • 类型:"" 或 URL

npm init 应默认用于软件包作者的主页。

🌐 The value npm init should use by default for the package author's homepage.

init-license

  • 默认: "ISC"
  • 类型:字符串

包许可默认应使用值 npm init

🌐 The value npm init should use by default for the package license.

init-module

  • 默认: "~/.npm-init.js"
  • 类型:路径

一个将由 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"
  • 类型:SemVer 字符串

npm init 应默认使用的包版本号值,如果在 package.json 中尚未设置的话。

🌐 The value that npm init should use by default for the package version number, if not already set in package.json.

yes

  • 默认值:空
  • 类型:空或布尔值

自动对 npm 在命令行上可能显示的任何提示回答“是”。

🌐 Automatically answer "yes" to any prompts that npm might print on the command line.

force

  • 默认:否
  • 类型:布尔

删除了针对不幸的副作用、常见错误、不必要的性能下降和恶意输入的各种保护。

🌐 Removes various protections against unfortunate side effects, common mistakes, unnecessary performance degradation, and malicious input.

  • 允许在全局安装中破坏非 npm 文件。
  • 允许 npm version 命令在不干净的 git 仓库上运行。
  • 允许使用 npm cache clean 删除缓存文件夹。
  • 允许安装具有 engines 声明且需要不同版本 npm 的包。
  • 允许安装具有 engines 声明且需要不同版本的 node 的软件包,即使启用了 --engine-strict
  • 允许 npm audit fix 安装超出你指定依赖范围的模块(包括 SemVer 主版本更改)。
  • 允许取消发布已发布包的所有版本。
  • 允许在根项目中安装冲突的 peerDependencies。
  • npm init 期间隐式设置 --yes
  • 允许覆盖 npm pkg 中的现有值
  • 允许取消发布整个包(不仅仅是单个版本)。

如果你对自己想要做什么没有明确的想法,强烈建议你不要使用此选项!

🌐 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

  • 默认:当前项目的范围(如果有)或
  • 类型:字符串

将操作与范围注册表的作用域相关联。

🌐 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 registry
npm login --scope=@mycorp --registry=https://registry.mycorp.com
# log out, removing the link and the auth token
npm 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

  • 默认值:
  • 类型:字符串(可以多次设置)

启用在当前项目的已配置工作区的上下文中运行命令,同时通过仅运行此配置选项定义的工作区进行过滤。

🌐 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:

  • 工作区名称
  • 工作区目录的路径
  • 父工作区目录的路径(将导致选择该文件夹中的所有工作区)

对于 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

  • 默认值:空
  • 类型:空或布尔值

设置为 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 配置中指定了一个或多个工作区。

此值不会导出到子进程的环境中。

🌐 This value is not exported to the environment for child processes.

workspaces-update

  • 默认:是
  • 类型:布尔

如果设置为 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

  • 默认:否
  • 类型:布尔

为命令启用工作区时包括工作区根。

🌐 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