TS2307:找不到模块'class-validator'

时间:2017-04-30 08:52:42

标签: typescript gulp gulp-typescript

我正在尝试在打字稿项目中使用class-validator模块。但是,当我编译typescript时会发出以下警告:

src/main/ts/domain/Order.ts(1,48): error TS2307: Cannot find module 'class-validator'.

它抱怨的界限如下:

  import { IsInt, IsNotEmpty, IsDate, Min } from "class-validator";

我有这样的gulp设置:

    var gulp = require('gulp');
    var ts = require('gulp-typescript');
    var tsProject = ts.createProject("tsconfig.json");

    gulp.task('default', function() {
      return tsProject.src()
        .pipe(tsProject())
        .js.pipe(gulp.dest("target/main/js"));
    });

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

    {
        "files": [
            "src/main/ts/**/*.ts"
        ],
        "compilerOptions": {
            "experimentalDecorators": true,
            "noImplicitAny": true,
            "target": "es6"
        }
    }

思想?

3 个答案:

答案 0 :(得分:1)

"moduleResolution": "node"

中设置tsconfig.json
{
  "compilerOptions": {
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "noImplicitAny": true,
    "target": "es6"
  }
}

您可以在此处找到有关TypeScript如何解析导入模块的更多信息:https://www.typescriptlang.org/docs/handbook/module-resolution.html#node

答案 1 :(得分:1)

除了安装class-validator之外,您还需要安装验证器的打字稿类型。

npm install @types/validator

答案 2 :(得分:0)

这对我有用:yarn add graphql class-validator type-graphql

来源:https://github.com/MichalLytek/type-graphql/issues/625#issuecomment-626368051