节点console.log行为和windows stdout

时间:2012-03-23 20:50:00

标签: windows node.js powershell

我尝试在Windows中重定向输出形式的node.js脚本时发现了奇怪的行为。这种行为的最小可重现性示例如下:

从Windows控制台(cmd.exe):

c:\> node -e console.log('hello')
hello

来自Powershell:

PS C:\> node -e console.log('hello')
PS C:\> 

也就是说,它在没有向控制台写任何内容的情况下退出。

这是一个问题,因为当我尝试将输出重定向到文件时,没有任何内容被写入,例如

node -e console.log(123) > temp.txt

正在创建一个名为temp.txt的空文件。

节点是如何将console.out写入stdio的?

3 个答案:

答案 0 :(得分:4)

问题似乎在括号中。

试试这个:

node -e "console.log('hello')"

至于重定向,有一个很明显的原因,为什么这不起作用。您也可以尝试将其传递给Out-File temp.txt

答案 1 :(得分:0)

从PowerShell调用时,您是否“点”调用“node”?

.\node -e "console.log('hello')" >test2.txt

这适合我。

答案 2 :(得分:0)

我遇到了同样的问题,在我的头撞墙后,我卸载了32位节点(来自我的64位机器)并安装了64位节点。现在我在Powershell脚本中获得Node输出。 不知道它是通过重新安装来修复的,还是64-和32-位。

相关问题