Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass a literal forward slash into Node.js in Git Bash for Windows?

I'm using Git Bash for Windows (as in, I right-click in some directory and select "Git Bash here" from the context menu). Node.js v5.10.1.

Here's my complete Node.js script, example.js:

console.log(process.argv);

Here's my command line and the output:

$ node example.js "https://example.com"
[ 'C:\\...\\node.exe',
  'C:\\...\\example.js',
  'https:\\example.com' ]

Notice how the input argument "https://example.com" has two literal forward slashes, but the resulting string "https:\\example.com" has one literal backslash.

I have found that no amount of backslash-escaping (e.g. "https:\\/example.com") or additional forward slashes (e.g. "https:////////example.com") in the command line can stop this from happening. The result is always one literal backslash.

Using the same Node.js executable to run the same script from an ordinary Windows cmd.exe window does not cause this behaviour. Running an equivalent Python script from Git Bash for Windows also does not cause this behaviour.

Update

Watch this:

$ node.exe -e "console.log(process.argv)" "https://example.com"
[ 'C:\\...\\node.exe',
  'https://example.com' ]

$ node -e "console.log(process.argv)" "https://example.com"
[ 'C:\\...\\node.exe',
  'https:\\example.com' ]

Why does removing ".exe" from the end of the call cause the slashes to reverse? I am using NVM 1.1.0, if that is relevant. which node.exe and which node return identical results, both pointing to that EXE file. There is no separate file named node in that directory. This happens on both Node.js 5.10.1 and Node.js 6.0.0.

like image 935
qntm Avatar asked Nov 08 '22 15:11

qntm


1 Answers

Git Bash is based on MSYS2, which does a "POSIX path conversion" like MinGW does. This conversion windows-ifys paths with slashes when calling windows-looking executables. MSYS2 might have done some "Windows-looking" detection in a way different from MinGW.

The question joezen777 mentioned in comments points to a solution: just add ;https:;http: to MSYS2_ARG_CONV_EXCL. The documentation is available at https://github.com/msys2/msys2/wiki/Porting.

like image 89
Mingye Wang Avatar answered Nov 14 '22 23:11

Mingye Wang