Closed Enteleform closed 3 years ago
.mts
and .cts
extensions.mts
/.cts
file through an import to its .mjs
/.cjs
would-be outputtsconfig.json
file present, which means it can be run/installed globallytypescript
as a peer dependencytsm rewrites module formats on the fly so that you can use tsm
in any way that suits your toolchain without compromising the semantics of your code. AKA, it allows you to handle this test scenario, which would fail with ts-node
(or even with esm
if it were there were no TS involvement)
// src/math.mjs
// or src/math.js w/ type: module
export const sum = (a, b) => a + b;
// test/math.ts
import * as assert from 'assert';
import * as math from '../src/math.mjs';
assert.equal(math.sum(1, 2), 3);
run it via:
$ node -r tsm test/math.ts # works
$ node -r ts-node/register test/math.ts # fails
$ node -r ts-node/register/transpile-only test/math.ts # fails
$ tsm test/math.ts # success
$ ts-node --transpile-only test/math.ts # fails
$ node --loader tsm test/math.ts # success
$ node --loader ts-node/esm/transpile-only test/math.ts # fails UNLESS:
# - you have `"type": "module"` in package.json
# - you have `tsconfig.json` file with `"module": "esnext"` or equivalent
Overall, tsm is much faster and does not have all the same friction issues. It's effectively a blend between a bundler and a runtime transformer, offering complete flexibility (for extensions & for tooling integration) and without compromising any of the import/export semantics of your files.
I think something like this belongs to the readme file
What use cases & functionality does
tsm
provide thatts-node
does not?In what cases would it be better to prefer one over the other?