27 June 2013

Node.js with posix path support

It was Mat Sutcliffe who sent me a patch to compile node.js with mingw last weekend, if I recalled node.js used to be compilable with mingw via scons but apparently they switch to gyp entirely and mingw support became broken since then. I though it was a bit weird to sent it to me, but it turn out the process would involve msys python (needed by gyp to work properly).

The patch itself is a win-win solution as it will build native win32 binary but with support for posix path (MSYS and CYGWIN) via generic mount mechanism. He further explain why he choose mount over cygpath, that is because node.js operate asynchronously so by using mount path would settled once rather repeatedly as with cygpath.

This is binary of node.js 0.10.12 (built as ./configure --gdb --without-etw --without-perfctr)
original patch for mingw by Mat -> replace msysmnt with mount
modified patch for mingw-w64
python msys

The functionality is implemented in path.js which is bundled inside the executable. So actually you could modify (if you wish) that part (look for mingwToWindowsPath resolvedPath) with your favorite hex editor if you don't want to recompile. Just make sure you don't add and reduce the string block length. Or if you think mingw is such mundane task feel free to apply patch only to path.js and then compile it with M$VS as usual :-)

How to build with mingw:
1. extract the source file for node.js homepage
2. download python for msys and extract to msys installation
3. download one of appropriate patches (above) put in source tree
3. start mingw, navigate to source tree and apply patch
4. ./configure [your options] (make sure that python for msys is the active one)
5. python tools/gyp_node -f make
6. LINK=g++ make -C out BUILDTYPE=Release V=1

- I'm not sure about etw and perfctr whether those compilable with mingw but both don't compile out of box. 
- The download is contain official installation hierarchy with node.exe replaced. Since it's not installer (it make no sense anyway) I'll assume you know what to do next ;-)



  1. Thanks for the peer review and for providing a place to host this. It's worth pointing out that the changes I made to path.js are an ugly hack using synchronous I/O, albeit amortized, so if you want to use Node for serious network services work you might want to look to do this in a different way - maybe by adding the POSIX path support to libuv instead. My application was not speed critical, so for my use case it didn't matter, I just needed the easiest way to get it working. And you can still build Node without any path-related changes; it simply won't understand absolute POSIX paths.

  2. Can you elaborate on this post?
    I'm failing to compile node v0.10.30 under cygwin.
    What I'm trying to do:

    1) Downloaded node's source and unpacked it

    2) Put the original patch in node's source directory

    3) What do you mean by "start mingw"?
    Apply patch. I had to manually change patch file dir, because they had some sort of a/ and b/ directory prefixes.

    4) ./configure. from cygwin. I can't run configure from windows somehow, can I?

    4) I installed mingw and then applied msys. It's in C:\MinGW\msys\1.0. I unpacked your python in there. Now if I run C:\MinGW\msys\1.0\bin\python.exe from Windows I get a python shell.

    However if I run /cygdrive/c/MinGW/msys/1.0/bin/python.exe from cygwin I get nothing, it just hangs.

    So I used cygwin's python for command
    python tools/gyp_node -f make

    6) LINK=g++ make -C out BUILDTYPE=Release V=1
    LINK=i686-pc-mingw32-g++ make -C out BUILDTYPE=Release V=1

    both result in a bunch of "note" and "warning" messages and in the end:

    #define EREMOTE 66 /* The object is remote */
    deps/cares/cares.target.mk:120: recipe for target '/home/Khazidhea/node-v0.10.30/out/Release/obj.target/cares/deps/cares/src/ares_cancel.o' failed
    make: *** [/home/Khazidhea/node-v0.10.30/out/Release/obj.target/cares/deps/cares/src/ares_cancel.o] Error 1
    make: Leaving directory '/home/Khazidhea/node-v0.10.30/out'

    Can you help me out? I'm running quite nice setup here with python on cygwin. I get a lot of things that I like from linux world without commiting to having to reinstall or install a 2nd OS on my system. I would very much like to have node.js be runnable from cygwin.

    1. Hi Azamat, back then the mingw-w64 binary above turned out to be unstable (probably miscompiled), when tested with emscripten... Just a warning.

      If you use cygwin, there is no need for python msys but you still need to configure node.js before gyp. I think it should run the same way as in MSYS. As for the error, it looks like gyp/makefile error. I only test this with MSYS.

      But I recommend you to just patch the official binary directly using hex editor. It will break linker checksum/digital signature but it works