1

I've been trying to create global commands that will run in "Command Prompt" on windows in Node.js. Unfortunately, all the tutorials seem to be for Mac/Linux. This normally wouldn't be a problem, however when I follow them exactly and just use different directory string formats and locations so it can be Windows compliant, Node.js fails to accurately parse the links.

The only way I've been able to get it to work is by going into the "Windows Command" file Node generates after running "npm link" and manually adjusting the values. This works, but seems like it isn't the best solution.

The 'directory identifier' I'm using is:

#!C:\Users\-my username-\AppData\Roaming\npm\"env node"

Generated Node.js "Windows Command" (Doesn't Work):

@IF EXIST "%~dp0\C:\Users\-username-\AppData\Roaming\npm\"env.exe" (
  "%~dp0\C:\Users\-username-\AppData\Roaming\npm\"env.exe"  node" "%~dp0\node_modules\Node-Command\Test.js" %*
) ELSE (
  @SETLOCAL
  @SET PATHEXT=%PATHEXT:;.JS;=;%
  C:\Users\-username-\AppData\Roaming\npm\"env  node" "%~dp0\node_modules\Node-Command\Test.js" %*
)

Manually adjusted Node.js "Windows Command":

@IF EXIST "%~dp0\node.exe" (
  "%~dp0\node.exe"  "%~dp0\node_modules\Node-Command\Test.js" %*
) ELSE (
  @SETLOCAL
  @SET PATHEXT=%PATHEXT:;.JS;=;%
  node  "%~dp0\node_modules\Node-Command\Test.js" %*
)

Error I'm Getting (from generated command):

@IF EXIST "C:\Users\-username-\AppData\Roaming\npm\\C:\Users\-username-\AppData\Roaming\npm\"env.exe" (
Alec
  • 942
  • 4
  • 11
  • The error you're getting does not seem to come from your manually adjusted version. See also [Stack Overflow: What does %~dp0 mean, and how does it work?](http://stackoverflow.com/questions/5034076/what-does-dp0-mean-and-how-does-it-work) – xmojmr Jan 23 '15 at 11:44
  • Sorry for the confusion. The error was from the generated command. – Alec Jan 23 '15 at 20:03

2 Answers2

3

I don't know what is the correct bullet-proof way, but this one works just fine on my development machine:

Create file test.cmd in folder %APPDATA%\npm containing

@node "C:\full\path\to\test.js" %*

On my version of Windows the %APPDATA% expands to C:\Users\my_user_name\AppData\Roaming so the full shell script name would be C:\Users\my_user_name\AppData\Roaming\npm\test.cmd

@ means don't print what the cmd script does internally

%* means pass all command line arguments given to the test.cmd script up to the test.js script as values in the process.argv array

The script assumes that both C:\Program Files\nodejs and C:\Users\my_user_name\AppData\Roaming were added to the global environment PATH variable by Node installer

xmojmr
  • 7,696
  • 5
  • 29
  • 51
  • Thanks for the help. It's not quite the solution I wanted, but it will work in the mean time. I'm using "@node %~dp0\node_modules\Node-Command\Test.js %*" instead so it goes of the current directory. I also wrote a node command to automate this process for me. If you'd like the source code I'd be more than happy to post it. – Alec Jan 26 '15 at 23:38
  • @Alec I'm also using custom script to automate generation of the command in my answer (at installation time) so portability of the generated script was not a concern for me. If you know an answer which solves your problem more comfortably then certainly publish it (with all the helpful details) as [self-answer](http://stackoverflow.com/help/self-answer) and don't hesitate to accept it if no one else provides something better – xmojmr Jan 27 '15 at 07:56
0

I put this at the top of my JS file:

#!/usr/bin/env node

Which Node seems to translate correctly to this in the .cmd file:

"%~dp0\node.exe"
molasses
  • 2,998
  • 5
  • 20
  • 22