Skip to content

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

Closed
@lukaszewczak

Description

@lukaszewczak
  • 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    cliIssues and PRs related to the Node.js command line interface.docIssues and PRs related to the documentations.windowsIssues and PRs related to the Windows platform.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions