67

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);
      }

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! [email protected] start:prod: `node dist/main.js`
        npm ERR! Exit status 1

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"
        },

13 Answers 13

120

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

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

"typescript.preferences.importModuleSpecifier": "relative"
Sign up to request clarification or add additional context in comments.

4 Comments

hello, I want to know why I add ` "paths": { "src/*": ["src/*"] } ` in tsconfig.json also throw error when use absolute import.
Would you please explain why is that so? Thank you very much!
is there any way that we can use this "src/.." path when importing?
It's not a good solution, because I want to use absolute path.
69

Delete the dist directory and run again with: npm run start:dev

6 Comments

Lots of thanks. Nest is Angular-like, but has some weird problems Angular doesn't have.
This doesn't answer the question, as it runs the app in development mode, not in production mode.
this fixed it for me, dunno why though
that fixed it for me. but why?
This fix it for me. Many Thanks, would've spent eternity fixing what is not broken many thanks
|
7

Just ensure that your script is pointing to main.js inside dist folder.

"start:prod": "node dist/src/main"

Comments

5

I've also seen the same problem due to a capitalized reference, when the filename is lowercase:

import { SomeClass } from './Some.class'; 

but the file was named some.class.ts

Fixing the import resolved the error.

2 Comments

My eyes have glossed over from staring at this too long and I never would have caught this without your suggestion—Thank you!
Such a silly thing, but big help! not sure why it even allows this when i use wrong case on local
5

I think it's worth mentioning that having js/ts files(that import modules) outside the src folder might cause issues when running start:prod

I had a seed.ts for seeding to my database with Prisma ORM, I moved it inside the src folder and resolved the issue.

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
This was my exact problem, and this is valid because files outside of the src folder aren't accounted for by your tsconfig, so moving the seed file from the prisma folder generated by prisma init into my src folder solved the error.
4

Sometimes the error can be caused by existing code outside the SRC folder (eg Typeform migrations, etc) which causes the compilation inside DIST to enter one level of folders (eg dist/migrations, dist/src) which makes the main file not be in the correct location.

Comments

3

A solution

Remove the noEmit from compilerOptions of tsconfig.json file.

1 Comment

Why would this help?
2

I got this resolved by deleting the generated files and recreating it eg: dist folder

1 Comment

2

If your problem doesn't include relative directory problem then try to remove the dist folder then try to npm run start:prod again, if you have a tsconfig.build.tsbuildinfo file delete it as well.

Comments

0

I forgot to install these two packages that were required by mikro-orm. Installing solved my issue:

yarn add @mikro-orm/core @mikro-orm/nestjs

Comments

0

In my case the solution was "stupid": you need to give a name for the folder without space. If you write: codes projects, it cannot find, you have to write codes_projects. I am adding this solution since it is too trivial, and likely the last place people will look.

Comments

0

May not be relevant for many people but in my case it was all caused by an instanceOf...

we had a method who received a two type parameter

getElement(svc: TypeA | TypeB) {
  svc instanceof TypeA
}

don't really know why... but only worked after removing this

Comments

-6

it caused by capitalized file Name.

Key-store.entity.ts -> key-store.entity.ts

make filename all to lowercase.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.