Path Error running AdonisJS in production via Docker

1k Views Asked by At

I'm getting a path error from require-ts and I have no idea how to determine what I'm missing. The app works fine locally using node ace serve but not within docker on a production server.

This is the error:

docker exec -it adonis_app node ace list:routes

  TypeError

 The "path" argument must be of type string. Received undefined


   1  Cache.makeCachePath
      /home/node/app/node_modules/@adonisjs/require-ts/build/src/Cache/index.js:41

   2  Config.getCached
      /home/node/app/node_modules/@adonisjs/require-ts/build/src/Config/index.js:139

   3  Config.parse
      /home/node/app/node_modules/@adonisjs/require-ts/build/src/Config/index.js:156

   4  register
      /home/node/app/node_modules/@adonisjs/require-ts/build/index.js:82

   5  Object.registerForAdonis [as default]
      /home/node/app/node_modules/@adonisjs/assembler/build/src/requireHook/index.js:17

   6  registerTsHook
      /home/node/app/node_modules/@adonisjs/core/build/src/utils/index.js:26

   7  App.onFind
      /home/node/app/node_modules/@adonisjs/core/build/src/Ignitor/Ace/App/index.js:132

This is my dockerfile:

ARG NODE_IMAGE=node:16.13.1-alpine

FROM $NODE_IMAGE AS base
RUN apk --no-cache add dumb-init
RUN mkdir -p /home/node/app && chown node:node /home/node/app
WORKDIR /home/node/app
USER node
RUN mkdir tmp

FROM base AS dependencies
COPY --chown=node:node ./package*.json ./
COPY --chown=node:node ./tsconfig*.json ./
RUN npm ci
COPY --chown=node:node . .

FROM dependencies AS build
RUN node ace build --production --ignore-ts-errors

FROM base AS production
ENV NODE_ENV=production
ENV PORT=$PORT
ENV HOST=0.0.0.0
COPY --chown=node:node ./package*.json ./
RUN npm ci --production
COPY --chown=node:node --from=build /home/node/app/build .
EXPOSE $PORT
CMD [ "dumb-init", "node", "build/server.js" ]

And docker-compose:

version: '3'

services:



  adonis_app:
    container_name: adonis_app
    restart: always
    build:
      context: .
      #target: dependencies
    depends_on:
      - postgres
    ports:
      - ${PORT}:${PORT}
      - 9229:9229
    env_file:
      - .env
    volumes:
      - ./:/home/node/app
      - uploads:/home/node/app/public/uploads
    networks:
      - adonis
    #command: dumb-init node ace serve --watch --node-args="--inspect=0.0.0.0"
    #command: dumb-init node build/server.js --node-args="--inspect=0.0.0.0"


  postgres:
    image: postgres:13.1
    healthcheck:
      test: [ "CMD", "pg_isready", "-q", "-d", "postgres", "-U", "root" ]
      timeout: 45s
      interval: 10s
      retries: 10
    restart: always
    environment:
      - POSTGRES_USER=${ROOT_DB_USER}
      - POSTGRES_PASSWORD=${ROOT_DB_PASS}
      - APP_DB_USER=${PG_USER}
      - APP_DB_PASS=${PG_PASSWORD}
      - APP_DB_NAME=${PG_DB_NAME}
    volumes:
      #- ./db:/docker-entrypoint-initdb.d/
      - ./db:/var/lib/postgresql/data
    ports:
      - 54325:5432
    networks:
      - adonis

networks:
  adonis:
    driver: bridge
    
volumes:
    db:
      driver: local
    uploads:
      driver: local

I receive the same error when running any ace command, such as migration and seeding. What am I missing?

1

There are 1 best solutions below

0
On

I finally have a solution after watching the adonis compiled code for two hours.

I found out that adonis uses a library called find-cache-dir. And it seems that it doesn't find any cache directory, so it returns undefined (the famous one).

The solution I opted for is to just specify the cache directory in the environment variables.
For example:

ENV CACHE_DIR /home/node/app/.cache/

Consult the library documentation if you want to specify the cache directory elsewhere.

EDIT: If the library returns undefined it could also be because the node_modules directory is not writable, in this case make it writable or move the cache directory.

Thanks for reading (this is my first answer).