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

141
Views
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 answers
Answer question

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 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!