在生产模式下运行nestjs时出错,找不到模块 [英] Error while running nestjs in production mode, cannot find module

查看:1922
本文介绍了在生产模式下运行nestjs时出错,找不到模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了以下通用类,这可能会导致问题

I have implemented a generic class as below which might be causing the problem,

    import { Logger } from '@nestjs/common';
    import { PaginationOptionsInterface, Pagination } from './paginate';
    import { Repository } from 'typeorm';

    export class EntityService<T> {
      private repository: Repository<T>;
      constructor(repository) {
        this.repository = repository;
      }

      async getEntityWithPagination(
        options: PaginationOptionsInterface,
      ): Promise<Pagination<T>> {
        const [results, total] = await this.repository.findAndCount({
          take: options.limit,
          skip: (options.page - 1) * options.limit,
        });
        return new Pagination<T>({ results, total });
      }
    }

并与其他实体服务一起使用,例如

and using with other entity services, such as

    @Injectable()
    export class CarService extends EntityService<CarEntity> {
      constructor(
        @InjectRepository(CarEntity)
        private carRepository: Repository<CarEntity>,
      ) {
        super(carRepository);
      }

代码在npm run start:dev上运行正常,但是在尝试与生产版npm run start:prod

the code is working perfectly fine with npm run start:dev but throwing below error when trying to run with production npm run start:prod

        internal/modules/cjs/loader.js:582
            throw err;
            ^

        Error: Cannot find module 'src/shared/entity.service'
            at Function.Module._resolveFilename (internal/modules/cjs/loader.js:580:15)
            at Function.Module._load (internal/modules/cjs/loader.js:506:25)
            at Module.require (internal/modules/cjs/loader.js:636:17)
            at require (internal/modules/cjs/helpers.js:20:18)
            at Object.<anonymous> (/home/tejas/Code/web/project/dist/car/car.service.js:27:26)
            at Module._compile (internal/modules/cjs/loader.js:688:30)
            at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
            at Module.load (internal/modules/cjs/loader.js:598:32)
            at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
            at Function.Module._load (internal/modules/cjs/loader.js:529:3)
        npm ERR! code ELIFECYCLE
        npm ERR! errno 1
        npm ERR! project@0.0.0 start:prod: `node dist/main.js`
        npm ERR! Exit status 1

我尝试删除dist文件夹,但还是没有运气.我也尝试过更新软件包,package.json如下.我不知道如何调试它.

I 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.

        dependencies": {
            "@nestjs/common": "^5.5.0",
            "@nestjs/core": "^5.5.0",
            "@nestjs/jwt": "^0.2.1",
            "@nestjs/passport": "^5.1.0",
            "@nestjs/typeorm": "^5.2.2",
            "bcryptjs": "^2.4.3",
            "glob": "^7.1.3",
            "passport": "^0.4.0",
            "passport-http-bearer": "^1.0.1",
            "passport-jwt": "^4.0.0",
            "pg": "^7.7.1",
            "reflect-metadata": "^0.1.12",
            "rimraf": "^2.6.2",
            "rxjs": "^6.2.2",
            "typeorm": "^0.2.9",
            "typescript": "^3.2.2"
        },
        "devDependencies": {
            "@nestjs/testing": "^5.5.0",
            "@types/express": "^4.16.0",
            "@types/jest": "^23.3.1",
            "@types/node": "^10.12.18",
            "@types/supertest": "^2.0.7",
            "jest": "^23.5.0",
            "nodemon": "^1.18.9",
            "prettier": "^1.14.2",
            "supertest": "^3.1.0",
            "ts-jest": "^23.1.3",
            "ts-loader": "^4.4.2",
            "ts-node": "^7.0.1",
            "tsconfig-paths": "^3.5.0",
            "tslint": "5.11.0",
            "webpack": "^4.28.2",
            "webpack-cli": "^3.1.2",
            "webpack-node-externals": "^1.7.2"
        },

推荐答案

我发现了问题,这是由于导入类时的绝对路径造成的.

I have found the issue, it was because of absolute path while importing the class.

import { EntityService } from '../shared/service-common'; //correct way

import { EntityService } from 'src/shared/service-common'; // wrong autoimport

要解决自动导入问题,我已在VS Code中添加了此设置

To fix auto import, I have added this setting in VS Code

"typescript.preferences.importModuleSpecifier": "relative"

这篇关于在生产模式下运行nestjs时出错,找不到模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆