After upgrading to purs/spago to 0.15 I get error "A CommonJS foreign module implementation was provided"

I upgraded purs/spago via brew (on macOS) to 0.15.

Since then I get in the exercise directories to the PureScript book on spago repl (after lots of compilation) the error:

A CommonJS foreign module implementation was provided for module Test.Unit.Console

Why’s that? And how can I fix this?

1 Like

Seems the exercises use TestUnicode - but this library was not updated to PureScript 0.15 yet and it uses FFI Modules written using CommonJS (exports.XYZ = … and require)

You could clone this, fix the modules and overwrite your package set to use your local one (do an pull-request to share your work, etc) but I that’s probably not what you are looking for if you are still learning.

You could switch back to PureScript 0.14 (assuming the docs where not changed yet) or you can wait till the libraries are updated.

1 Like

Indeed somebody already did the work see PR 52 - so if you want I can try to explain how to use the changed code from this PR to get it working for you.

Did you clone the code? (I want to grab the .dhall files, do the changes and post them do you)

1 Like

Ok,

here is what you can do to get it to compile (tested only for Excercise 2):

  • go in the folder
  • do spago upgrade-set (this will update the latest set)
  • in packages.dhall change the override for test-unit:
let overrides =
      { test-unit =
        { dependencies =
          [ "aff"
          , "either"
          , "prelude"
          , "effect"
          , "quickcheck"
          , "free"
          , "strings"
          , "lists"
          , "js-timers"
          , "avar"
          ]
        , repo = "https://github.com/mhmdanas/purescript-test-unit.git"
        , version = "101026860937735c7e3e46f249f16d56d9c875b2"
        }
      }

this will point to the commit in the PR I linked above

Now math was deprecated but is used here - you can switch this for number - so remove math from the dependencies in spago.dhall and grab numbers (either add it there or do spago install numbers

Change the Solutions.purs file-improrts to

import Data.Int (rem)
import Data.Number (pi, sqrt)

(basically switching Math for Data.Numbers)

and now spago build and spago test should work


Here is all of the packages.dhall file:

let upstream =
      https://github.com/purescript/package-sets/releases/download/psc-0.15.0-20220505/packages.dhall
        sha256:ba57c25c86fd54c2b672cda3a6836bbbdff4b1bbf946bceaabb64e5a10285638

let overrides =
      { test-unit =
        { dependencies =
          [ "aff"
          , "either"
          , "prelude"
          , "effect"
          , "quickcheck"
          , "free"
          , "strings"
          , "lists"
          , "js-timers"
          , "avar"
          ]
        , repo = "https://github.com/mhmdanas/purescript-test-unit.git"
        , version = "101026860937735c7e3e46f249f16d56d9c875b2"
        }
      }

let additions =
      { react-basic =
        { dependencies = [ "prelude", "effect", "record" ]
        , repo = "https://github.com/lumihq/purescript-react-basic.git"
        , version = "main"
        }
      , react-basic-hooks =
        { dependencies =
          [ "prelude"
          , "aff-promise"
          , "aff"
          , "console"
          , "datetime"
          , "effect"
          , "either"
          , "indexed-monad"
          , "maybe"
          , "newtype"
          , "numbers"
          , "react-basic"
          , "type-equality"
          , "unsafe-coerce"
          , "unsafe-reference"
          , "web-html"
          ]
        , repo =
            "https://github.com/milesfrain/purescript-react-basic-hooks.git"
        , version = "v6.3.0-ps-0.14"
        }
      , react-basic-dom =
        { dependencies =
          [ "prelude"
          , "effect"
          , "foreign-object"
          , "react-basic"
          , "unsafe-coerce"
          , "web-dom"
          , "web-events"
          , "web-file"
          , "web-html"
          ]
        , repo = "https://github.com/lumihq/purescript-react-basic-dom.git"
        , version = "v3.2.0"
        }
      , indexed-monad =
        { dependencies = [ "control", "newtype" ]
        , repo = "https://github.com/garyb/purescript-indexed-monad.git"
        , version = "master"
        }
      }

in  upstream // overrides // additions

this is the new spago.dhall:

{-
Welcome to a Spago project!
You can edit this file as you like.
-}
{ name = "my-project"
, dependencies =
  [ "console"
  , "effect"
  , "foldable-traversable"
  , "integers"
  , "lists"
  , "numbers"
  , "prelude"
  , "psci-support"
  , "test-unit"
  ]
, packages = ./packages.dhall
, sources = [ "src/**/*.purs", "test/**/*.purs" ]
}

and the Solution.purs:

module Test.NoPeeking.Solutions where

import Prelude

import Data.Int (rem)
import Data.Number (pi, sqrt)

-- ANCHOR: diagonal
diagonal w h = sqrt (w * w + h * h)
-- ANCHOR_END: diagonal

circleArea r = pi * r * r

leftoverCents n = rem n 100

sadly you will have to do the same with every exercise for now till somebody fixed the book (I think somebody posted there that they are already trying/doing it - so give the community a few weeks please - the new release is just a week old)

Have fun

5 Likes

Thanks a lot for this @CarstenKoenig. I really appreciated your solution.

However I think for me it is easier to downgrade to PureScript 0.14.

I’ll have a look how to do this with Homebrew…

1 Like

Yeah I guess that’s not a bad idea - at least till the book-sources are fixed.

AFAIK you should be able to do brew install purescript@0.14.9 (assuming the package is called purescript)

1 Like

Well, for me brew install does not seem to understand the @ syntax, but unlinking the new and relinked the old version worked.

Additionally I had to copy in the old packages.dhall file from git, because spago update-set had modified this config file.

1 Like

sorry about brew - thought I remembered that this should work similar to npm and co.

1 Like