ts-node
约 1 分钟阅读
推荐使用 tsx笔记tsxesbuild-kit/tsx · MIT, TS · 基于 ESBuild Transform API - 不支持插件 · 支持 ts path · 不支持 emitDecoratorMetadata - esbuild-kit/core-utils#14打开:/notes/web/nodejs/tsx
ts-node 不支持 watch,默认参数可用性低
- TypeStrong/ts-nodeGitHubTypeStrong/ts-node推荐使用 tsx · ts-node 不支持 watch,默认参数可用性低 · TypeStrong/ts-node · node 直接执行 ts · watch · nodemon · ts-node-dev · tsconfig.json#ts-node笔记:ts-node
- node 直接执行 ts
- watch
- nodemon
- ts-node-dev
npx ts-node main.ts # 直接执行 ts
npm i -D tsconfig-paths
npx ts-node -r tsconfig-paths/register main.ts # tsconfig 里的 path 能生效
node -r ts-node/register -r tsconfig-paths/register main.ts # 使用 node
npx ts-node --showConfig
# --swc 隐含 --transpileOnly
npx ts-node --esm --swc --experimentalSpecifierResolution=node \
run.ts # 约等于 tsx run.tsPackagestsconfig-pathsnpm Packagetsconfig-pathsNPM
tsconfig.json#ts-node
{
"extends": "ts-node/node16/tsconfig.json",
// 针对 ts-node 的配置
"ts-node": {
"transpileOnly": true, // 忽略类型检查,更快
"files": true, // 从 tsconfig.json 读取 files, include, exclude
"require": ["tsconfig-paths/register"],
"compilerOptions": {
"module": "commonjs"
}
}
}{
"ts-node": {
"transpileOnly": true,
"swc": true,
"esm": true,
"experimentalSpecifierResolution": "node"
}
}| env | demo |
|---|---|
| TS_NODE_COMPILER_OPTIONS | {"module":"commonjs"} |
| TS_NODE_PROJECT | ./tsconfig.commonjs.json |
| NODE_OPTIONS | --trace-deprecation --abort-on-uncaught-exception |
FAQ
Unknown file extension ".ts"
- TypeStrong/ts-node#1062GitHub IssueTypeStrong/ts-node#1062
检测在 ts-node 运行
var detectTSNode = false;
try {
if (process[Symbol.for("ts-node.register.instance")]) {
detectTSNode = true;
}
} catch() {
}swc import assert
- swc 1.3.83 调整后有问题
- keepImportAssertions
- TypeStrong/ts-node#2056GitHub IssueTypeStrong/ts-node#2056
- TypeStrong/ts-node#2051GitHub IssueTypeStrong/ts-node#2051
关联信息
反向链接、本文链接的其他页面和外部资料。
站内链接
- tsx笔记 · tsx
esbuild-kit/tsx · MIT, TS · 基于 ESBuild Transform API - 不支持插件 · 支持 ts path · 不支持 emitDecoratorMetadata - esbuild-kit/core-utils#14
References
GitHub5 条
- TypeStrong/ts-nodegithub.com/TypeStrong/ts-node
- TypeStrong/ts-node#1062github.com/TypeStrong/ts-node/issues/1062
- TypeStrong/ts-node#2051github.com/TypeStrong/ts-node/issues/2051
- TypeStrong/ts-node#2056github.com/TypeStrong/ts-node/issues/2056
- yorickdevries/detect-ts-node/index.jsgithub.com/yorickdevries/detect-ts-node/blob/master/index.js