You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Node.js+TypeScript+Express开发,tsc打包无输出问题求助

问题排查:tsc无输出但nodemon可正常运行

我使用Node.js、TypeScript和Express开发应用,执行tsc命令期望将代码打包至dist目录,但执行后无任何输出;不过执行nodemon命令时程序可正常运行。以下是相关核心信息及排查结论:

package.json 内容

{
  "name": "server",
  "version": "1.0.0",
  "description": "",
  "main": "dist/app.js",
  "scripts": {
    "server": "nodemon",
    "start": "node dist/app.js",
    "dev": "nodemon src/app.ts",
    "prebuild": "tslint -c tslint.json -p tsconfig.json --fix",
    "build": "tsc",
    "tsc": "tsc",
    "build3": "webpack --config webpack.config.js",
    "depend": "cmd.exe /c 'npx dependency-cruiser --format json > dependencies.json'"
  },
  "author": "",
  "dependencies": {
    "@babel/core": "^7.17.8",
    "@babel/node": "^7.20.0",
    "@types/jest": "^29.2.0",
    "axios": "^0.21.1",
    "babel-loader": "8.2.4",
    "eslint": "^8.13.0",
    "express": "^4.17.3",
    "iconv-lite": "^0.6.3",
    "json-loader": "^0.5.7",
    "lodash": "^4.17.21",
    "moment": "^2.29.4",
    "node-tesr": "0.0.9",
    "puppeteer": "^13.7.0",
    "sqlite3": "^5.1.2",
    "ts-node": "^10.9.1"
  },
  "devDependencies": {
    "@babel/plugin-proposal-class-properties": "7.16.7",
    "@babel/plugin-proposal-decorators": "7.17.8",
    "@babel/preset-env": "7.16.11",
    "@ffprobe-installer/ffprobe": "^1.4.1",
    "@types/node": "^18.15.3",
    "body-parser": "^1.20.0",
    "cheerio": "^1.0.0-rc.10",
    "cross-env": "7.0.3",
    "dependency-cruiser": "^12.10.1",
    "eslint-import-resolver-alias": "^1.1.2",
    "eslint-webpack-plugin": "^3.1.1",
    "file-loader": "6.2.0",
    "get-video-duration": "^4.0.0",
    "log4js": "^6.4.6",
    "node-ffprobe": "^3.0.0",
    "request": "^2.88.2",
    "tesseract.js": "^2.1.5",
    "ts-loader": "^9.4.2",
    "tsconfig-paths": "^4.1.2",
    "tslint": "^6.1.3",
    "typescript": "^4.9.5",
    "uglifyjs-webpack-plugin": "^2.2.0",
    "webpack": "^5.76.1",
    "webpack-cli": "^5.0.1"
  }
}

tsconfig.json 内容

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "outDir": "dist",
    "rootDir": ".",
    "baseUrl": ".",
    "paths": {
      "@src/*": ["./src/*"],
      "@base/*": ["./*"]
    },
    "lib": ["es6", "DOM"],
    "moduleResolution": "node",
    "strict": false,
    "sourceMap": true,
    "skipLibCheck": true,
    "allowJs": true,
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "forceConsistentCasingInFileNames": true,
    "isolatedModules": false,
    "noImplicitAny": false,
    "noEmit": true,
    "experimentalDecorators": true,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "noImplicitReturns": false,
    "noFallthroughCasesInSwitch": false,
    "noImplicitUseStrict": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules"]
}

项目目录结构

-server(root)
  --src
    ---pages
      ----common
    app.ts
  --dist
  --package.json
  --tsconfig.json

问题原因

  1. 核心原因:tsconfig.json中配置了"noEmit": true,这个选项会让TypeScript仅执行类型检查,完全不生成任何编译输出文件,所以执行tsc后dist目录没有内容。
  2. nodemon可正常运行的原因:dev脚本使用nodemon src/app.ts,实际通过ts-node直接在内存中编译并执行TypeScript代码,不需要生成物理文件,因此不受noEmit配置的影响。

修复方案

  1. 修改tsconfig.json中的noEmit配置,将其改为false:
"noEmit": false
  1. 执行npm run build或直接tsc命令,此时TypeScript会正常将src目录下的代码编译输出到dist目录。
  2. (可选优化)当前rootDir设为.(项目根目录),可能导致编译时意外包含非src目录的文件。建议将rootDir改为./src,确保仅编译src目录内的代码:
"rootDir": "./src"

内容的提问来源于stack exchange,提问作者huo liu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 07:57:58