【问题标题】:tsc not creating the dist foldertsc 没有创建 dist 文件夹
【发布时间】:2020-03-22 03:49:43
【问题描述】:

全部在标题中,试图将我的 TypeScript 项目编译到 ./bin 文件夹,tsc 命令执行没有错误导致没有创建任何内容,不知道为什么。

我的 tsconfig.json

{
  "compilerOptions": {
    "allowJs": true,
    "checkJs": false,
    "resolveJsonModule": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "lib": ["es6", "es7", "dom", "esnext"],
    "module": "commonjs",
    "moduleResolution": "node",
    "noEmit": true,
    "noImplicitAny": false,
    "outDir": "bin",
    "removeComments": true,
    "sourceMap": true,
    "target": "es2017",
    "rootDirs": ["src/", "config/"],
    "typeRoots": ["./node_modules/@types", "./typings"]
  },
  "include": ["src/**/*", "./typings"],
  "exclude": ["node_modules", "bin", "**/__mocks__*", "**/*.spec.**", "test", "assets"]
}

在我的 package.json 中,这是我要编译的脚本:

"scripts": {
    "build-ts": "tsc",
    "watch-ts": "tsc -w",
  },

我的项目结构:

rootdir
    |
    |-----src
    |      |----server.ts
    |      |----app.ts
    |-----test
    |-----node_modules
    |-----typings
    |-----config
    |-----tsconfig.json
    |-----package.json

知道我做错了什么吗?

【问题讨论】:

  • 从最明显的开始:您是否有权在该目录中创建文件夹?外壳中的任何错误?当您在提示符下直接运行tsc 时会发生什么?浏览一下您的配置,它似乎没问题。
  • 当我运行命令时,它执行时没有错误
  • 您需要在您的build-ts 脚​​本中删除"noEmit": true 或传递--noEmit false
  • @RemcoHaszing 是的,就是这样 :) 谢谢,如果你写了答案,我会投票

标签: javascript node.js typescript


【解决方案1】:

noEmit 选项导致 TypeScript 不发出任何文件。您需要在 build-ts 脚本中删除 "noEmit": true 或传递 --noEmit false。

额外提示:将脚本重命名为 prepack 以在您运行 npm packnpm publish 时让 npm 为您编译 TypeScript。

【讨论】:

  • 谢谢!这也对我有用。我想知道它是如何到达那里的?我猜一些包/脚本添加了它......关于重命名,你的意思是在 package.json 中用“prepack”字面替换“脚本”?我也是 npm 的新手并做出反应
【解决方案2】:

TypeScript 可能无法创建dist 文件夹的另一种情况是,当tsconfig.json 中的compilerOptions 中设置了"incremental": true"composite": true,并且您删除了dist 文件夹而不删除tsconfig.tsbuildinfo跟踪增量构建。

如果您保留tsconfig.tsbuildinfo 并删除或修改dist,编译器将不需要输出任何内容,因为tsconfig.tsbuildinfo 告诉它没有工作要做。问题是tsconfig.tsbuildinfo 引用了dist 的旧状态。

假设您的npm run clean 命令运行rimraf dist。您还必须将其更新为 rimraf tsconfig.tsbuildinfo

【讨论】:

  • 在我的例子中是"composite": true设置,它隐式设置"incremental": true
  • 我的天啊,我是这个问题的原始发帖人,2 年的信,我遇到了同样的问题,但这次是复合的,LOOL ...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-22
  • 1970-01-01
  • 2021-08-21
  • 2017-01-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多