[Haskell-beginners] Haskell -> JavaScript -> nodejs

Martin Vlk martin at vlkk.cz
Sun Jun 21 20:02:11 UTC 2015


Thanks, in the end I was able to figure it out, here is the code:
(https://github.com/martinvlk/ghcjs-node-minimal)

{-# LANGUAGE JavaScriptFFI,
             OverloadedStrings #-}

module Main (main) where

import           GHCJS.Types
import           GHCJS.Foreign

foreign import javascript unsafe "require($1)"
   require :: JSString -> IO (JSRef a)
foreign import javascript unsafe "$1.hostname()"
   hostname :: JSRef a -> IO JSString

main :: IO ()
main = require "os" >>= hostname
       >>= \h -> putStrLn $ "Our hostname is '"
       ++ (fromJSString h) ++ "'"

Martin

Sumit Sahrawat, Maths & Computing, IIT (BHU):
> Better try on the cafe. cc-ing.
> 
> On 20 June 2015 at 15:26, Martin Vlk <martin at vlkk.cz> wrote:
> 
>> I made some progress - in order for Haskell modules to work with ghcjs
>> you need to use cabal with the --ghcjs flag to install them.
>>
>> Martin
>>
>> Martin Vlk:
>>> Hi,
>>> I can successfully compile a basic Hello World into JavaScript with
>>> ghcjs and run it with nodejs.
>>> But what I ultimately need to do is write a nodejs module in Haskell and
>>> compile down to JavaScript with ghcjs. I need to be able to require
>>> other nodejs modules as well.
>>>
>>> Could somebody point me in the right direction? E.g. what approach and
>>> libraries to use...
>>>
>>> I have tried a simple thing with jsaddle/lens, but when I try to compile
>>> with ghcjs it is not finding the imported modules, even though when
>>> compiling with ghc it does find them (they are installed using cabal).
>>>
>>> Many Thanks
>>> Martin
>>>
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
> 
> 
> 
> 
> 
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> 


More information about the Beginners mailing list