试试这个方法。
在运行 npm 的批处理文件(我们称之为 test.bat)中,我有以下内容:
@ECHO OFF
call npm install jquery 2<&1 || exit /b 1
ECHO Continuing...
现在,当我运行 test.bat 时,我看到以下内容:
[email protected] /cdn-cgi/l/email-protection node_modules\jquery
Continuing...
但是,如果我将“jquery”更改为包的名称doesn't存在(即模拟 npm 故障,正如你所说,不会影响 %ERRORLEVEL%)我看到这个:
npm ERR! Windows_NT 6.2.9200
npm ERR! argv "C:\\Program Files (x86)\\nodejs\\\\node.exe" "C:\\Program Files (x86)\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "asdasdas"
npm ERR! node v0.10.33
npm ERR! npm v2.3.0
npm ERR! code ETARGET
npm ERR! notarget No compatible version found: undefined@'*'
npm ERR! notarget No valid targets found.
npm ERR! notarget Perhaps not compatible with your version of node?
npm ERR! notarget This is most likely not a problem with npm itself.
npm ERR! notarget In most cases you or one of your dependencies are requesting
npm ERR! notarget a package version that doesn't exist.
npm ERR! Please include the following file with any support request:
npm ERR! C:\Projects\XXXX\npm-debug.log
请注意,“ECHO Continuing”行不会被执行。
The "2<&1
“部分似乎采用 STDERR 输出并将其重定向到 STDOUT:http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx?mfr=true http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx?mfr=true
“...句柄2(即STDERR),从ipconfig命令到句柄1
(即标准输出)...”
我从这个解决方案中获取了https://stackoverflow.com/a/10359327/68432 https://stackoverflow.com/a/10359327/68432