quantlibjs / ql

1 stars 0 forks source link

Support for ES Module Import in `@quantlib/ql #1

Open nguyennhukhanh opened 1 month ago

nguyennhukhanh commented 1 month ago

Title: Support for ES Module Import in @quantlib/ql

Description:

I encountered an error while predicting the next price using the @quantlib/ql library. The error message indicates that the ES Module is not supported with the require() function. Below are the details of the error:

Error predicting next price: Error [ERR_REQUIRE_ESM]: require() of ES Module ..\node_modules\@quantlib\ql\ql.mjs not supported.
Instead change the require of ..\node_modules\@quantlib\ql\ql.mjs to a dynamic import() which is available in all CommonJS modules.
    at ..\dist\src\modules\quant_trading\test.service.js:70:103
    at async TestBinanceService.predictNextPrice (..\dist\src\modules\quant_trading\test.service.js:70:30)
    at async QuantTradingController.tbinancePredictNextPrice (..\dist\src\modules\quant_trading\quant_trading.controller.js:40:16) {
  code: 'ERR_REQUIRE_ESM'
}

Steps to Reproduce:

  1. Attempt to use the @quantlib/ql library in a CommonJS module.
  2. Call the predictNextPrice function.
  3. Observe the error.

Expected Behavior: The library should support dynamic import for ES Modules when used in CommonJS modules.

Actual Behavior: The library throws an error indicating that require() of ES Module is not supported.

Suggested Solution: Update the library to support dynamic import for ES Modules. This can be done by replacing the require() statements with dynamic import() statements.

Environment:

Additional Context: Here is the stack trace for reference:

❌ [ExceptionFilter][error] - [2024-09-18 04:31:58]: Body: {}
❌ [ExceptionFilter][error] - [2024-09-18 04:31:58]: Query: {"symbol":"BTCUSDT","interval":"5m","predictionInterval":"1440","predictionCount":"100"}
❌ [ExceptionFilter][error] - [2024-09-18 04:31:58]: Params: {}
❌ [ExceptionFilter][Error] - [2024-09-18 04:31:58]: [Error] require() of ES Module ..\node_modules\@quantlib\ql\ql.mjs not supported.        
Instead change the require of ..\node_modules\@quantlib\ql\ql.mjs to a dynamic import() which is available in all CommonJS modules.{ code: 'ERR_REQUIRE_ESM', [Symbol(level)]: 'error' }
Stack Trace:
Error [ERR_REQUIRE_ESM]: require() of ES Module ..\node_modules\@quantlib\ql\ql.mjs not supported.
Instead change the require of ..\node_modules\@quantlib\ql\ql.mjs to a dynamic import() which is available in all CommonJS modules.
    at ..\dist\src\modules\quant_trading\test.service.js:70:103
    at async TestBinanceService.predictNextPrice (..\dist\src\modules\quant_trading\test.service.js:70:30)
    at async QuantTradingController.tbinancePredictNextPrice (..\dist\src\modules\quant_trading\quant_trading.controller.js:40:16)
cjbd commented 4 weeks ago

@nguyennhukhanh, pls refer to the README for nodejs part, instead of use require, try import