【问题标题】:mocha and ts-node cannot find local .d.ts filemocha 和 ts-node 找不到本地 .d.ts 文件
【发布时间】:2019-09-22 20:41:03
【问题描述】:

我有以下项目结构

|_typetests
|          |_type.test.ts
|
|
myproj.d.ts
tsconfig.json

我的 tsconfig.json 看起来像这样:

{
  "compilerOptions": {
      "module": "commonjs",
      "moduleResolution": "node",
      "lib": [
          "es6"
      ],
      "target": "es6",
      "noImplicitAny": true,
      "noImplicitThis": true,
      "strictNullChecks": true,
      "types": [
          "node",
          "mocha"
      ],
      "noEmit": true,
      "forceConsistentCasingInFileNames": true,
      "baseUrl": "./"
  },
  "include": [
      "types/*.test.ts"
  ],
  "exclude": ["node_modules"]
}

如果我运行./node_modules/.bin/tsc -p . --traceResolution

然后我可以看到:

模块名称“myproj”已成功解析为“/Users/paulcowan/projects/myproj/myproj.d.ts”。 ========

但是当我通过 mocha 运行以下命令时

./node_modules/.bin/mocha -r ts-node/register types/*.test.ts

Error: Cannot find module 'myproj'

【问题讨论】:

    标签: typescript mocha.js ts-node


    【解决方案1】:

    尝试使用--files 标志来解决您的问题。

    或者将TS_NODE_FILES环境变量设置为true再试一次

    TS_NODE_FILES=true ./node_modules/.bin/mocha -r ts-node/register types/*.test.ts

    【讨论】:

    • 仍然没有雪茄,有什么我可以更好地调试这个,我不认为它正在拾取 tsconfig.json
    • @dagda1 尝试将typeRoots 选项添加到您的 tsconfig 文件中。 "typeRoots": ["./node_modules/@types", "./myproj.d.ts"] 再试一次,没有我的选择。
    • 同样的交易,我不认为它正在接收 tsconfig.json
    • TS_NODE_FILES 为我工作;我把它放在package.json的脚本命令中:"scripts": { "test": "TS_NODE_FILES=true mocha -r ts-node/register tests/**/*-test.ts -r ts-node/register tests/*-test.ts"}
    猜你喜欢
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 2020-07-02
    • 2019-06-27
    • 2019-01-07
    • 2021-02-10
    • 2020-11-23
    • 2017-04-16
    相关资源
    最近更新 更多