• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

244
Views
error in using ESM Package of "auto-bind" in node.js project

I have a Node.js project.

I want to use auto-bind package @v5.0.0.

The versions above v4 , are ESM type. so can't use then by require('') . but my Node.js project is based on Commonjs Modules. So i can't use of import way.

my problem: i used of bellow way to use of this package:

const autoBind = (...args) => import('auto-bind').then(({default: autoBind}) => autoBind(...args));

but the module of controller.js do not send this binding method to homeController.js module.

i want to put, auto-bind in controller.js module, then use it in homeController.js module, so in result, i can send message method to constructor of homeController:

the homeController.js module:

const controller = require('app/http/controllers/controller.js');
module.exports = new class homeController extends controller{
index(req,res){
    res.send(this.message());
}
message(){
    return('<p> home!</p>');
}
}

the controller.js module:

const autoBind = (...args) => import('auto-bind').then(({default: autoBind}) => autoBind(...args));
module.exports = class controller {
    constructor(){
        autoBind(this);
    }
}

when i call it's adress in browser, it return this erroe:

TypeError: Cannot read properties of undefined (reading 'message')

note: this works by auto-bind@v1.2.0 without any problem. but it's problem is by type of ESM of auto-bind module. how can fix this?

almost 3 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error