Cannot find module when attempting run PureScript function from JavaScript

spago
ffi
#1

If I try to include a PS module in JS code with either of these techniques:

var Tps = require('Test.Foo')
// or
var Tps = require('./output/Test.Foo')

I encounter a cannot fine module 'Test.Foo' error when running spago test.

Here’s a minimal repo configured to reproduce this issue:

git clone https://github.com/milesfrain/spago_require
cd spago_require
spago test

[info] Build succeeded.
internal/modules/cjs/loader.js:615
throw err;
^
Error: Cannot find module ‘./output/Test.Foo’

Once I get this working, I’ll update chapter 10 to include better instructions. I’m also planning to add the fix described in this related thread.

There’s a similar spago github issue, but that’s resolved. Not sure if I’m encountering a regression.

#2

Try using ../output/Test.Foo instead of ./output/Test.Foo

#3

Wait, that might not work
Try ../Test.Foo

1 Like
#4

Excellent. Thanks, that works.

1 Like
#5

I’m not sure if it works with spago bundle-app. I think you might want to check, just in case.

Edit: if you use that only in testing code, I think that’ll work correctly.

#6

As noted in the docs, spago bundle-app will run the main function of the module you’re picking as entry point (default: Main)

This is equivalent to using spago bundle-module and then calling node -e "require('./index').main()"

Note: in this case - where you want to do interop with JS, you most likely want to use bundle-module

1 Like