Reputation: 942
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"
@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" %*
)
@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" %*
)
@IF EXIST "C:\Users\-username-\AppData\Roaming\npm\\C:\Users\-username-\AppData\Roaming\npm\"env.exe" (
Upvotes: 1
Views: 2850
Reputation: 3318
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"
Upvotes: 0
Reputation: 8145
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
Upvotes: 3