Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

151
Views
JavaScript doesnt recognize my custom class from typescript file

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!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

  • Export your class:
export class CourseObject {...}

  • When importing into a .ts file:
import { CourseObject } from 'path/CourseObject'
  • Or when importing into a plain .js file, use require:
const CourseObject = require("./path/CourseObject.js").CourseObject
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!