So right now I'm simply testing to see if I can create and store some info in a Javascript file using a custom TypeScript class object. Before testing I'm running the command 'tsc courses.ts' followed by 'node app.js' It says its working and running on the port I specified but when I go to the page it break and says there is a reference error when doing 'let aCourse = new CourseObject' CourseObject is not defined.
Any ideas on how I might fix this?
courses.ts
class CourseObject {
courseNumber: number;
courseName: string;
courseDescription: string;
credits: number;
preRequisites: string;
constructor(courseNumber: number, courseName: string, courseDescription: string,
credits: number, preRequisites: string){
this.courseNumber = courseNumber;
this.courseName = courseName;
this.courseDescription = courseDescription;
this.credits = credits;
this.preRequisites = preRequisites;
}
}
app.js
... omitted for brevity ...
response.writeHead(200, {"Content-Type": "text/html"});
let courses = [];
let aCourse = new CourseObject();
... omitted for brevity ...
EDIT FIXED ran tsc --init to generatea tsconfig.json file. module: commonjs and target es2016
then changed code to: courses.ts
export class CourseObject{ ... }
and app.js added
const CourseObject = require(PATH/courses.js).CourseObject
Thanks @CRice for the help!
export class CourseObject {...}
.ts file:import { CourseObject } from 'path/CourseObject'
.js file, use require:const CourseObject = require("./path/CourseObject.js").CourseObject