解决路径tsconfig的nestjs有问题

时间:2019-12-17 15:24:56

标签: typescript nestjs tsconfig

我使用NestJS 6.12.9和Typescript 3.6.3。我有tsconfig.json文件:

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es2017",
    "sourceMap": true,
    "noEmit": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "moduleResolution": "node",
    "paths":{
      "@Models/*":["src/database/entities/*"],
      "@Commons/*":["src/common/*"]
    }
  },
  "exclude": ["node_modules", "dist"],
  "include": [
    "./src/**/*"
  ]
}

在文件jwt.service.ts中,我有代码:

import { User } from "@Models/user.entity";

当我运行npm run start:dev时,它将内置到javascript中,并从“ @ Models / user.entity”中导入{User}

const user_entity_1 = require("../../../../../../../D:\\Project\\MyJob\\Backend\\src\\database\\entities\\user.entity");

并出现错误:

  

内部/模块/cjs/loader.js:638       犯错       ^

     

错误:找不到模块'../../../../../../../D:\Project\MyJob\Backend\src\database\entities\user.entity'ntity '       在Function.Module._resolveFilename(内部/模块/cjs/loader.js:636:15)       在Function.Module._load(内部/模块/cjs/loader.js:562:25)       在Module.require(internal / modules / cjs / loader.js:692:17)       在要求时(内部/模块/cjs/helpers.js:25:18)       在对象。 (D:\ Project \ MyJob \ Backend \ dist \ shared \ auth \ jwt.service.js:25:23)       在Module._compile(内部/模块/cjs/loader.js:778:30)       在Object.Module._extensions..js(内部/模块/cjs/loader.js:789:10)       在Module.load(internal / modules / cjs / loader.js:653:32)       在tryModuleLoad(内部/模块/cjs/loader.js:593:12)       在Function.Module._load(internal / modules / cjs / loader.js:585:3)

它仅在我使用时运行:

const user_entity_1 = require("../..\\database\\entities\\user.entity");

1 个答案:

答案 0 :(得分:0)

我在 nestjs 中遇到了同样的问题并像这样解决了这个问题...看图片

enter image description here