简体   繁体   中英

TS2307: Cannot find module 'class-validator'

I'm attempting to use the class-validator module in a typescript project. However when I compile typescript issues the following warning:

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

The line it is complaining about looks like this:

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

I have gulp setup like this:

    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"));
    });

And my tsconfig.json looks like this:

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

Thoughts?

Set "moduleResolution": "node" in your tsconfig.json :

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "noImplicitAny": true,
    "target": "es6"
  }
}

You can find more info on how TypeScript resolves your imported modules here: https://www.typescriptlang.org/docs/handbook/module-resolution.html#node

This worked for me: yarn add graphql class-validator type-graphql

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

You need to install typescript types for validator in addition to installing class-validator.

That is npm install @types/validator

You need ti install class-validator dependency, use this commond:

npm install class-validator

You need to install call-validator and class-transformer packages

npm i --save class-validator class-transformer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM