2
votes

Is it possible to make all node modules available when running via pulp repl? It seems like some only work when invoked via pulp run.

I'm following the pattern in the Purescript Book: https://github.com/paf31/purescript-book/blob/master/text/chapter12.md

exports.fooImpl = function(onSuccess, onFailure) {
    return function() {
        require('some-lib').doThing(function(error, data) {
            if (error) onFailure(error.code)()
            else onSuccess(data)();
        }

    }
}

Purescript definitions:

foreign import fooImpl
  :: Fn2 
      (String -> Effect Unit)
      (ErrorCode -> Effect Unit)
      (Effect Unit)

foo:: (Either ErrorCode String -> Effect Unit) -> Effect Unit
foo path k =
  runFn3 readFileImpl
         path
         (k <<< Right)
         (k <<< Left)

Now if I hop into a REPL

pulp repl 

and try to call my function, I get an error when I try to call the required library. .xxx is not a function

However, if I run via pulp run everything runs as expected and the node library function is called without issue.

Similarly, if I run a node repl and call the library directly I similarly experience no problems.

Interestingly, not all node libraries have this issue. Built ins like fs work fine, but third party ones are the ones that have trouble in the repl.

Is there a way to make these libraries available when running in the REPL?

2

2 Answers

1
votes

Do you have a reproducible example? It works for me. I will use https://github.com/nonbili/purescript-msgpack-msgpack as an example. Start pulp repl in the repo directory.

PSCi, version 0.12.5
Type :? for help

> import Msgpack
> encode' { x: 1 }
"¡x\1"

The implementation of encode' is

-- Msgpack.purs
foreign import encodeToString_ :: Json -> String
encode' :: forall a. EncodeJson a => a -> String
encode' = encodeToString_ <<< encodeJson

// Msgpack.js
var msgpack = require("@msgpack/msgpack");

exports.encodeToString_ = function(json) {
  return uint8ArrayToString(msgpack.encode(json));
};
0
votes

I believe this was user error caused a name conflict.

Edit:

I now realize that this is driven purely my the module name. As long as that doesn't conflict with the node_modules library, all seems well.

Meaning, even if you directly shadow the names on the file system. For instance

npm install SomeLibrary
node_modules/SomeLibrary/

And have a purescript directory structure that mirrors the name

src/SomeLibrary/SomeLibrary.js
src/SomeLibrary/SomeLibrary.purs
src/main

As long as the module name inside of the Purs file doesn't match the node_modules library name 1:1, everything will be fine

module SomeLibraryPurescript where ... 

A-OK. Whereas

module SomeLibrary where ...

fails

I created a file structure in purescript that shadowed the node_modules name.

src/MyLibrary/MyLibrary.js
src/MyLibrary/MyLibrary.purs
src/Main.purs 

When purescript populates the .psci_modules, it was pulling in my MyLibrary module, which seems to have prevented it from finding the actual node_modules one.

I fixed this by

  1. clearing out the output/ and .psci_modules/ directories
  2. moving the library I was trying wrap with an FFI into a Data/ directory
  3. pulp repl

Resulting directory structure: