Search code examples
node.jshaste

How to I run in nodejs a javascript file produced by haste?


If I have a file, hello.hs:

main = putStrLn "Hello, World!"

I can compile it to hello.js using haste command:

hastec hello.hs

How do I run the result hello.js file under nodejs?


Solution

  • By default, Haste's main is set to execute when the browser's onload event fires. This obviously makes no sense for Node, so you need to pass the --onexec flag to Haste when compiling your program:

    $ hastec --onexec hello.hs
    

    Haste uses Node to run its test suite in this way. Note, however, that with the exception of writing to standard output (like putStrLn), Haste does not map system operations (file IO, etc.) to Node equivalents. If you're writing an application that needs to interact with the OS, you're better off using vanilla GHC.

    Update: Thank you, and good answer. To recap, if you want to compile and run hello.hs under node, the two lines would be:

    hastec --onexec hello.hs
    node hello.js