Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strange behavior for CLI options -e and -p on Windows #15522

Closed
lukaszewczak opened this issue Sep 21, 2017 · 2 comments
Closed

Strange behavior for CLI options -e and -p on Windows #15522

lukaszewczak opened this issue Sep 21, 2017 · 2 comments
Labels
cli Issues and PRs related to the Node.js command line interface. doc Issues and PRs related to the documentations. windows Issues and PRs related to the Windows platform.

Comments

@lukaszewczak
Copy link
Contributor

  • 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:

  1. 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.

  1. 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.

@mscdex mscdex added cli Issues and PRs related to the Node.js command line interface. doc Issues and PRs related to the documentations. windows Issues and PRs related to the Windows platform. labels Sep 21, 2017
@bzoz
Copy link
Contributor

bzoz commented Sep 21, 2017

Windows shell traditionally uses double quote as the quote char. The documentation already suggests using ".

If you think that this needs more clarification, feel free to open a PR

@lukaszewczak
Copy link
Contributor Author

Thank you @bzoz for comment, so I will prepare PR for this.

lukaszewczak added a commit to lukaszewczak/node that referenced this issue Sep 23, 2017
lukaszewczak added a commit to lukaszewczak/node that referenced this issue Sep 23, 2017
jasnell pushed a commit that referenced this issue Sep 26, 2017
PR-URL: #15568
Fixes: #15522
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
MylesBorins pushed a commit that referenced this issue Sep 29, 2017
PR-URL: #15568
Fixes: #15522
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
addaleax pushed a commit to addaleax/ayo that referenced this issue Sep 30, 2017
PR-URL: nodejs/node#15568
Fixes: nodejs/node#15522
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
MylesBorins pushed a commit that referenced this issue Oct 17, 2017
PR-URL: #15568
Fixes: #15522
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
MylesBorins pushed a commit that referenced this issue Oct 25, 2017
PR-URL: #15568
Fixes: #15522
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cli Issues and PRs related to the Node.js command line interface. doc Issues and PRs related to the documentations. windows Issues and PRs related to the Windows platform.
Projects
None yet
3 participants