Home > database >  sqlite3 with docker throws MODULE_NOT_FOUND error
sqlite3 with docker throws MODULE_NOT_FOUND error

Time:02-04

My node application works on my local(MacOS), but it does not work if I use docker.

  1. it works if I try local: npm install npm start
  2. It throws error if I try docker docker-compose build docker-compose up

I'm getting this error. Error: Cannot find module '/src/node_modules/sqlite3/lib/binding/napi-v6-linux-musl-x64/node_sqlite3.node'

Package.json

{
  "name": "api",
  "version": "1.0.0",
  "description": "API",
  "main": "index.js",
  "scripts": {
    "test": "rm -rf usersdb.sqlite && jest --forceExit",
    "test:coverage": "npm run test -- --coverage --forceExit",
    "start": "nodemon app.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bcryptjs": "^2.4.3",
    "body-parser": "^1.20.1",
    "cors": "^2.8.5",
    "dotenv": "^10.0.0",
    "express": "^4.18.2",
    "jsonwebtoken": "^9.0.0",
    "lodash": "^4.17.21",
    "mysql": "^2.18.1",
    "nodemon": "^2.0.20",
    "sqlite3": "^5.1.4"
  },
  "devDependencies": {
    "jest": "^28.1.1",
    "supertest": "^6.3.3"
  }
}

docker-compose.yml

version: "3.7"
services:
  api:
    image: test/api
    build: ./
    command: npm start
    restart: on-failure
    environment:
      SERVER_PORT: 3004
      TOKEN_KEY: test123
    volumes:
      - .:/src
    ports:
      - "3004:3004"

Dockerfile

FROM node:12.22-alpine as base

WORKDIR /src
COPY package*.json ./
EXPOSE 3004

RUN apk add --no-cache python2 g   make

RUN npm install
FROM base as dev
ENV NODE_ENV=development
RUN npm install -g nodemon
COPY . ./
CMD ["nodemon", "app.js"]

CodePudding user response:

You will need to delete the volumes: block from your docker-compose.yml.

The volumes: block will overwrite your entire application with content from your host system. That includes overwriting the Linux-OS node_modules tree with the MacOS version from your host system.

You don't need this volumes: block. The code and node_modules: tree are built into your image. If you need to develop your application, you can install Node on your host system (on MacOS this might be as little as brew install node) and use that for day-to-day development, even if you're planning to eventually use Docker for final deployment or if you have dependencies that run in containers.

CodePudding user response:

Delete the volumes: block from your docker-compose.yml.

The volumes: block in your docker-compose.yml file is overwriting your entire application with content from your host system. That includes overwriting the Linux-OS node_modules tree with the MacOS version from your host system.

You don't need this volumes: block. The code and node_modules: tree are built into your image. If you need to develop your application, you can install Node on your host system (on MacOS this might be as little as brew install node) and use that for day-to-day development, even if you're planning to eventually use Docker for final deployment or if you have dependencies that run in containers.

  • Related