NestJS: Copying Assets not working on Linux

Viewed 1321

I'm working on a NestJS project and copying my assets works fine on my Mac. However, once I dockerize it it won't work.

nest-cli.json

{
  "collection": "@nestjs/schematics",
  "sourceRoot": "src",
  "compilerOptions": {
    "assets": [
      "**/*.hbs",
      "**/*.css",
      "**/*.jpg",
      "**/*.png",
      "**/*.jpeg"
    ],
    "watchAssets": true
  }
}

Dockerfile:

FROM mhart/alpine-node:latest

RUN npm install pm2 -g

COPY backend /var/www/backend
COPY process.json /var/www
WORKDIR /var/www

#RUN npm i -g @nestjs/cli (tried with and w/out -> no difference)
RUN cd ./backend && npm i --legacy-peer-deps && npm run build

# Expose ports
EXPOSE 80

CMD ["pm2-runtime", "./backend/main"]

tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true
  }
}

tsconfig.build.json

npm{
  "extends": "./tsconfig.json",
  "exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
}

I'm not getting any build errors and I've reproduced these steps manually on my mac and everything's being copied. When I go into the docker image and also run the buld step manually it works with no errors but again, no assets are being copied.

I have my assets in different module & services folders and will want to keep it this way - so I'm not looking for a simply post build copy dir solution :)

I've tried different linux dists. and Node version - I'm not sure what else to check

1 Answers

I had a similar problem trying to dockerize my project on a Linux host (yarn build on local worked just fine, but assets were not copied in the docker image).

Original Setup

Dockerfile:

FROM node:16-bullseye AS builder
WORKDIR /usr/app
COPY package.json ./
COPY yarn.lock ./
COPY src ./
COPY nest-cli.json ./
COPY tsconfig.build.json ./
COPY tsconfig.json ./
RUN yarn install
RUN yarn build
...

nest-cli.json:

{
  ...,
  "compilerOptions": {
    "assets: [ "modules/mails/templates/**/*.hbs" ],
    ...
  }
}

Fix

The paths specified within 'compilerOptions.assets' are always relative to the 'src' folder within the project. Inspecting my docker image I realized that the content of the 'src' folder was being copied in the root of the build folder inside docker.

When the content of 'src' is moved to the root, NestJS is able to build the project successfully, but since assets are looked for within the 'src' folder, no files are copied. Changing this in my Dockerfile fixed the problem:

COPY src ./src # Instead of 'COPY src ./'
Related