Description
- Version: 8.5.0
- Platform: Windows 64
- Subsystem: cli
Hi,
As I went through the CLI tutorial, I came across a strange situation while executing simple command.
The problem was with this two commands, with options -p
and -e
$ node -e 'console.log(3 + 2)'
and
$ node -p '3 + 2'
On Linux those two commands execute correctly.
lukaszs@RS18:~$ node -e 'console.log(3 + 4)'
7
lukaszs@RS18:~$ node -p '3 + 2'
5
lukaszs@RS18:~$
But on Windows I receive this:
- First for the
node -e 'console.log(3 + 4)'
C:\Users\lukaszs>node -e 'console.log(3 + 4)'
[eval]:1
'console.log(3
^^^^^^^^^^^^^^
SyntaxError: Invalid or unexpected token
at createScript (vm.js:74:10)
at Object.runInThisContext (vm.js:116:10)
at Object.<anonymous> ([eval]-wrapper:6:22)
at Module._compile (module.js:573:30)
at evalScript (bootstrap_node.js:452:27)
at startup (bootstrap_node.js:139:9)
at bootstrap_node.js:598:3
After I remove spaces, I got no error, but without a result:
C:\Users\lukaszs>node -e 'console.log(3+4)'
C:\Users\lukaszs>
So I change the -e
option to -p
, I receive this:
C:\Users\lukaszs>node -p 'console.log(3+4)'
console.log(3+4)
C:\Users\lukaszs>
Node prints my script as a string without evaluating it.
- The very similar situation was with second command:
node -p '3 + 2'
C:\Users\lukaszs>node -p '3 + 2'
[eval]:1
'3
^^
SyntaxError: Invalid or unexpected token
at createScript (vm.js:74:10)
at Object.runInThisContext (vm.js:116:10)
at Object.<anonymous> ([eval]-wrapper:6:22)
at Module._compile (module.js:573:30)
at evalScript (bootstrap_node.js:452:27)
at startup (bootstrap_node.js:139:9)
at bootstrap_node.js:598:3
After removing spaces, I receive string like in the previous example:
C:\Users\lukaszs>node -p '3+2'
3+2
C:\Users\lukaszs>
So I went to the CLI docs, I I saw this description:
-e, --eval "script"
Evaluate the following argument as JavaScript. The modules which are predefined in the REPL can also be used in script.
-p, --print "script"
Identical to -e but prints the result.
There is one difference in my examples according to docs. It seams I need to use double quote "
instead of single quote '
.
So let's test it.
C:\Users\lukaszs>node -e "console.log(3 + 4)"
7
C:\Users\lukaszs>
Ok, this works!
C:\Users\lukaszs>node -p "3 + 2"
5
C:\Users\lukaszs>
And another example works too.
So it looks like for Windows I need to use double quote "
but on Linux it doesn't matter what I used, because every version works on Linux.
lukaszs@RS18:~$ node -p '3 + 2'
5
lukaszs@RS18:~$ node -p "3 + 2"
5
lukaszs@RS18:~$ node -e 'console.log(3 + 4)'
7
lukaszs@RS18:~$ node -e "console.log(3 + 4)"
7
lukaszs@RS18:~$
If this is not a bug, but expected result, I think there should be at least some info in the docs that on Windows you should use only double quote "
. Because without this info, going thru tutorials you can get really frustrated when you try to execute this simple command and it not work.