I am having a problem with my nodeJS back-end. I want to create a docker compose environment but I keep getting this error when I try to start my Prisma NodeJS app. I already tried lots of things and I can’t find any usable solutions on Google. The application works without docker.
Docker compose file:
endbit-express:
container_name: endbit-express
build: ./endbit-express
volumes:
- ./endbit-express:/app
- /app/node_modules
ports:
- 8080:8080
depends_on:
- mysql
environment:
- DATABASE_URL=mysql://root:root@localhost:3306/endbit
networks:
- endbit
mysql:
container_name: endbit-mysql
image: mysql:8.0.28
restart: always
ports:
- 6033:3306
environment:
- MYSQL_DATABASE=endbit
- MYSQL_ROOT_PASSWORD=root
volumes:
- dbdata:/var/lib/mysql
networks:
- endbit
Docker file:
FROM node:17.4.0
WORKDIR /app
COPY package*.json ./
COPY prisma ./prisma
RUN npm install
COPY . .
RUN prisma generate
EXPOSE 8080
CMD ["npm", "start"]
Error:
endbit-express | > [email protected] start
endbit-express | > node ./bin/www
endbit-express |
endbit-express | /app/node_modules/.prisma/client/index.js:3
endbit-express | throw new Error(
endbit-express | ^
endbit-express |
endbit-express | Error: @prisma/client did not initialize yet. Please run "prisma generate" and try to import it again.
endbit-express | In case this error is unexpected for you, please report it in https://github.com/prisma/prisma/issues
endbit-express | at new PrismaClient (/app/node_modules/.prisma/client/index.js:3:11)
endbit-express | at Object.<anonymous> (/app/config/passport.js:7:16)
endbit-express | at Module._compile (node:internal/modules/cjs/loader:1097:14)
endbit-express | at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
endbit-express | at Module.load (node:internal/modules/cjs/loader:975:32)
endbit-express | at Function.Module._load (node:internal/modules/cjs/loader:822:12)
endbit-express | at Module.require (node:internal/modules/cjs/loader:999:19)
endbit-express | at require (node:internal/modules/cjs/helpers:102:18)
endbit-express | at Object.<anonymous> (/app/app.js:7:1)
endbit-express | at Module._compile (node:internal/modules/cjs/loader:1097:14)
endbit-express |
endbit-express | Node.js v17.4.0
2
Answers
Eventually I found the answer myself: I am using a docker compose file, and Docker doesn't recognize the localhost:3306 URL. In docker compose you need to link the containers by their name, so I changed the backend component environment with this (host = docker service name):
add
RUN npm i -g prisma
beforeRUN prisma generate
it should work