Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access node modules in typescript while using docker & docker compose

I'm trying to create a docker image that is hosting a NodeJS project during develop. It simply executes the NodeJS application with nodemon so it restarts every time when I make changes.

I also use TypeScript to develop the application. TypeScript must have access to a installed @types module. However the mounted volume that should show the node_modules folder is empty. So TypeScript cannot find the modules and cannot compile. My IDE is also complaining (obviously)

Is there a way to make the node_modules visible for TypeScript?

This is my Dockerfile:

FROM node:alpine

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
COPY package.json .

RUN npm install

# Bundle app source
COPY . .

# Run app
CMD [ "npm", "start" ]

This is my docker-compose.yml:

version: "3"

services:
  server:
    build: .
    environment:
      - NODE_ENV=development
    command: npm run debug
    volumes:
      - .:/usr/src/app
      - /usr/src/app/node_modules
    ports:
      - "8080:8080"
      - "9229:9229"
like image 272
n9iels Avatar asked Nov 07 '22 18:11

n9iels


1 Answers

You should use a separate folder for your node_modules and then set that folder in NODE_PATH environment

FROM node:alpine

# Create app directory
ENV NODE_PATH /usr/node_modules
WORKDIR /usr/node_modules

# Install app dependencies
COPY package.json .

RUN npm install

WORKDIR /usr/src/app

# Bundle app source
COPY . .

# Run app
CMD [ "npm", "start" ]

This way your code would be separate and the node_modules will there in image itself. Anytime you add a new module you will rebuild the image, else development can go fine withe mounted volume

like image 184
Tarun Lalwani Avatar answered Nov 14 '22 21:11

Tarun Lalwani