Overview
Currently, I am running a TypeScript program from this repository.
node --loader ts-node/esm ./examples/ts/cli.ts bitget fetchBalance
The TypeScript version of this program takes significantly longer to run compared to the JavaScript version.
node --loader ts-node/esm ./examples/ts/cli.ts bitget fetchBalance 14.73s user 0.60s system 153% cpu 10.010 total
node ./examples/js/cli.js bitget fetchBalance 0.49s user 0.07s system 14% cpu 3.756 total
I am exploring ways to reduce the runtime of this program and one area I am investigating is the way in which TypeScript is being executed.
TSX Error
Prior to executing a git reset --hard
, TSX was functioning properly. However, after the reset, I encountered the following error despite having installed JSBN:
% tsx ./examples/ts/cli.ts bitget fetchBalance
node:internal/errors:484
ErrorCaptureStackTrace(err);
^
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/.../ccxt/ts/src/static_dependencies/jsencrypt/lib/jsbn/jsbn.js' imported from /.../ccxt/ts/src/static_dependencies/jsencrypt/lib/jsrsasign/asn1-1.0.js
at __node_internal_captureLargerStackTrace (node:internal/errors:484:5)
at new NodeError (node:internal/errors:393:5)
...
Node.js v18.12.0
ts-node Issue
Attempting to use ts-node
resulted in the following error:
% ts-node ./examples/ts/cli.ts bitget fetchBalance
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /.../ccxt/examples/ts/cli.ts
Solution Suggestions from Stack Overflow
Issue with Solution 1
If I remove
"type": "module",
from package.json
, it leads to another error:
import fs from 'fs';
^^^^^^
SyntaxError: Cannot use import statement outside a module
Problem with Solution 2
"compilerOptions": {
"esModuleInterop": true,
}
This setting already exists in my tsconfig.json
file.
- These issues occur when using
ts-node
orts-node-esm
ts-node-esm Challenge
When attempting to utilize tsx-node-esm
, I encountered the following errors:
% ts-node-esm ./examples/ts/cli.ts bitget fetchBalance
/usr/local/lib/node_modules/ts-node/src/index.ts:859
...
}