Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

156
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda