xyboox
xyboox

Reputation: 13

docker-compose node js module not found after installing new module

I have this docker images that were perfectly running up until I had to add a new npm package to the node.js app and right after, the newly added module is "not found". I thought maybe the package is not good, so I tried with another to the same result ( not found ). These are my files:

docker-compose.yml

version: '3.8'
services:
  builditjobs:
    container_name: 'BIJ-API'
    build: .
    ports:
      - '12100:12100'
    depends_on:
      - db
    volumes:
      - './api:/var/apps/BIJ' #  Mount the local directory to the image so we don't have to restart our image to get new code
      - node_modules:/var/apps/BIJ/node_modules
  db:
    container_name: 'DB'
    image: mysql:5.7
    ports:
      - '3306:3306'
    volumes:
      - db-data:/var/lib/mysql
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=*******
      - MYSQL_DATABASE=bij
      - MYSQL_USER=bij
      - MYSQL_PASSWORD=******
  admin:
    container_name: 'ADMINER'
    image: adminer
    depends_on:
      - db
    ports:
      - '8080:8080'
volumes:
  db-data:
  node_modules:

Dockerfile

FROM node:10
RUN mkdir -p /var/apps/BIJ
WORKDIR /var/apps/BIJ
RUN apt-get update && apt-get install wait-for-it
RUN npm i npm@latest -g
RUN node -v
RUN npm -v
RUN npm install pm2 -g
RUN pm2 ls
COPY package.json ./
RUN npm install --no-optional && npm cache clean -f
RUN chown -R node:node /var/apps/BIJ
RUN ls -la
EXPOSE 12100
ENTRYPOINT ./docker-entrypoint.sh

docker-entrypoint:

#!/bin/bash
wait-for-it db:3306 -- pm2 start ecosystem.config.js && pm2 logs API-1.0.0 --raw

package.json

{
  "name": "bij-api",
  "version": "1.0.0",
  "description": "BIJ API",
  "main": "server.ts",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
  },
  "author": "CD",
  "license": "ISC",
  "dependencies": {
    "@setreflex/logger": "^1.0.10",
    "@types/node": "^14.0.11",
    "body-parser": "^1.19.0",
    "colors": "^1.4.0",
    "compression": "^1.7.4",
    "cors": "^2.8.5",
    "crypto-js": "^4.0.0",
    "express": "^4.17.1",
    "jsonwebtoken": "^8.5.1",
    "mariadb": "^2.4.0",
    "moment": "^2.25.3",
    "morgan": "^1.10.0",
    "nodemailer": "^6.4.6",
    "q": "^1.5.1",
    "schema-inspector": "^1.6.9"
  }
}

Any idea what's wrong? I emphasise : it was working perfectly fine until I did a npm i crypto-js -s and rebuilt image. Thanks!

LE: this is the exact error I get:

internal/modules/cjs/loader.js:638
BIJ-API        |     throw err;
BIJ-API        |     ^
BIJ-API        | 
BIJ-API        | Error: Cannot find module 'crypto-js'
BIJ-API        |     at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
BIJ-API        |     at Function.Module._load (internal/modules/cjs/loader.js:562:25)
BIJ-API        |     at Module.require (internal/modules/cjs/loader.js:692:17)
BIJ-API        |     at require (internal/modules/cjs/helpers.js:25:18)

Upvotes: 0

Views: 1143

Answers (1)

Venkatesh A
Venkatesh A

Reputation: 2474

My advice would be to install the dependencies(node_modules) each and every time u build it.

The Dockerfile is as follows...

FROM node:12.3.1 WORKDIR /www/api RUN npm install pm2 -g RUN npm install babel-cli -g ADD package.json /www/api RUN npm install ADD . /www/api COPY docker-entrypoint.sh /usr/local/bin/ RUN chmod +x /usr/local/bin/docker-entrypoint.sh EXPOSE 3000 ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]

while the compose file is

api:
    build: .
    entrypoint:
        - /usr/local/bin/docker-entrypoint.sh
    restart: always
    ports:
        - "3000:3000"`

and then u could add a .dockerignore to ignore the node modules

Upvotes: 1

Related Questions