我正在尝试将Nest-class v7包与NestJS v7一起使用。 class-transformer软件包与npm i class-transformer --save
一起安装。我已按照readme.md
该软件包确实有一个打字稿定义,我可以在我的文件系统上(在node_modules内的正确目录中)看到它,VS Code可以很好地导航到该定义,所以我完全困惑为什么它不起作用。我试图删除我的node_modules目录,清除我的NPM缓存,然后再次使用npm install
重新安装所有内容。
当我尝试编译时,我只会得到Cannot find module 'class-transformer' or its corresponding type declarations.
我的实体代码:
import {
Column,
Entity,
Index,
OneToMany,
PrimaryGeneratedColumn,
} from "typeorm";
import { Expose } from 'class-transformer';
@Entity("country", { schema: "my_schema" })
export class Country {
@PrimaryGeneratedColumn({ type: "int", name: "id" })
id: number;
// Some properties here
@Expose()
get name(): string {
return this.someProperty.content;
}
...
}
我的tsconfig.json:
{
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": false,
"outDir": "./dist",
}
}
我的package.js:
{
"name": "project-name",
"version": "0.0.1",
"description": "",
"author": "",
"private": true,
"license": "UNLICENSED",
"scripts": {
"prebuild": "rimraf dist",
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug 0.0.0.0:9229 --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@nestjs/common": "^7.4.2",
"@nestjs/config": "^0.5.0",
"@nestjs/core": "^7.0.0",
"@nestjs/platform-express": "^7.0.0",
"@nestjs/typeorm": "^7.1.0",
"class-transformer": "^0.3.1",
"mysql": "^2.18.1",
"rimraf": "^3.0.2",
"rxjs": "^6.5.4",
"typeorm": "^0.2.25"
},
"devDependencies": {
"@nestjs/cli": "^7.0.0",
"@nestjs/schematics": "^7.0.0",
"@nestjs/testing": "^7.0.0",
"@types/express": "^4.17.3",
"@types/jest": "25.2.3",
"@types/node": "^13.9.1",
"@types/supertest": "^2.0.8",
"@typescript-eslint/eslint-plugin": "3.0.2",
"@typescript-eslint/parser": "3.0.2",
"eslint": "7.2.0",
"eslint-config-prettier": "^6.10.0",
"eslint-plugin-import": "^2.20.1",
"jest": "26.0.1",
"prettier": "^1.19.1",
"supertest": "^4.0.2",
"ts-jest": "26.1.0",
"ts-loader": "^6.2.1",
"ts-node": "^8.6.2",
"tsconfig-paths": "^3.9.0",
"typescript": "^3.7.4"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
我花了两个令人沮丧的时间试图弄清为什么会这样。这是我遇到过此类问题的唯一软件包,并且查看我的配置,软件包本身以及其他所有内容,对我来说毫无意义。