Nestjs/prisma/docker/postgres – app worked yesterday but now keeps saying “cannot find module ‘/app/dist/project-name/src/main’

Nestjs/prisma/docker/postgres – app worked yesterday but now keeps saying “cannot find module ‘/app/dist/project-name/src/main’

If you are working with NestJS, Prisma, Docker, and Postgres, you may encounter an issue where your app was working fine yesterday but now throws an error stating “cannot find module ‘/app/dist/project-name/src/main’”. This error typically occurs when there is a problem with the build or deployment process. In this blog post, we will explore a few possible solutions to resolve this issue.

Solution 1: Rebuild the Docker container

One possible solution is to rebuild the Docker container to ensure that all the necessary dependencies are included. To do this, follow these steps:

  1. Stop the running Docker container by running the command: docker-compose down
  2. Delete the existing Docker container by running the command: docker-compose rm
  3. Rebuild the Docker container by running the command: docker-compose up --build

This will rebuild the Docker container and ensure that all the required modules are present.

Solution 2: Check the build process

Another possible solution is to check the build process and ensure that the necessary files are being included. Sometimes, the build process may exclude certain files, leading to the “cannot find module” error. To resolve this, follow these steps:

  1. Open your tsconfig.json file and ensure that the include property includes all the necessary directories and files. For example:
{
  "compilerOptions": {
    "outDir": "./dist",
    "rootDir": "./src",
    "module": "commonjs",
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "sourceMap": true,
    "strict": true,
    "noImplicitAny": true,
    "moduleResolution": "node",
    "baseUrl": "./",
    "paths": {
      "*": ["node_modules/*"],
      "@/*": ["src/*"]
    },
    "target": "es2017"
  },
  "include": ["src/**/*.ts", "src/**/*.tsx", "prisma/**/*.ts"]
}
  1. Ensure that your build process is correctly copying the necessary files to the dist directory. For example, in your package.json file, you should have a build script that looks like this:
"scripts": {
  "build": "rimraf dist && tsc",
  ...
}

Make sure that the rimraf dist command is deleting the existing dist directory before running the tsc command to compile the TypeScript files.

Solution 3: Check the file paths

Lastly, double-check the file paths in your code to ensure that they are correct. The error message indicates that the module cannot be found at the specified path. Verify that the file /app/dist/project-name/src/main actually exists and that the path is correct.

Additionally, check the import statements in your code and make sure they are referencing the correct file paths. For example, if you have an import statement like:

import { SomeModule } from '/app/dist/project-name/src/some-module';

Make sure that the file some-module.ts exists at the specified path.

By following these solutions, you should be able to resolve the “cannot find module” error in your NestJS, Prisma, Docker, and Postgres application. Remember to rebuild the Docker container, check the build process, and verify the file paths in your code.


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *