简体   繁体   English

如何使节点模块在Purescript REPL中可用?

[英]How do you make node modules available in the Purescript REPL?

Is it possible to make all node modules available when running via pulp repl ? 通过pulp repl运行时是否可以使所有节点模块可用? It seems like some only work when invoked via pulp run . 似乎只有通过pulp run才能调用某些方法。

I'm following the pattern in the Purescript Book: https://github.com/paf31/purescript-book/blob/master/text/chapter12.md 我正在遵循Purescript书中的模式: 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: Purescript定义:

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 现在,如果我跳进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. 但是,如果我通过pulp run ,那么一切都会按预期运行,并且调用节点库函数不会出现问题。

Similarly, if I run a node repl and call the library directly I similarly experience no problems. 同样,如果我运行一个节点repl并直接调用该库,我同样不会遇到任何问题。

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. fs这样的内置插件可以正常工作,但是第三方插件则代表了麻烦。

Is there a way to make these libraries available when running in the REPL? 在REPL中运行时,是否有办法使这些库可用?

Do you have a reproducible example? 您有可复制的示例吗? It works for me. 这个对我有用。 I will use https://github.com/nonbili/purescript-msgpack-msgpack as an example. 我将以https://github.com/nonbili/purescript-msgpack-msgpack为例。 Start pulp repl in the repo directory. 在repo目录中启动pulp repl

PSCi, version 0.12.5
Type :? for help

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

The implementation of encode' is encode'的实现是

-- 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));
};

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. 只要这与node_modules库没有冲突,一切似乎都很好。

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 并具有反映名称的purescript目录结构

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 只要Purs文件中的模块名称与node_modules库名称1:1不匹配,就可以了

module SomeLibraryPurescript where ... 

A-OK. 一个OK。 Whereas 鉴于

module SomeLibrary where ...

fails 失败

I created a file structure in purescript that shadowed the node_modules name. 我用purescript创建了一个文件结构,该结构遮盖了node_modules的名称。

 
 
 
  
  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. 当purescript填充 .psci_modules ,它正在拉入 MyLibrary模块,这似乎阻止了它查找 实际的 node_modules。

I fixed this by 我固定了

  1. clearing out the output/ and .psci_modules/ directories 清除 output/.psci_modules/目录
  2. moving the library I was trying wrap with an FFI into a Data/ directory 将我尝试用FFI包装的库移到 Data/目录中
  3. pulp repl

Resulting directory structure: 产生的目录结构:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM