'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 [email protected] without any problem.
but it's problem is by type of ESM of auto-bind module.
how can fix this?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
