tl;dr
-
The cause问题在于gulp
(以及显示此消息的任何其他命令)实际上是批处理文件 (*.cmd
, *.bat
)和行为是内置于cmd.exe
,批处理文件的解释器。
-
包管理器越来越多地提供电源外壳脚本(*.ps1
)(请参阅底部部分),这可以绕过从 PowerShell 运行时的问题。
The 行为既不是由 PowerShell 引起的,PowerShell 也无法更改它(证明了不包含提示信息)。
The 行为是内置于cmd.exe
- 在本例中,Powershell 正在调用.cmd
文件(批处理文件),其解释为cmd.exe
.
-
如果您显式控制目标可执行文件的调用,则可以通过移动到 Powershell 来修复此问题- 请注意,这有其自身的考虑因素,请参见下文。
-
If you do not explicitly control the invocation of the target executable, you're out of luck (unless you're willing to install third-party cmd.exe
replacements) and must press Ctrl+C twice in order to terminate execution.
-
A[不明智]解决方法 is to 修改cmd.exe
binary - see 文章包含有关如何修补的说明cmd.exe可执行文件以抑制提示 https://itsme.home.xs4all.nl/projects/misc/patching-cmdexe.html。此外,您可以在以下位置发布功能请求:GitHub https://github.com/Microsoft/console请求从源头修复此行为,尽管出于向后兼容性的原因这不太可能发生。
演示该行为:
这些示例假设已安装 Node.js 并且node.exe
因此在你的路径中:
First, invoke node.exe
directly, with a tight loop that requires you to press Ctrl+C to terminate the process.
PS> node -e "while (true);"
As you'll see, pressing Ctrl+C instantly terminates the process - no confirmation prompt.
现在,让我们创建一个调用相同命令的示例批处理文件并调用该批处理文件:
PS> "@echo off`nnode -e `"while (true);`"" | Set-Content test.cmd
PS> ./test.cmd
As you'll see, pressing Ctrl+C now presents the undesired Terminate batch job (Y/N)?
prompt. (You'd get the same behavior if you ran the batch file from cmd.exe
.)
为了证明gulp
is a .cmd
(批处理)文件:
你说你正在通过以下方式运行你的命令gulp https://www.npmjs.com/package/gulp's CLI.
在 Windows 上,入口点为gulp
CLI is gulp.cmd
[请参阅底部部分的更新] - 即,一个批处理文件.这就是它的一般工作原理npm
-package“二进制文件”(可执行文件)实现为 JS 文件或 shell 脚本。
That gulp
调用gulp.cmd
可以通过如下方式验证:
# Execute from a project folder that has `gulp` installed as a dependency.
# If `gulp` is installed *globally*
# Note: CLI `npx` requires npm version 5.2.0+
PS C:\some\NodeJs\project> npx where gulp
你会看到类似的东西:
C:\some\NodeJs\project\node_modules\.bin\gulp
C:\some\NodeJs\project\node_modules\.bin\gulp.cmd
注意where.exe
还列出了无扩展 Unix- 外壳脚本,...\gulp
;然而,从cmd.exe
/powershell这样的shell脚本是不能直接执行的,它是...\gulp.cmd
- the 批处理文件- 已执行。
(如果有疑问,请输入命令,例如@set /p dummy="Press a key"
在开始时gulp.cmd
文件,您将看到当您调用时会执行此命令gulp
没有.cmd
扩大。
另请注意,有no gulp.exe
.)
更一般地,在 Windows 上,一个项目的node_modules\.bin
子文件夹包含pairs项目本地依赖的包附带的 CLI 的 CLI 入口点:
-
node_modules\.bin\<some-cli>
是 Unix shell 脚本(其执行解释器通过其控制舍邦线 https://stackoverflow.com/a/33510581/45375).
-
node_modules\.bin\<some-cli>.cmd
是辅助批处理文件Windows.
更新和未来考虑:
在 npm 模块的上下文中,如果电源外壳脚本 (*.ps1
) 被用作 Windows 上的帮助程序脚本。有门票可以去npm
, yarn
和类似的软件来做到这一点。也有一些缺点:
被叫时来自 PowerShell, a *.ps1
文件将被找到并在进程中运行, so 一个可能的解决方案是npm
项目到also提供*.ps1
帮助脚本,这将优先于*.cmd
同名的文件。
-
Update:
- 最新版本
npm
(在 6.14.10 中验证)确实要安装这样的*.ps1
files.
- 替代包管理器yarn https://yarnpkg.com/, since v2似乎根本不再使用批处理文件,所以原来的问题是bypassed there; (v1相比之下,仍然使用批处理文件(仅);从 v1 升级必须在每个项目基础见迁移说明 https://yarnpkg.com/getting-started/migration).