2

I just need to use prelude-ls library in LiveScript, but no from the REPL. In my little test, I have 4 files:

  • main.htm
  • application.ls
  • application.js
  • require.js

I have the latest version of require.js (2.1.15) and in my main.htm I load the scripts:

<!DOCTYPE html>
<html>
    <head>
        <script src="./require.js" type="text/javascript"></script>
        <script src="./prelude.js" type="text/javascript"></script>
    </head>
    <body>

    </body>
</html>

Then, I go to my application.ls to test:

require! 'prelude-ls'

[1 2 3] |> prelude-ls.map (* 2)

My compile command is:

lsc -cwd $myFilePath And it compiles just fine. Then, I go there to get the final result, to test and receive the following error:

Uncaught Error: Module name "prelude-ls" has not been loaded yet for context: _. Use require([])

Well, I saw the this is a very common error and its corrections would happen in the js file, not in the ls and none of the links I followed solved my problems. I've tried it in 2 computers and had the really same result.

My final js file, application.js is:

// Generated by LiveScript 1.2.0
(function(){
  var preludeLs;
  preludeLs = require('prelude-ls');
  preludeLs.map((function(it){
    return it * 2;
  }))(
  [1, 2, 3]);
}).call(this);

Plase, help me if possible. I really read all the documentation of livescript and it doens't cites its first use with prelude-ls.

Marcelo Camargo
  • 2,240
  • 2
  • 22
  • 51

2 Answers2

1

I think you're confusing AMD.js (here, Require.JS) and CommonJS. AMD.js uses the

require(['dep1', 'dep2'], function (dep1, dep2) {
  dep1.callSomething();
});

(this allows for async loading)

CommonJS (which is what prelude-ls uses), on the other hand, is basically what node does (and what we generate with require!):

var preludeLs = require('prelude-ls')
Ven
  • 19,015
  • 2
  • 41
  • 61
  • In use with commonJS I yet get the error that `require is not defined`. I tried `npm install requirejs` but didn't work. – Marcelo Camargo Sep 15 '14 at 12:46
  • You need to include the file on your page to get `require`. You might want to look at browserify – Ven Sep 15 '14 at 12:47
0

At first I didn't understand, I find the answer confusing, though correct. Please let me sum it up.

Actually it's very simple and it works as says the doc.

We must install prelude-ls with npm or bower. We need to include the file prelude-browser.js (at prelude-ls/browser/prelude-browser.js).

Then we just require prelude at the beginning of our code:

prelude = require 'prelude-ls'

and we can call prelude.sum, etc.

A second option is to import exactly the function we need, like so:

{map, filter, lines} = require 'prelude-ls'

so we can call them directly.

That works with gulp and angularjs FYI.

This answer is clear.

Ehvince
  • 17,274
  • 7
  • 58
  • 79