脚本
选择命令行版本:
See Details
目录
描述
¥Description
package.json 文件的 "scripts" 属性支持许多内置脚本及其预设的生命周期事件以及任意脚本。所有这些都可以通过运行 npm run <stage> 来执行。具有匹配名称的前置和后置命令也将为这些运行(例如 premyscript、myscript、postmyscript)。来自依赖的脚本可以使用 npm explore <pkg> -- npm run <stage> 运行。
¥The "scripts" property of your package.json file supports a number of built-in scripts and their preset life cycle events as well as arbitrary scripts. These all can be executed by running npm run <stage>. Pre and post commands with matching names will be run for those as well (e.g. premyscript, myscript, postmyscript). Scripts from dependencies can be run with npm explore <pkg> -- npm run <stage>.
前后脚本
¥Pre & Post Scripts
要为 package.json 的 "scripts" 部分中定义的任何脚本创建 "pre" 或 "post" 脚本,只需创建另一个具有匹配名称的脚本并将 "pre" 或 "post" 添加到它们的开头。
¥To create "pre" or "post" scripts for any scripts defined in the "scripts" section of the package.json, simply create another script with a matching name and add "pre" or "post" to the beginning of them.
{"scripts": {"precompress": "{{ executes BEFORE the `compress` script }}","compress": "{{ run command to compress files }}","postcompress": "{{ executes AFTER `compress` script }}"}}
在此示例中,npm run compress 将按照所述执行这些脚本。
¥In this example npm run compress would execute these scripts as described.
生命周期脚本
¥Life Cycle Scripts
有一些特殊的生命周期脚本只在某些情况下发生。除了 pre<event>、post<event> 和 <event> 脚本之外,还会出现这些脚本。
¥There are some special life cycle scripts that happen only in certain situations. These scripts happen in addition to the pre<event>, post<event>, and <event> scripts.
prepare,prepublish,prepublishOnly,prepack,postpack,dependencies
prepare (since npm@4.0.0)
-
在打包之前运行,即在
npm publish和npm pack期间¥Runs BEFORE the package is packed, i.e. during
npm publishandnpm pack -
在本地
npm install上运行,不带包参数(可以像--production或--omit=dev那样使用标志运行,但在安装特定包(例如npm install express)时不会运行)¥Runs on local
npm installwithout package arguments (runs with flags like--productionor--omit=dev, but does not run when installing specific packages likenpm install express) -
在
prepublishOnly和prepack之后、postpack之前运行¥Runs AFTER
prepublishOnlyandprepack, but BEFOREpostpack -
如果软件包通过
npm install <folder>作为链接安装,则运行该软件包¥Runs for a package if it's being installed as a link through
npm install <folder> -
注意:如果通过 git 安装的包包含
prepare脚本,则在打包和安装包之前,将安装其dependencies和devDependencies,并运行准备脚本。¥NOTE: If a package being installed through git contains a
preparescript, itsdependenciesanddevDependencieswill be installed, and the prepare script will be run, before the package is packaged and installed. -
自
npm@7起,这些脚本在后台运行。要查看输出,请运行:--foreground-scripts。¥As of
npm@7these scripts run in the background. To see the output, run with:--foreground-scripts. -
工作区中,准备脚本会在所有包中并发运行。如果你有相互依赖的包,并且其中一个包必须先于另一个包构建,请考虑使用
--foreground-scripts(可以在.npmrc中使用foreground-scripts=true设置)按顺序运行脚本,或者采用不同的构建结构。¥In workspaces, prepare scripts run concurrently across all packages. If you have interdependent packages where one must build before another, consider using
--foreground-scripts(which can be set in.npmrcwithforeground-scripts=true) to run scripts sequentially, or structure your build differently.
prepublish (DEPRECATED)
-
在
npm publish期间不运行,但在npm ci和npm install期间运行。有关更多信息,请参见下文。¥Does not run during
npm publish, but does run duringnpm ciandnpm install. See below for more info.
prepublishOnly
-
在封装准备和封装之前运行,仅在
npm publish上运行。¥Runs BEFORE the package is prepared and packed, ONLY on
npm publish.
prepack
-
在打包 tarball 之前运行(在“
npm pack”、“npm publish”上以及安装 git 依赖时)。¥Runs BEFORE a tarball is packed (on "
npm pack", "npm publish", and when installing a git dependency). -
注意:“
npm run pack”与“npm pack”不同。“npm run pack”是用户自定义的脚本名称,而“npm pack”是命令行接口 (CLI) 定义的命令。¥NOTE: "
npm run pack" is NOT the same as "npm pack". "npm run pack" is an arbitrary user defined script name, whereas, "npm pack" is a CLI defined command.
postpack
-
在生成 tarball 之后但在它被移动到最终目的地之前运行(如果有的话,publish 不会在本地保存 tarball)
¥Runs AFTER the tarball has been generated but before it is moved to its final destination (if at all, publish does not save the tarball locally)
dependencies
-
如果发生更改,则在修改
node_modules目录的任何操作之后运行。¥Runs AFTER any operations that modify the
node_modulesdirectory IF changes occurred. -
不在全局模式下运行
¥Does NOT run in global mode
准备和预发布
¥Prepare and Prepublish
弃用注意事项:prepublish
¥Deprecation Note: prepublish
从 npm@1.1.71 开始,npm CLI 已经为 npm publish 和 npm install 运行了 prepublish 脚本,因为它是一种方便的方式来准备一个包以供使用(一些常见的用例在下面的部分中描述)。在实践中,它也被证明是 非常混乱。从 npm@4.0.0 开始,引入了一个新事件 prepare,它保留了这种现有行为。添加了一个新事件 prepublishOnly 作为过渡策略,以允许用户避免现有 npm 版本的混乱行为,并且只在 npm publish 上运行(例如,最后一次运行测试以确保它们处于良好状态)。
¥Since npm@1.1.71, the npm CLI has run the prepublish script for both npm publish and npm install, because it's a convenient way to prepare a package for use (some common use cases are described in the section below). It has also turned out to be, in practice, very confusing. As of npm@4.0.0, a new event has been introduced, prepare, that preserves this existing behavior. A new event, prepublishOnly has been added as a transitional strategy to allow users to avoid the confusing behavior of existing npm versions and only run on npm publish (for instance, running the tests one last time to ensure they're in good shape).
有关此更改的详细说明,请参阅 https://github.com/npm/npm/issues/10074,并进一步阅读。
¥See https://github.com/npm/npm/issues/10074 for a much lengthier justification, with further reading, for this change.
用例
¥Use Cases
使用 prepare 脚本执行平台无关且需要在软件包使用前运行的构建任务。这包括以下任务:
¥Use a prepare script to perform build tasks that are platform-independent and need to run before your package is used. This includes tasks such as:
-
将 TypeScript 或其他源代码编译为 JavaScript。
¥Compiling TypeScript or other source code into JavaScript.
-
创建 JavaScript 源代码的缩小版本。
¥Creating minified versions of JavaScript source code.
-
获取你的包将使用的远程资源。
¥Fetching remote resources that your package will use.
在 prepare 脚本中运行这些构建任务可确保它们只在单个位置执行一次,从而降低复杂性和可变性。此外,这意味着:
¥Running these build tasks in the prepare script ensures they happen once, in a single place, reducing complexity and variability. Additionally, this means that:
-
你可以将构建工具依赖为
devDependencies,这样你的用户就不需要安装这些工具。¥You can depend on build tools as
devDependencies, and thus your users don't need to have them installed. -
你不需要在你的包中包含缩小器,从而为你的用户减少大小。
¥You don't need to include minifiers in your package, reducing the size for your users.
-
你不需要依赖你的用户在目标机器上拥有
curl或wget或其他系统工具。¥You don't need to rely on your users having
curlorwgetor other system tools on the target machines.