简体   繁体   中英

NestJS and docker-compose error on build, main file not found

So I've been working with a NestJS app using docker compose and Typescript. I didn't had any issues until recently, now every time I do docker-compose build myApp and docker-compose up myApp the container fails and shows this error message:

错误信息

I've tried a few things to solve the issue, for example I delete the container and the image and rebuild it again with docker-compose build --no-cache myApp but this didn't work. I don't know why sometimes it does work, but most of the time it doesn't. Seems to me like a random outcome, I don't know if I should wait longer after deleting the container and image for it to work. I believe the problem is that the Nest build is not being done correctly. I need to find a way to solve this because it's really taking a lot of my time trying to find the solution.

Here is my dockerfile and my package.json.

Dockerfile

FROM node:14.17.0-alpine

WORKDIR /tmp

COPY . .

RUN npm install

RUN npm run build

EXPOSE 3005

ENV NODE_TLS_REJECT_UNAUTHORIZED=0

# Run it
ENTRYPOINT ["node", "/tmp/dist/main"]

package.json

{
  "name": "carbon-api",
  "private": true,
  "version": "0.2.1",
  "description": "",
  "license": "UNLICENSED",
  "scripts": {
    "prebuild": "rimraf dist",
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/main",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage --no-cache",
    "test:export": "jest --coverage && jest-coverage-to-csv ./coverage/api-sumary.json ./coverage/api-sumary.csv",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json"
  },
  "dependencies": {
    "@casl/ability": "^5.2.2",
    "@grpc/proto-loader": "^0.5.6",
    "@nestjs/common": "^7.5.1",
    "@nestjs/core": "^7.5.1",
    "@nestjs/jwt": "^7.2.0",
    "@nestjs/microservices": "^7.2.0",
    "@nestjs/passport": "^7.1.5",
    "@nestjs/platform-express": "^7.5.1",
    "@nestjs/swagger": "^4.5.10",
    "@nestjs/typeorm": "^7.1.0",
    "@types/bcrypt": "^3.0.0",
    "aws-sdk": "^2.955.0",
    "bcryptjs": "^2.4.3",
    "body-parser": "^1.19.0",
    "body-parser-xml": "^2.0.0",
    "class-transformer": "^0.4.0",
    "class-validator": "^0.13.1",
    "dotenv": "^8.2.0",
    "grpc": "^1.24.4",
    "mysql": "^2.18.1",
    "nest-access-control": "^2.0.2",
    "nest-winston": "^1.3.5",
    "passport": "^0.4.1",
    "passport-jwt": "^4.0.0",
    "passport-local": "^1.0.0",
    "redis": "^3.0.2",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^6.6.3",
    "swagger-ui-express": "^4.1.4",
    "typeorm": "^0.2.31",
    "winston": "^3.3.3"
  },
  "devDependencies": {
    "@nestjs/cli": "^7.5.1",
    "@nestjs/schematics": "^7.1.3",
    "@nestjs/testing": "^7.5.1",
    "@types/express": "^4.17.8",
    "@types/jest": "^26.0.15",
    "@types/multer": "^1.4.7",
    "@types/node": "^14.14.6",
    "@types/passport-jwt": "^3.0.4",
    "@types/passport-local": "^1.0.33",
    "@types/supertest": "^2.0.10",
    "@typescript-eslint/eslint-plugin": "^4.6.1",
    "@typescript-eslint/parser": "^4.6.1",
    "eslint": "^7.12.1",
    "eslint-config-prettier": "^6.15.0",
    "eslint-plugin-prettier": "^3.1.4",
    "jest": "^26.6.3",
    "prettier": "^2.1.2",
    "supertest": "^6.0.0",
    "ts-jest": "^26.4.3",
    "ts-loader": "^8.0.8",
    "ts-node": "^9.0.0",
    "tsconfig-paths": "^3.9.0",
    "typescript": "^4.0.5",
    "jest-coverage-to-csv": "^1.1.0"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "coveragePathIgnorePatterns": [
      ".module.ts",
      ".config.ts",
      "main.ts",
      ".entity.ts",
      ".dto.ts",
      ".enum.ts",
      "constants.ts",
      "paginationParams.ts"
    ],
    "rootDir": "src",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "coverageReporters": [
      [
        "json-summary",
        {
          "file": "api-sumary.json"
        }
      ],
      "html"
    ],
    "testEnvironment": "node"
  }
}

So for anyone out there, maybe it's useful.

The problem was that I decided to run npm install to be able to run unit tests in my local machine. This made some changes on my package-lock.json file which was causing this problem during the build on docker (I don't know why changes were made to it if i didn't add new dependencies).

I noticed it because git was showing this file as modified when it should've remained the same as the last time.

Either way, my solution was to go to a previous working version of my project, copy the old package-lock.json and voilá, that solved the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM