1

I'm trying to create a docker with node, mysql and react on the front side. But when I try to import the mysql lib, I'm getting the following error:

const express = require("express");
const router  = express.Router();
const mysql   = require('mysql');

Error: Cannot find module 'mysql'

I already check my node_modules folder and there is a mysql directory there.
This is my package.json:

{
  "name": "api",
  "version": "1.0.0",
  "private": true,
  "scripts": {
    "start": "node bin/www",
    "dev": "./node_modules/.bin/nodemon bin/www"
  },
  "dependencies": {
    "cookie-parser": "~1.4.3",
    "cors": "^2.8.4",
    "debug": "~2.6.9",
    "express": "~4.16.0",
    "http-errors": "~1.6.2",
    "jade": "~1.11.0",
    "morgan": "~1.9.0",
    "mysql": "^2.18.1",
    "node-mysql": "^0.4.2"
  },
  "devDependencies": {
    "nodemon": "^1.14.12"
  }
}

This is my Dockerfile, I tried to install the mysql globally inside the container, but didn't solve:

FROM mhart/alpine-node:12

WORKDIR /api

COPY package*.json /api/

RUN npm i -G nodemon
RUN npm install -G mysql
RUN npm install

COPY . /api/

EXPOSE 80

CMD ["npm", "run", "dev"]

docker-compose.yml

version: "3"
services:
    client:
        container_name: MY_api
        image: mhart/alpine-node:12
        build: ./client      
        restart: always
        ports:
            - "3000:3000"
        working_dir: /client
        volumes:
            - ./client:/client
        entrypoint: ["npm", "start"]
        links:
            - api
        networks: 
            - MY_network
    api:
        container_name: MY_client
        build: ./api
        restart: always
        ports:
            - "9000:9000"
        working_dir: /api
        volumes:
            - ./api:/api
            - /api/node_modules
        depends_on: 
            - mysqldb
        networks:
            - MY_network
    mysqldb:
        container_name: MY_mysql
        image: mysql:8.0.19
        restart: always
        ports:
            - "3307:3306"
        environment:
            - MYSQL_ROOT_PASSWORD=MY_PASS 
            - MYSQL_USER=USER
            - MYSQL_PASSWORD=MY_PASS
            - MYSQL_DATABASE=MY_DATABASE
        networks: 
            - MY_network
networks:
    MY_network:
        driver: bridge



1
  • 1
    The volumes: declaration tells Docker that your node_modules directory contains vital user data that it must not modify; if you've recently added the package to your package.json file, Docker will not see the update because the old volume takes precedence. If you delete that section of your docker-compose.yml file, it will run the code and library tree that's baked into your image. Commented Jan 25, 2020 at 13:44

1 Answer 1

2

Remove your mounted volume - /api/node_modules from your docker-compose.

And remove those lines RUN npm i -G nodemon RUN npm install -G mysql inside your Dockerfile.

Everything should work fine

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.