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

114
Views
Mix Node's CommonJS and Typescript's ES modules

I have a legacy app in Node.js v10 (but upgrading is an option) which uses CommonJS modules (require).
The codebase is written in JS.
Now, I want to refactor part of the project and write it in Typescript using TS modules (import/export) and classes.
What is the best way to interface those newly generated JS files (compiled from TS) from my legacy JS code?

Say I have a class in TS:

export default class DemoClass {

public static sayHello() {
    console.log("Hello from demo class...");
}

}

which gets compiled to:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var DemoClass = /** @class */ (function () {
    function DemoClass() {
    }
    DemoClass.sayHello = function () {
        console.log("Hello from demo class...");
    };
    return DemoClass;
}());
exports.default = DemoClass;
//# sourceMappingURL=DemoClass.js.map

I have set "module": "commonjs" in my tsconfig.json.

Now, how do I require that DemoClass.js from my legacy app.js and e.g. invoke sayHello()?
The following code seems to work, but this "default" stuff does not seem right:

var DemoClass = require('./lib/DemoClass');
DemoClass.default.sayHello();
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Instead of using export default, use export =.

export = class DemoClass {
    public static sayHello() {
        console.log("Hello from demo class...");
    }
}

Playground link

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!