Search code examples
javascripttypescripttsc

Uncaught ReferenceError: exports is not defined //year 2020


I'm stuck. I tried many solutions but none of them works. Trying to implement a module, I get this error message:

Uncaught ReferenceError: exports is not defined

I tried solution from Stack Overflow, but it does not work. Changing anything in tsconfing does not make any difference. I added var exports = {}; into scripts in HTML file and error changes from "exports" to "require" - dead end.

server node:

var fs = require('fs');
const express = require('express');
var path = require('path');
var { request } = require('http');
var app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.listen(3533);

html:

   <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="description" content="testing site">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" type="text/css" href="style.css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>simple testin </title>
</head>
<body >
</body>
<script type="text/javascript" src="webscripts/run_scripts.js"></script>
<script type="text/javascript" src="webscripts/library.js"></script>  
</html>

library.ts:

export function appearSomeTestingSentence()  {
    document.write('111111');
};

library.js:

  "use strict";
exports.__esModule = true;
exports.appearSomeTestingSentence = void 0;
exports.appearSomeTestingSentence = function () {
    document.write('111111');
};

run_scripts.ts:

import {appearSomeTestingSentence} from './library.js';
appearSomeTestingSentence();

run_scripts.js:

"use strict";
exports.__esModule = true;
var library_1 = require("./library");
library_1.appearSomeTestingSentence();

Solution

  • Finally i sort it out. I had to delete *.js files before i compile with tsc and i had to restart tsc after changing something in tsconfig. Problem solved after changing module to"module" : "ESNext" in tsconfig file.