I'm currently working on developing a TypeScript application where each class is saved in its own .ts file. I prefer to use VS Code for this project. So far, my compile task seems to be functioning correctly (transpiling .ts files into .js files). However, when trying to load my main.js file into my HTML page, I encounter the following JavaScript error:
Can't find variable: require
The TypeScript code snippet is as follows:
// __________ car.ts __________
class Car {
color: string;
constructor(color: string) {
this.color = color;
console.log("created a new " + color + " car");
}
}
export = Car;
// __________ main.ts __________
import Car = require('car');
class Startup {
public static main(): number {
console.log('Hello World');
var c = new Car("red");
return 0;
}
}
Here's a glimpse at my tsconfig file:
{
"compilerOptions": {
"module": "commonjs",
"sourceMap": true
},
"files": [
"car.ts",
"main.ts"
]
}
I seem to be missing a crucial step here. Why does JavaScript require something like 'require'? Is there an alternative approach to working with classes stored in separate files?