React:CMD 文件到 运行 应用程序

React: CMD File to Run Application

我正在尝试编写一个 .cmd 文件来安装依赖项,然后 运行 我创建的 React 应用程序。经过研究,我在 .cmd 文件中有以下代码:

@echo off
npm install
pause
npm run dev

npm install 安装依赖项,暂停后,npm 运行 dev 启动应用程序。无论出于何种原因,在 npm install 为 运行 之后,命令 window 关闭并且没有进入暂停状态。 运行 npm install 在同一目录的 VS code 中接收...

PS C:\testApp> npm install
npm WARN @date-io/date-fns@1.3.13 requires a peer of date-fns@^2.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN tsutils@3.20.0 requires a peer of typescript@>=2.8.0 || >= 3.2.0-dev || >= 3.3.0-dev || >= 3.4.0-dev || >= 3.5.0-dev || >= 3.6.0-dev || >= 3.6.0-beta || >= 3.7.0-dev || >= 3.7.0-beta but none is installed. You must install peer dependencies yourself.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@2.3.2 (node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.3.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.13 (node_modules\watchpack-chokidar2\node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.13 (node_modules\webpack-dev-server\node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.13 (node_modules\jest-each\node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

up to date in 30.79s

147 packages are looking for funding
  run `npm fund` for details

我该如何解决这个问题?

如果您检查实际调用的 npm.cmd 和 运行 的 npm 命令,它看起来像这样:

@ECHO off
SETLOCAL
CALL :find_dp0

IF EXIST "%dp0%\node.exe" (
  SET "_prog=%dp0%\node.exe"
) ELSE (
  SET "_prog=node"
  SET PATHEXT=%PATHEXT:;.JS;=;%
)

"%_prog%"  "%dp0%\node_modules\npm\bin\npx-cli.js" %*
ENDLOCAL
EXIT /b %errorlevel%
:find_dp0
SET dp0=%~dp0
EXIT /b

您的问题在于最后一行:EXIT /b 退出当前命令 window。我猜你可以只复制那个脚本然后 运行 它而不是直接调用 npm。

所以基本上是这样的:

@ECHO off
REM change the path to your local nodejs
node.exe  "c:\program files\nodejs\node_modules\npm\bin\npx-cli.js" install

pause
npm run dev