After compiled ts to js es5 and run html/js i am getting an error in the browser:

after compiled ts to js es5 and run html/js i am getting an error in the browser: // follows ts code… actually i am trying use export but failed

sudokuSolver.js?2019-11-20a:2 Uncaught ReferenceError: exports is not defined

"use strict";
exports.__esModule = true;
var UNASSIGNED = 0;
function usedInRow(matrix, row, num) {
    for (var col = 0; col < matrix.length; col++) {
        if (matrix[row][col] === num) {
            return true;
        }
    }
    return false;
}
function usedInCol(matrix, col, num) {
    for (var row = 0; row < matrix.length; row++) {
        if (matrix[row][col] === num) {
            return true;
        }
    }
    return false;
}
function usedInBox(matrix, boxStartRow, boxStartCol, num) {
    for (var row = 0; row < 3; row++) {
        for (var col = 0; col < 3; col++) {
            if (matrix[row + boxStartRow][col + boxStartCol] === num) {
                return true;
            }
        }
    }
    return false;
}
function isSafe(matrix, row, col, num) {
    return (!usedInRow(matrix, row, num) &&
        !usedInCol(matrix, col, num) &&
        !usedInBox(matrix, row - (row % 3), col - (col % 3), num));
}
function solveSudoku(matrix) {
    var row = 0;
    var col = 0;
    var checkBlankSpaces = false;
    for (row = 0; row < matrix.length; row++) {
        for (col = 0; col < matrix[row].length; col++) {
            if (matrix[row][col] === UNASSIGNED) {
                checkBlankSpaces = true;
                break;
            }
        }
        if (checkBlankSpaces === true) {
            break;
        }
    }
    if (checkBlankSpaces === false) {
        return true;
    }
    for (var num = 1; num <= 9; num++) {
        if (isSafe(matrix, row, col, num)) {
            matrix[row][col] = num;
            if (solveSudoku(matrix)) {
                return true;
            }
            matrix[row][col] = UNASSIGNED;
        }
        //  else {
        //   //return true;
        //   //break;   // added
        //   noSolution = true;
        // }
    }
    return false;
}
function hasSolution(matrix) {
    var row = 0;
    var col = 0;
    var checkBlankSpaces = false;
    var noSolution = false;
    for (row = 0; row < matrix.length; row++) {
        for (col = 0; col < matrix[row].length; col++) {
            if (matrix[row][col] === UNASSIGNED) {
                checkBlankSpaces = true;
                break;
            }
        }
        if (checkBlankSpaces === true) {
            break;
        }
    }
    if (checkBlankSpaces === false) {
        return true;
    }
    for (var num = 1; num <= 9; num++) {
        if (isSafe(matrix, row, col, num)) {
            matrix[row][col] = num;
            if (solveSudoku(matrix)) {
                return true;
            }
            matrix[row][col] = UNASSIGNED;
        }
        //  else {
        //   //return true;
        //   //break;   // added
        //   noSolution = true;
        // }
    }
    return false;
}
function sudokuSolver(matrix) {
    if (solveSudoku(matrix) === true) {
        return matrix;
    }
    else {
        alert('NO SOLUTION EXISTS!'); // added
        return 'NO SOLUTION EXISTS!';
    }
}
exports.sudokuSolver = sudokuSolver;

// ts below

  const UNASSIGNED = 0;

  function usedInRow(matrix, row, num) {
    for (let col = 0; col < matrix.length; col++) {
      if (matrix[row][col] === num) {
        return true;
      }
    }
    return false;
  }
  
  function usedInCol(matrix, col, num) {
    for (let row = 0; row < matrix.length; row++) {
      if (matrix[row][col] === num) {
        return true;
      }
    }
    return false;
  }
  
  function usedInBox(matrix, boxStartRow, boxStartCol, num) {
    for (let row = 0; row < 3; row++) {
      for (let col = 0; col < 3; col++) {
        if (matrix[row + boxStartRow][col + boxStartCol] === num) {
          return true;
        }
      }
    }
    return false;
  }
  
  function isSafe(matrix, row, col, num) {
    return (
      !usedInRow(matrix, row, num) &&
      !usedInCol(matrix, col, num) &&
      !usedInBox(matrix, row - (row % 3), col - (col % 3), num)
    );
  }
  function solveSudoku(matrix) {
    let row = 0;
    let col = 0;
    let checkBlankSpaces = false;
  
    for (row = 0; row < matrix.length; row++) {
      for (col = 0; col < matrix[row].length; col++) {
        if (matrix[row][col] === UNASSIGNED) {
          checkBlankSpaces = true;
          break;
        }
      }
      if (checkBlankSpaces === true) {
        break;
      }
    }
    if (checkBlankSpaces === false) {
      return true;
    }
  
    for (let num = 1; num <= 9; num++) {
      if (isSafe(matrix, row, col, num)) {
        matrix[row][col] = num;
        if (solveSudoku(matrix)) {
          return true;
        }
        matrix[row][col] = UNASSIGNED;
      }
      //  else {
      //   //return true;
      //   //break;   // added
      //   noSolution = true;
      // }
    }
    return false;
  }
  
  function hasSolution(matrix) {
    let row = 0;
    let col = 0;
    let checkBlankSpaces = false;
    let noSolution = false;
  
    for (row = 0; row < matrix.length; row++) {
      for (col = 0; col < matrix[row].length; col++) {
        if (matrix[row][col] === UNASSIGNED) {
          checkBlankSpaces = true;
          break;
        }
      }
      if (checkBlankSpaces === true) {
        break;
      }
    }
    if (checkBlankSpaces === false) {
      return true;
    }
  
    for (let num = 1; num <= 9; num++) {
      if (isSafe(matrix, row, col, num)) {
        matrix[row][col] = num;
        if (solveSudoku(matrix)) {
          return true;
        } 
        matrix[row][col] = UNASSIGNED;
      }
      //  else {
      //   //return true;
      //   //break;   // added
      //   noSolution = true;
      // }
    }
    return false;
    
  }
  
  
  
  
  export function sudokuSolver (matrix) {
      if (solveSudoku(matrix) === true) {
        return matrix;
      } else {
        alert('NO SOLUTION EXISTS!') ;  // added
        return 'NO SOLUTION EXISTS!';
      }
      
    }
  
  

I’ve not done much with TypeScript, but to make the error go away, all you’d need to do is change:

exports.__esModule = true;

to:

var exports = {"__esModule": true};

As mentioned here:

Does that help?

Looks like a compiler issue to me. What are you using to compile it?

Post your compiler configs if you can.

I use
tsc

Looks like you’re compiling your code to CommonJS modules; if you want your code to work in the browser, compile to UMD instead (and / or use an actual module bundler).

https://www.typescriptlang.org/docs/handbook/modules.html#code-generation-for-modules

PS: Another option would be to compile to AMD with the outFile option specified, in which case TSC can act as a module bundler as well.

1 Like