patterntypescriptCritical
Typescript ReferenceError: exports is not defined
Viewed 0 times
typescriptexportsreferenceerrordefinednot
Problem
Trying to implement a module following the official handbook, I get this error message:
Uncaught ReferenceError: exports is not defined
at app.js:2
But nowhere in my code do I ever use the name
How can I fix this?
Files
app.ts
module-1.t
tsconfig.json
Uncaught ReferenceError: exports is not defined
at app.js:2
But nowhere in my code do I ever use the name
exports.How can I fix this?
Files
app.ts
let a = 2;
let b:number = 3;
import Person = require ('./mods/module-1');module-1.t
export class Person {
constructor(){
console.log('Person Class');
}
}
export default Person;tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"noImplicitAny": false,
"sourceMap": true,
"outDir": "scripts/"
},
"exclude": [
"node_modules"
]
}Solution
EDIT:
This answer might not work depending if you're not targeting
Original Answer
If CommonJS isn't installed (which defines
As per the comments, this alone may not work with later versions of
Note:
Look at your
It is the root of the error message, and after removing
This answer might not work depending if you're not targeting
es5 anymore, I'll try to make the answer more complete.Original Answer
If CommonJS isn't installed (which defines
exports), you have to remove this line from your tsconfig.json:"module": "commonjs",As per the comments, this alone may not work with later versions of
tsc. If that is the case, you can install a module loader like CommonJS, SystemJS or RequireJS and then specify that.Note:
Look at your
main.js file that tsc generated. You will find this at the very top:Object.defineProperty(exports, "__esModule", { value: true });It is the root of the error message, and after removing
"module": "commonjs",, it will vanish.Code Snippets
"module": "commonjs",Object.defineProperty(exports, "__esModule", { value: true });Context
Stack Overflow Q#43042889, score: 136
Revisions (0)
No revisions yet.