{"id":502,"hash":"cfad680ae99757776d5e709238fef8ad90a36581caf28983924279793a3659c4","pattern":"Error while running nestjs in production mode, cannot find module","full_message":"I have implemented a generic class as below which might be causing the problem, \n\n    import { Logger } from '@nestjs/common';\n    import { PaginationOptionsInterface, Pagination } from './paginate';\n    import { Repository } from 'typeorm';\n\n    export class EntityService<T> {\n      private repository: Repository<T>;\n      constructor(repository) {\n        this.repository = repository;\n      }\n\n      async getEntityWithPagination(\n        options: PaginationOptionsInterface,\n      ): Promise<Pagination<T>> {\n        const [results, total] = await this.repository.findAndCount({\n          take: options.limit,\n          skip: (options.page - 1) * options.limit,\n        });\n        return new Pagination<T>({ results, total });\n      }\n    }\n\nand using with other entity services, such as \n\n    @Injectable()\n    export class CarService extends EntityService<CarEntity> {\n      constructor(\n        @InjectRepository(CarEntity)\n        private carRepository: Repository<CarEntity>,\n      ) {\n        super(carRepository);\n      }\n\nthe code is working perfectly fine with npm run start:dev but throwing below error when trying to run with production npm run start:prod \n\n        internal/modules/cjs/loader.js:582\n            throw err;\n            ^\n\n        Error: Cannot find module 'src/shared/entity.service'\n            at Function.Module._resolveFilename (internal/modules/cjs/loader.js:580:15)\n            at Function.Module._load (internal/modules/cjs/loader.js:506:25)\n            at Module.require (internal/modules/cjs/loader.js:636:17)\n            at require (internal/modules/cjs/helpers.js:20:18)\n            at Object.<anonymous> (/home/tejas/Code/web/project/dist/car/car.service.js:27:26)\n            at Module._compile (internal/modules/cjs/loader.js:688:30)\n            at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)\n            at Module.load (internal/modules/cjs/loader.js:598:32)\n            at tryModuleLoad (internal/modules/cjs/loader.js:537:12)\n            at Function.Module._load (internal/modules/cjs/loader.js:529:3)\n        npm ERR! code ELIFECYCLE\n        npm ERR! errno 1\n        npm ERR! project@0.0.0 start:prod: `node dist/main.js`\n        npm ERR! Exit status 1\n\nI have tried deleting dist folder, but still no luck. I have tried updating packages also, package.json is as follows. I have no clue how to debug this.\n\n        dependencies\": {\n            \"@nestjs/common\": \"^5.5.0\",\n            \"@nestjs/core\": \"^5.5.0\",\n            \"@nestjs/jwt\": \"^0.2.1\",\n            \"@nestjs/passport\": \"^5.1.0\",\n            \"@nestjs/typeorm\": \"^5.2.2\",\n            \"bcryptjs\": \"^2.4.3\",\n            \"glob\": \"^7.1.3\",\n            \"passport\": \"^0.4.0\",\n            \"passport-http-bearer\": \"^1.0.1\",\n            \"passport-jwt\": \"^4.0.0\",\n            \"pg\": \"^7.7.1\",\n            \"reflect-metadata\": \"^0.1.12\",\n            \"rimraf\": \"^2.6.2\",\n            \"rxjs\": \"^6.2.2\",\n            \"typeorm\": \"^0.2.9\",\n            \"typescript\": \"^3.2.2\"\n        },\n        \"devDependencies\": {\n            \"@nestjs/testing\": \"^5.5.0\",\n            \"@types/express\": \"^4.16.0\",\n            \"@types/jest\": \"^23.3.1\",\n            \"@types/node\": \"^10.12.18\",\n            \"@types/supertest\": \"^2.0.7\",\n            \"jest\": \"^23.5.0\",\n            \"nodemon\": \"^1.18.9\",\n            \"prettier\": \"^1.14.2\",\n            \"supertest\": \"^3.1.0\",\n            \"ts-jest\": \"^23.1.3\",\n            \"ts-loader\": \"^4.4.2\",\n            \"ts-node\": \"^7.0.1\",\n            \"tsconfig-paths\": \"^3.5.0\",\n            \"tslint\": \"5.11.0\",\n            \"webpack\": \"^4.28.2\",\n            \"webpack-cli\": \"^3.1.2\",\n            \"webpack-node-externals\": \"^1.7.2\"\n        },","ecosystem":"npm","package_name":"typescript","package_version":null,"solution":"I have found the issue, it was because of absolute path while importing the class.\n\nimport { EntityService } from '../shared/service-common'; //correct way\n\nimport { EntityService } from 'src/shared/service-common'; // wrong autoimport\n\nTo fix auto import, I have added this setting in VS Code \n\n\"typescript.preferences.importModuleSpecifier\": \"relative\"","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/53963007/error-while-running-nestjs-in-production-mode-cannot-find-module","votes":67,"created_at":"2026-04-19T04:51:16.270554+00:00","updated_at":"2026-04-19T04:51:16.270554+00:00"}