Home > Software design >  How to get a string of directory which contains single backslash in node js
How to get a string of directory which contains single backslash in node js

Time:10-23

I am trying to get a directory argument from cli using node js. Suppose my command line is: node myapp.js D:\test\strange. when I do console.log(process.argv[2]) it shows: D:teststrange how do I get D:\test\strange

CodePudding user response:

The problem is in the command line processor of the shell used to run node. Using

console.log(process.argv)

as the contents of log-argv.js,

  1. Under the Windows command line processor CMD.exe

     node log-argv D:\test\strange
    

    outputs (in test directory D:\localhost\SO)

     [
       'C:\\Program Files\\nodejs\\node.exe',
       'D:\\localhost\\SO\\log-argv',
       'D:\\test\\strange'
     ]
    

    which is what was wanted.

  2. Under git-bash.exe for windows however, the same command line outputs

     [
       'C:\\Program Files\\nodejs\\node.exe',
       'D:\\localhost\\SO\\log-argv',
       '\x96D:teststrange'
     ]
    

    suggesting node is relying on the command line processor, used to run it, to parse the command line string for it. This is entirely consistent with C programming practice.

  • Related