如何用Node初始化stoqey的 IKBR连接?
为什么README的基本示例无法工作?
// stoqey.js
import { ibkr, MarketDataManager } from '@stoqey/ibkr';
await ibkr();
const mkdManager = MarketDataManager.Instance;
const data = await mkdManager.getHistoricalData(
{
symbol: 'AAPL',
secType: 'STK'
},
null,
'1 D',
'1 D',
'TRADES',
true );
console.log(data);
$ node stoqey.js
file:///C:/x/stoqey.js:3
import { ibkr, MarketDataManager } from '@stoqey/ibkr';
^^^^
SyntaxError: Named export 'ibkr' not found. The requested module '@stoqey/ibkr' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:
import pkg from '@stoqey/ibkr';
const { ibkr, MarketDataManager } = pkg;
at ModuleJob._instantiate (node:internal/modules/esm/module_job:180:21)
at async ModuleJob.run (node:internal/modules/esm/module_job:263:5)
at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:578:26)
at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:116:5)
Node.js v22.14.0
$ cat package.json
{
"type": "module",
"dependencies": {
"@stoqey/ibkr": "^2.5.8"
}
}
$ cat .env
IBKR_HOST=localhost
IBKR_PORT=4001
IBKR_CLIENT_ID=777
DEBUG=ibkr*
解决方案
你看到的错误并不是这个库特有的——Node会默认把.js文件当作CommonJS处理,因此除非项目被配置为ESM,否则import和顶层await将无法工作。
你可以在这里看到一个可用的设置 here。
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。