【问题标题】:"Cannot find module" error message with gulp-jasminegulp-jasmine 的“找不到模块”错误消息
【发布时间】:2018-10-22 19:26:06
【问题描述】:

我的 ts 源文件输出在 root/distr 文件夹中。

我的 ts 测试文件输出与源位于同一文件夹中:root/tests

我有这个目录结构:

root
--- distr
   --- fileA.js
   --- api
      --- myapi.js
--- src
   --- fileA.ts
   --- api
       --- myapi.ts
--- tests
    --- unit
        --- specs
            --- api
                --- testA.spec.js
                --- testA.spec.ts

testA.spec.ts内容:

import * as myapi from "api/myapi"; //<= this is where I get the error

describe("API: \"app/version\" route", () => {

    it("should return the version", (done) => {
        expect(200).toBe(200); //on purpose for now
        done();
    });
}

tsconfig.json内容:

"outDir": "../distr",
"baseUrl": ".",
"paths": {
     "*": ["src/*"],
     "api/*": ["src/api/*"],
},

gulpfile.js 内容中,我有:

runSequence('clean:tests', 'build:tests', 'jasmine', function() {
    done();
});


gulp.task('build:tests', function () {
    return gulp.src(['tests/**/*.ts'])
      .pipe(ts())
      .pipe(gulp.dest('tests/'));
});

gulp.task('jasmine', function(done) {

    return gulp.src('tests/**/*.spec.js')
        .pipe(plumber(function (error) {
            console.log(error.toString());
            this.emit('end');
        }))
        .pipe(jasmine());
})

当我运行 gulp 命令时,我得到:

[19:44:25] Starting 'build:tests'...
[19:44:28] Finished 'build:tests' after 2.39 s
[19:44:28] Starting 'jasmine'...
BLA:Error in plugin "gulp-jasmine"
Message:
    Cannot find module 'api/myapi'
Details:
    code: MODULE_NOT_FOUND

知道为什么我会收到此消息吗? 谢谢!

【问题讨论】:

    标签: node.js typescript tslint tsconfig


    【解决方案1】:

    尝试:

    import EngineA from "./engines/engineA"

    (确保本地路径正确)。如果是这样,那么您必须正确配置 "baseUrl": and "rootDirs" intsconfig.json`。祝你好运。

    【讨论】:

    • 我配置了 tsconfig.ts 的路径部分,这样我就不必使用相对路径(类似于“../../../../src/api /myapi") 当我从 src 文件夹而不是测试文件夹构建 ts 时,它工作正常。
    • 我对属性名称有误,已修复。如果你只执行 jasmine 或 tsc 而没有 gulp 会发生什么?像`npx tsc && npx jasmine`?如果这行得通,gulp 会以某种方式弄乱结构.. 抱歉没有太大帮助 - 当事情像这样发生故障时,我倾向于简化并进入基础......
    • 所以我猜你的问题是 gulp 配置 - 不在 tsconfig.json - 你确定要使用 gulp 吗?我相信你有你的理由,gulp 很棒,但是只有 typescript 和 ts-node 依赖以及这两个 npm 脚本,你有运行和观看的经验,我敢肯定比 gulp 更快:“test”:“ts -node node_modules/jasmine/bin/jasmine", "watch": "tsc --watch" 顺便说一句,您不必担心源映射来轻松调试测试,因为您正在“直接”执行 ts 代码。我的两分钱,祝你好运
    猜你喜欢
    • 1970-01-01
    • 2016-06-29
    • 2016-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-29
    • 2016-10-01
    相关资源
    最近更新 更多