I'm currently using the serve script to serve up directories with Node.js on Windows 7. It works well in the MSYS shell or using sh
, as I've put node.exe and the serve script in my ~/bin (which is on my PATH), and typing just "serve" works because of it's Shebang (#!
) directive which tells the shell to run it with node.
However, Windows Command Prompt doesn't seem to support normal files without a *.bat or *.exe extension, nor the shebang directive. Are there any registry keys or other hacks that I can get to force this behavior out of the built-in cmd.exe
?
I know I could just write up a simple batch file to run it with node, but I was wondering if it could be done in a built-in fasion so I don't have to write a script for every script like this?
Update: Actually, I was thinking, is it possible to write a default handler for all 'files not found' etc. that I could automatically try executing within sh -c
?
Thanks.
Actually looks like someone who knows how to write batch files better than I has also approached this. Their batch file may work better.
http://whitescreen.nicolaas.net/programming/windows-shebangs
No, there's no way to "force" the command prompt to do this.
Windows simply wasn't designed like Unix/Linux.
Is there a shell extension that does something similar?
Not that I've heard of, but that should be asked on Super User, not here.
Yes, this is possible using the
PATHEXT
environment variable. Which is e.g. also used to register.vbs
or.wsh
scripts to be run "directly".First you need to extend the
PATHEXT
variable to contain the extension of that serve script (in the following I assume that extension is .foo as I don't know Node.js)The default values are something like this:
You need to change it (through the Control Panel) to look like this:
Using the control panel (Control Panel -> System -> Advanced System Settings -> Environment Variables is necessary to persist the value of the PATHEXT variable.
Then you need to register the correct "interpreter" with that extension using the commands
FTYPE
andASSOC
:(The above example is shamelessly taken from the help provided by
ftype /?
.)ASSOC and FTYPE will write directly into the registry, so you will need an administrative account to run them.
Here is a simple way to force windows to support shebang however it has a caveat regarding the file naming. Copy the following text in to a batch file and follow general idea in REM comments.
There's no way to execute random file, unless it is an actual executable binary file. Windows CreateProcess() function just not designed for it. The only files it can execute are those with
MZ
magic or with extensions from%PATHEXT%
list.However, CMD itself has a limited support for custom interpreters through
EXTPROC
clause. The limitation is that interpreter should also support and omit this clause in its execution.Command prompt does not support shebang , however there are a lot hybrid techniques for different languages that you allow to combine batch and other languages syntax in one file.As your question concerns node.js here's a batch-node.js hybrid (save it with
.bat
or.cmd
extension):It is possible to be done with many other languages like Ruby,Perl,Python,PHP and etc.