0

Typing node --version and "node" --version in cmd.exe on Windows both give the same result. But npm --version and "npm" --version don’t!

C:\>node --version
v14.7.0

C:\>"node" --version
v14.7.0

C:\>npm --version
6.14.7

C:\>"npm" --version
internal/modules/cjs/loader.js:1088
  throw err;
  ^

Error: Cannot find module 'C:\node_modules\npm\bin\npm-cli.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:1085:15)
    at Function.Module._load (internal/modules/cjs/loader.js:928:27)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
    at internal/main/run_main_module.js:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}
internal/modules/cjs/loader.js:1088
  throw err;
  ^

Error: Cannot find module 'C:\node_modules\npm\bin\npm-cli.js'
[90m    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:1085:15)[39m
[90m    at Function.Module._load (internal/modules/cjs/loader.js:928:27)[39m
[90m    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)[39m
[90m    at internal/main/run_main_module.js:17:47[39m {
  code: [32m'MODULE_NOT_FOUND'[39m,
  requireStack: []
}

Neither do echo test and "echo" test:

C:\>echo test
test

C:\>"echo" test
'"echo"' is not recognized as an internal or external command,
operable program or batch file.

In bash, quoting the program name makes no difference as far as I know. Why does it make a difference in cmd.exe, sometimes?

lydell
  • 1,147
  • 12
  • 16
  • `echo` is an internal command (not an executable file), so quoting isn't needed (and prevents the parser to recognize it as an internal command). `find` is an executable (native, but not internal, but as an EXE), so `"find" "string" file.txt` works. `npm` is also an external command, but obviously does parameter parsing different (probably using the whole command line as a base. The error you show is not a `cmd` error, but clearly from `npm` (or another executable, called by `npm`). – Stephan Aug 05 '20 at 20:29
  • @Stephan The full name of `npm` is `npm.cmd`. It is a batch file with lots of small mistakes. The first line and the last line are 100% okay. All other lines between should be improved by author. Code of [npm.cmd](https://github.com/npm/cli/blob/latest/bin/npm.cmd) is available online on GitHub. – Mofi Aug 06 '20 at 05:31
  • @lydell The batch file `npm.cmd` and `node.exe` executed by `npm.cmd` respectively the JavaScript code are not good coded and for that reason `"npm" --version` results in an error exception. You have to distinguish between internal commands of `cmd.exe` and "external" [Windows Commands](https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/windows-commands) which are executables in `%SystemRoot%\System32` with file extension `.com` or `.exe`. If `command /?` works and outputs the help and `where command` outputs an error message, `command` is an internal command. – Mofi Aug 06 '20 at 05:39
  • 1
    @lydell Internal commands of `cmd.exe` are, for example, `echo`, `copy`, `dir`, `cd`, `if`, `for` and many more. External commands are, for example, `attrib.exe`, `find.exe`, `findstr.exe`, `robocopy.exe`, `xcopy.exe`, `chcp.com`, `more.com`, `tree.com`. Run `cmd /?` and read entire help, especially the last paragraph explaining when a file name (or any other argument string) __must__ be enclosed in double quotes. Please take a look on [What is the reason for "X is not recognized as an internal or external command, operable program or batch file"?](https://stackoverflow.com/a/41461002/3074564) – Mofi Aug 06 '20 at 05:52
  • @lydell The long referenced answer explains in full details how `cmd.exe` and `where.exe` find executables or scripts which are typed by the user on command line or in a batch file with just file name without file extension and without full path. I recommend for batch files to use full qualified file names wherever possible because of full path is well-known, for example, write `%SystemRoot%\System32\find.exe` instead of just `find`. Then Windows command processor does not need to search for `find` and it is made sure that right `find` is executed and not another `find.*` found somewhere else. – Mofi Aug 06 '20 at 05:52
  • Thanks for the links and explanations! So, in summary, quoting internal commands simply isn’t supported for whatever reason. For external commands, the correct program is run, but the program is able to “see” the quotes and might act differently if it does not expect the quotes to be there? – lydell Aug 08 '20 at 07:47
  • Yes, the programmer who wrote the code for interpreting the argument strings passed to a script or executable is responsible for handling correct also an argument string enclosed in double quotes. A parent program/script cannot know how a started child program/script interprets the argument strings. Therefore Windows command processor used to run commands and executables does not remove the double quotes surrounding an argument string. – Mofi Aug 08 '20 at 20:42

0 Answers0