Running PureScript code in the frontend/ Observables/ RxJS [Solved]

Hi!
I wanted to use Observables and I get Uncaught ReferenceError: require is not defined at

  var Rx = require("rxjs");

in my bundle.

This is what I did:

I thought https://github.com/LukaJCB/purescript-rxps is the most recent package.

Just to see if it works

import RxJS.Observable (Observable, fromArray, subscribeNext)

main :: Effect Unit
main = do
  liftEffect ((fromArray [ "1", "2", "3", "4", "5" ]) # subObservable)
  log "Hello from PureScript!"


subObservable :: Observable String -> Effect Unit
subObservable obs = do
  sub <- extract (obs # subscribeNext log)
  pure unit

bundled with

spago bundle-app --main Main --to dist/Main.js

and called from the browser with

  <script type="text/javascript" src="../dist/Main.js"></script>

What did I do wrong? And can I get away without manually hacking my bundle?
Thanks.

1 Like

The core issue here is that the library has required in the library, therefore you need to use some bundler after purescript to import the required module into your js output file.

Purescript bundler takes care of transforming ps files to js file.

Your js file generated by purescript has require.

Your browser has no idea about the require keyword.

Run parcel / webpack or whatever to transfer that js require into your browser js compatible file.

2 Likes

yes, thank you, I thought bundle-app means something different.
Also

has helped a lot.

Bundle-app will run your project through purs bundle, which only knows how to bundle compiler artifacts (things in output). It does bundle, it just doesn’t bundle foreign libraries.

2 Likes