Search code examples
node.jswindowsunicodeescapingunicode-literals

How to use Unicode literals with the Node.js -e "evaluate script" commandline switch


Node.js has an -e commandline switch to evaluate code provided on the commandline rather than in a separate script file.

Oddly, I can't find official documentation for it online but the node executable self-documents it if you run node --help:

>node --help
Usage: node [options] [ -e script | script.js ] [arguments]
       node debug script.js [arguments]

Options:
  -v, --version         print Node.js version
  -e, --eval script     evaluate script
  -p, --print           evaluate script and print result

Now there are several ways to use Unicode literals in JavaScript, all seemingly begin with \u.

But no matter how I try to quote or escape the string with the Unicode literal, the code always fails to execute. Both under official Node.js and also JXcore.

Node

>node -e console.log('hello \u00A9')
[eval]:1
console.log('hello
            ^^^^^^

SyntaxError: Unexpected token ILLEGAL
    at Object.exports.runInThisContext (vm.js:53:16)
    at Object.<anonymous> ([eval]-wrapper:6:22)
    at Module._compile (module.js:435:26)
    at node.js:578:27
    at doNTCallback0 (node.js:419:9)
    at process._tickCallback (node.js:348:13)

JXcore

>jx -e console.log('hello \u{00A9}')
SyntaxError: unterminated string literal ([eval] 1:12)
    at  ([eval]-wrapper:6:8)
    at Module.prototype._compile (module.js:621:10)
    at evalScript (node.js:1054:18)
    at startup (node.js:419:7)
    at node.js:1604:3

I've tried double \\ and quadruple \\\\. I've tried single and double quote characters to delimit the strings.

(I am only trying this under Windows, just in case this might work fine under *nix.)


Solution

  • Try

    node -e "console.log('hello \u00A9')"
    

    Double-quoting ensures that console.log('hello \u00A9') is interpreted as a single argument by node.

    In your original attempt, node saw 2 arguments following -e (split by the space inside 'hello \u00A9'): console.log('hello and \u00A9'), and since the first argument following -e was not by itself a syntactically valid expression, you got a syntax error.


    Background:

    The scriptargument (-e's option-argument) containing the snippet of JavaScript code to execute must be a distinct argument on the command line, so that it can be distinguished from other arguments, such as the arguments to pass to it ([arguments]).

    The way to make a string with embedded whitespace distinct as an argument is to quote it as a whole.

    On Windows, this is typically done with double quotes, but it is ultimately up to the program invoked to interpret the arguments (after the shell may have expanded tokens, such as environment-variable references).

    Since single- and double-quoting can be used interchangeably in JavaScript, you may even get away with swapping single- and double quotes: node -e 'console.log("hello \u00A9")'

    (That definitely works on Unix, where the shell itself recognizes a single-quoted string as a distinct argument, and all programs are handed an array of parsed arguments rather than a command line to interpret themselves).