3

I am new to docker and I want to build a react app (with typescript) inside docker and I need to use semantic-ui-react for that. I followed the instruction from here to build by app : https://mherman.org/blog/dockerizing-a-react-app/

I added semantic ui usingnpm i -g semantic-ui-react and this is my App.tsx:

import React from 'react';
import './App.css';
import {Button} from "semantic-ui-react";

class App extends React.PureComponent {

    render() {
        return (<div>
            This is text
            <Button>
                ok
            </Button>
        </div>)
    }
}

export default App;

When I open http://localhost:3001 I get this error :

./src/App.tsx
Module not found: Can't resolve 'semantic-ui-react' in '/app/src'

I tried to build the docker image using npm then I changed to yarn thinking it will fix. Build commands i've used so far:

docker-compose build --no-cache && docker-compose up -d --force-recreate

docker-compose build --no-cache

docker-compose up -d --force-recreate --build

my Dockerfile

# base image
FROM node:12.2.0-alpine

# set working directory
WORKDIR /app

# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH

# install and cache app dependencies
COPY package.json /app/package.json
RUN cat package.json
RUN yarn install

# start app
CMD ["yarn", "start"]

my docker-compose.yml:

version: '2.2'

volumes:
  frontend:

services:

  frontend:
    container_name: container
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - '.:/app'
      - '/app/node_modules'
    ports:
      - '3001:3000'
    environment:
      - NODE_ENV=development

I'm using Ubuntu 18.04

Inside my Dockerfile I've added cat package.json to see if semantic ui is added and it is.

Step 5/7 : RUN cat package.json
 ---> Running in b6bbcda3221b
{
  "name": "frontend",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.3.2",
    "@testing-library/user-event": "^7.1.2",
    "@types/jest": "^24.0.0",
    "@types/node": "^12.0.0",
    "@types/react": "^16.9.0",
    "@types/react-dom": "^16.9.0",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-scripts": "3.4.0",
    "semantic-ui-react": "^0.88.2",
    "typescript": "~3.7.2"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

I've executed yarn start from terminal and there is no error and the project works fine when I open it in browser.

Why doesn't docker find the semantic ui module ?

R. Barbus
  • 73
  • 1
  • 7

1 Answers1

1

Apparently this is a duplicate of Docker-compose: node_modules not present in a volume after npm install succeeds

And the answer from FrederikNS applies here as well. I removed

- '/app/node_modules' from volumes inside docker-compose.yml and now it recognize all modules

R. Barbus
  • 73
  • 1
  • 7