Skip to content

"ReferenceError: exports is not defined" when importing a module #128

Description

@gdelazzari

Hi, I'm trying to use purs-loader to integrate some PureScript code in my Vue project (created with the latest vue-cli).

My vue.config.json (which allows to modify the Webpack's configuration) looks like this:

module.exports = {
  chainWebpack: config => {
    config.module
      .rule('purescript')
      .test(/\.purs$/)
      .use('purs-loader')
      .loader('purs-loader')
      .options({
        src: [
          'src/**/*.purs',
          'bower_components/purescript-*/src/**/*.purs'
        ],
        output: 'output',
        pscIde: false
      })
    
    config.resolve.extensions.add('.purs')
  }
}

I npm installed the packages purescript and purs-loader as dev-dependencies, created a bower.json file with the Purescript packages I needed for testing:

{
  "name": "purescript-webpack-example",
  "private": true,
  "dependencies": {
    "purescript-prelude": "^4.1.0",
    "purescript-arrays": "^5.3.0"
  }
}

and run bower install which completed successfully.

I then created a Test.purs file:

module Test where

import Prelude

data Point = Point { x :: Int, y :: Int }
type Polyline = Array Point

newPoint :: Int -> Int -> Point
newPoint x y = Point { x: x, y: y }

sumPoint :: Point -> Int
sumPoint (Point p) = p.x + p.y

newPolyline :: Int -> Int -> Polyline
newPolyline x y = [newPoint x y, newPoint x y]

and imported and tested it from JS:

import Test from '@/Test.purs'

// ...

let p = Test.newPoint(3)(2)
console.log(p)
console.log(Test.sumPoint(p))

let pl = Test.newPolyline(3)(5)
console.log(pl)

with everything working as expected. Then I introduced this function in the Test.purs file:

import Data.Array (length)

polylineLen :: Polyline -> Int
polylineLen pl = length pl

Webpack compiled succesfully but, now, my app doesn't run anymore in the browser with the following error in the browser console:

ReferenceError: exports is not defined app.js line 915 > eval:13:1
    <anonymous> Array.js:7
    js app.js:915
    __webpack_require__ app.js:767
    fn app.js:130
    <anonymous> Array.purs:3
    purs app.js:927
    __webpack_require__ app.js:767
    fn app.js:130
    <anonymous> Test.purs:3
    purs app.js:4056
    __webpack_require__ app.js:767
    fn app.js:130
    <anonymous> cjs.js:4
    ./node_modules/cache-loader/dist/cjs.js?!./node_modules/babel-loader/lib/index.js!./node_modules/cache-loader/dist/cjs.js?!./node_modules/vue-loader/lib/index.js?!./src/App.vue?vue&type=script&lang=js& app.js:2223
    __webpack_require__ app.js:767
    fn app.js:130
    <anonymous> App.vue:1
    ./src/App.vue?vue&type=script&lang=js& app.js:4020
    __webpack_require__ app.js:767
    fn app.js:130
    <anonymous> App.vue:1
    vue app.js:4008
    __webpack_require__ app.js:767
    fn app.js:130
    <anonymous> main.js:11
    js app.js:4163
    __webpack_require__ app.js:767
    fn app.js:130
    1 app.js:4345
    __webpack_require__ app.js:767
    <anonymous> app.js:902
    <anonymous> app.js:905

If I click on the error hyperlink app.js line 915 > eval:13:1 on the developer console, I can see that the error refers to line 13 of the Array.js PureScript-transpiled file: which is

// ...
exports.range = function (start) {
  // ...

I don't understand what's happening, I'm not familiar with Webpack at all... what could be wrong? Also why can I succesfully use some functions from Prelude (like the basic operators) but not other functions or functions from other modules? For instance I also can't use show, and clicking on the error on the console takes me to the Show.js file with a similar line that assigns the exports.

Any help would be really appreciated,
thank you very much in advance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions