Reputation: 107
echo "console.log('Test success!')" > app.js
node app
Runs fine on Linux. PowerShell, however, produces the following error message:
~\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
On the other hand, if I were to echo > app.js
, then input console.log('Test success!')
from a text editor. node app
runs just fine everywhere.
How come?
My best guess: In PowerShell, echo seems to make strings hexadecimal and not UTF-8.
Upvotes: 1
Views: 269
Reputation: 440112
PowerShell's >
operator invariably produces UTF-16LE-encoded files, which node
cannot handle.
To create UTF-8-encoded files instead, use Out-File
or Set-Content
with -Encoding Utf8
:
"console.log('Test success!')" | Set-Content -Encoding Utf8 app.js
Note that PowerShell invariably prepends a pseudo-BOM (the first 3 bytes are hex. byte values EF BB BF
) when creating UTF-8 files, which is only used in the Windows world, but node
seems to be able to handle that, both on Windows and Unix-like platforms.
Upvotes: 1
Reputation: 524
The behavior is dependent on the shell that you are using. In Windows, if you are using the standard command prompt, cmd.exe, the quotes themselves will be included. So app.js will contain this:
"console.log('Test success!')"
Removing the quotes will give the expected outcome.
echo console.log('Test success!') > app.js
Note that if you run this on PowerShell or Linux, you will get different results since those shells handle quotes differently.
Upvotes: 0