Currently, I am working with NestJS 6.12.9 and Typescript 3.6.3 in my project. The configuration file tsconfig.json looks like this:
{
"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/**/*"
]
}
In the file jwt.service.ts, I have the following code snippet:
import { User } from "@Models/user.entity";
When I execute npm run start:dev
, it compiles into JavaScript and the import statement for User entity resolves to:
const user_entity_1 = require("../../../../../../../D:\\Project\\MyJob\\Backend\\src\\database\\entities\\user.entity");
However, an error occurs with the message:
internal/modules/cjs/loader.js:638 throw err; ^ Error: Cannot find module '../../../../../../../D:\Project\MyJob\Backend\src\database\entities\user.entity'ntity' at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15) at Function.Module._load (internal/modules/cjs/loader.js:562:25) at Module.require (internal/modules/cjs/loader.js:692:17) at require (internal/modules/cjs/helpers.js:25:18) at Object. (D:\Project\MyJob\Backend\dist\shared\auth\jwt.service.js:25:23) at Module._compile (internal/modules/cjs/loader.js:778:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10) at Module.load (internal/modules/cjs/loader.js:653:32) at tryModuleLoad (internal/modules/cjs/loader.js:593:12) at Function.Module._load (internal/modules/cjs/loader.js:585:3)
The issue is resolved only when I use this modified path in the code:
const user_entity_1 = require("../..\\database\\entities\\user.entity");