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

143
Vistas
How to use TypesSript in JavaScript?

How to require a TypeScript class in a Node CommonJS JavaScript file?

Working with mongoose, in my TS code, I do:

// user.model.ts
export const UserModel = model<User>('User', schema);

In my JS code:

// user.controller.js
const UserModel = require('./user.model');

But I receive the following error message:

node:internal/modules/cjs/loader:936
  throw err;
  ^

Error: Cannot find module './user.model'
code: 'MODULE_NOT_FOUND'

When I add .ts:

// user.controller.js
const UserModel = require('./user.model.ts');
                                     ^^^

The error message changes:

user.model.ts:1
import { Document, model, Schema } from 'mongoose';
^^^^^^

SyntaxError: Cannot use import statement outside a module
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

TypeScript files must be "transpiled" via the TypeScript compiler before they can be run or imported into vanilla JS. If you're working in Node, one of the best ways to do this is to utilize ts-node, which will automatically just-in-time transform your scripts as it runs them. See their docs here.

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