1.安装typescript

pnpm add -g typescript
#查看版本号
tsc --version

2.安装@types/node

pnpm add -D @types/node

3.创建tsconfig.json

{
    "compilerOptions": {
        "target": "es6",
        "module": "ES6",
        "outDir": "./dist",
        "rootDir": "./src",
        "strict": true,
        "moduleResolution": "node",
        "esModuleInterop": true,
    },
    "include": [
        "./src/**/*.ts"
    ],
    "exclude": [
        "./node_modules"
    ]
}

4.执行tsc可将src目录的ts生成js到dist目录

tsc && node ./dist/test

如果无法使用import方式引入库可在package.json添加type=module

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "type": "module",
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "cheerio": "^1.0.0"
  },
  "devDependencies": {
    "@types/node": "^22.7.3"
  }
}