Wener Notes

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
bash
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.ts
Packagestsconfig-pathsnpm Packagetsconfig-pathsNPM

tsconfig.json#ts-node

tsconfig.json
{
  "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"
    }
  }
}
json
{
  "ts-node": {
    "transpileOnly": true,
    "swc": true,
    "esm": true,
    "experimentalSpecifierResolution": "node"
  }
}
envdemo
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"

检测在 ts-node 运行

TypeScript
var detectTSNode = false;

try {
    if (process[Symbol.for("ts-node.register.instance")]) {
        detectTSNode = true;
    }
} catch() {
}

swc import assert

关联信息

反向链接、本文链接的其他页面和外部资料。

站内链接

  • tsx
    笔记 · tsx

    esbuild-kit/tsx · MIT, TS · 基于 ESBuild Transform API - 不支持插件 · 支持 ts path · 不支持 emitDecoratorMetadata - esbuild-kit/core-utils#14

References

GitHub5 条
最近更新commit 1866667Edit

On this page