Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在PowerShell中,使用echo命令编写的文件不是';t与NodeJS一起工作_Node.js_Linux_Powershell_Terminal_Echo - Fatal编程技术网

Node.js 在PowerShell中,使用echo命令编写的文件不是';t与NodeJS一起工作

Node.js 在PowerShell中,使用echo命令编写的文件不是';t与NodeJS一起工作,node.js,linux,powershell,terminal,echo,Node.js,Linux,Powershell,Terminal,Echo,app.jsecho“console.log('testsuccess!')> 节点应用程序 在Linux上运行良好。但是,PowerShell会生成以下错误消息: ~\apps\test\app.js:1 (function (exports, require, module, __filename, __dirname) { ��c ^ SyntaxError: Invalid

app.js
echo“console.log('testsuccess!')>

节点应用程序

在Linux上运行良好。但是,PowerShell会生成以下错误消息:

~\apps\test\app.js:1
(function (exports, require, module, __filename, __dirname) { ��c
                                                          ^
SyntaxError: Invalid or unexpected token
    at Object.exports.runInThisContext (vm.js:76:16)
    at Module._compile (module.js:528:28)
    at Object.Module._extensions..js (module.js:565:10)
    at Module.load (module.js:473:32)
    at tryModuleLoad (module.js:432:12)
    at Function.Module._load (module.js:424:3)
    at Module.runMain (module.js:590:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:509:3
另一方面,如果我要
echo>app.js
,那么从文本编辑器中输入
console.log('testsuccess!')
<代码>节点应用程序
在任何地方都可以正常运行

为什么


我的最佳猜测是:在PowerShell中,echo似乎将字符串设置为十六进制而不是UTF-8。

该行为取决于您使用的shell。在Windows中,如果使用标准命令提示符cmd.exe,则会包括引号本身。因此app.js将包含以下内容:

“console.log('测试成功!')”

删除引号将得到预期的结果

echo console.log('testsuccess!')>app.js


请注意,如果您在PowerShell或Linux上运行此操作,您将得到不同的结果,因为这些shell处理引号的方式不同。

PowerShell的
操作符总是生成UTF-16LE编码的文件,而
节点
无法处理这些文件

要创建UTF-8编码的文件,请使用
Out File
Set Content
-Encoding Utf8

"console.log('Test success!')" | Set-Content -Encoding Utf8 app.js

请注意,在创建UTF-8文件时,PowerShell总是在a前面加上前缀(前3个字节是十六进制字节值
EF BB BF
),UTF-8文件仅在Windows世界中使用,但
node
似乎能够在Windows和类似Unix的平台上处理此问题。

完全正确。我会澄清这是在Powershell中发生的。马上!使用bash中的
文件*
进行验证。谢谢