Docker doesn't find semantic-ui-react - reactjs

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 ?

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

Related

Cannot deploy React app to Firebase hosting properly from github Action

I can deploy React app to Firebase properly but If I try to deploy from Github Actions, It can't be deployed(404 error).
The directory structure is like this:
- github
- workflow
- action.yaml
- public
- sub-dir(React App)
- build
When I deploy a project with this command,
firebase deploy --only hosting
It will run correctly.
(The URL is [my-domain]/sub-dir/build)
but when it is deployed from github actions, getting 404 error page even though accessing the same URL.
this is the statement of action.yaml
name: Continuous Deployment
on:
push:
### Triggers a push to the main branch and executes the process
branches:
- main
jobs:
deploy:
runs-on: ubuntu-20.04
steps:
- name: checkout branch
uses: actions/checkout#v3
with:
fetch-depth: 0
- name: configure git info
run: |
git remote set-url origin https://github-actions:${GITHUB_TOKEN}#github.com/${GITHUB_REPOSITORY}
git config --global user.name "${GITHUB_ACTOR}"
git config --global user.email "${GITHUB_ACTOR}#users.noreply.github.com"
- name: check git status and branch
run: |
git status
git branch
### sub-dir build
- name: sub-dir build
run: |
cd public/sub-dir
yarn install
yarn build
- name: push the built project
run: |
git add .
git commit -m "Push different part from GitHub Actions"
git push origin HEAD:${GITHUB_REF}
### Firebase deploy
- uses: actions/setup-node#v3
with:
node-version: "14.18.3"
cache: npm
- name: Checkout Repo
uses: actions/checkout#master
- name: Install Dependencies
run: npm install
- name: Install Functions Dependencies
run: |
node -v
cd functions && npm install
npm i -g firebase-tools
firebase --version
git pull origin HEAD:${GITHUB_REF}
- name: Deploy to Firebase
uses: w9jds/firebase-action#master
with:
args: deploy --only hosting
env:
FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
package.json
{
"name": "xxx",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.16.5",
"#testing-library/react": "^13.4.0",
"#testing-library/user-event": "^13.5.0",
"#types/jest": "^27.5.2",
"#types/node": "^16.18.11",
"#types/react": "^18.0.26",
"#types/react-dom": "^18.0.10",
"firebase": "^9.15.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-redux": "^8.0.5",
"react-scripts": "5.0.1",
"redux": "^4.2.0",
"typescript": "^4.9.4",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"#types/react-redux": "^7.1.25"
},
"homepage": "./"
}
I have no idea about the reason. I would be happy to get the answer to this.
Thank you in advance.

React on AWS Amplify, Code in AWS CodeCommit - CustomerError: Artifact directory doesn't exist: build

I am trying to figure out why the directory for my build can't be found. I have tried many versions that I found on other pages ./myapp/build, build, ./build/. I have tried cding into the folder. I am hosting my code in AWS CodeCommit.
I am at a loss. I am not a front-end developer, so maybe I am missing something very simple here. It loads locally perfectly fine.
package.json
{
"name": "myapp",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.16.5",
"#testing-library/react": "^13.4.0",
"#testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
amplify.yml
version: 1
frontend:
phases:
# IMPORTANT - Please verify your build commands
preBuild:
commands:
- npm ci
build:
commands:
- echo 'BEFORE BUILD'
- ls
- npm run build
- echo 'AFTER BUILD'
- ls
artifacts:
# IMPORTANT - Please verify your build output directory
baseDirectory: /myapp/build
files:
- '**/*'
cache:
paths:
- node_modules/**/*
I had luck following this tutorial integrating github and amplify. Also here is my buildspec for reference as well : )
version: 1
frontend:
phases:
preBuild:
commands:
- npm ci
build:
commands:
- npm run build
artifacts:
baseDirectory: build
files:
- '**/*'
cache:
paths:
- node_modules/**/*

Why is AWS CodeBuild failing npm run build step when it is succeeding locally?

I have a ReactJS app that I am building and deploying with AWS CodePipeline.
My CodePipeline is triggered on pushes to Github. For some reason, the build is failing in CodeBuild with error:
[Container] 2021/01/18 02:59:02 Running command npm run build
> liferoll-web#0.1.0 build /codebuild/output/src739247890/src
> react-scripts build
Creating an optimized production build...
Failed to compile.
./src/App.tsx
Cannot find module: 'package-name'. Make sure this package is installed.
You can install this package by running: npm install package-name.
But is succeeding fine on other machines. This is so strange to me as the only place I can reproduce the error is on CodeBuild.
buildspec.yml
version: 0.2
phases:
pre_build:
commands:
- npm install
build:
commands:
- echo Build started on `date`
- npm run build
files:
- '**/*'
base-directory: build
package.json
{
"name": "my-website",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.11.9",
"#testing-library/react": "^11.2.3",
"#testing-library/user-event": "^12.6.0",
"#types/jest": "^26.0.20",
"#types/node": "^12.19.13",
"#types/react": "^16.14.2",
"#types/react-dom": "^16.9.10",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-scripts": "4.0.1",
"react-stacked-carousel": "github:liferoll/react-stacked-carousel",
"typescript": "^4.1.3",
"web-vitals": "^0.2.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
App.tsx imports:
import React, {useRef, useState} from 'react';
import logo from './assets/liferoll-logo.png';
import iosDownloadSVG from './assets/download-ios.svg'
import qrCode from './assets/qrcode.png'
import loaderGif from './assets/loading.gif'
import clip1 from './assets/clip1.mov'
import clip2 from './assets/clip2.mov'
import clip3 from './assets/clip3.mov'
import clip4 from './assets/clip4.mov'
import './App.css';
//https://github.com/saadqbal/react-stacked-carousel/issues/1
import {StackedCarousel} from 'react-stacked-carousel'
import 'react-stacked-carousel/dist/index.css';
I have no idea how to correctly debug this and would appreciate guidance. Posting here to vet-out any obvious issues
There are many reasons why something would work the way you intend locally, but fail on a remote machine such as CodeBuild.
From the immediate error however, it's telling you that something is not installed or available at the time of building.
Cannot find module: 'package-name'. Make sure this package is installed.
This may not have to do with a direct import, but an import of something you're importing. My perception is your usage of "react-stacked-carousel" is abnormal and highly suspect.

TypeError: Path must be a string. DOCKER REACT

I can run my react app locally (either on windows and linux) by typing in console
npm run
but when i try run it in docker container it runs and listening on port 3001, but when i open the app by browser i have following error (with internal server error response):
TypeError: Path must be a string. Received undefined
at assertPath (path.js:28:11)
at Object.join (path.js:1236:7)
at noopServiceWorkerMiddleware (/app/node_modules/react-dev-utils/noopServiceWorkerMiddleware.js:14:26)
at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/app/node_modules/express/lib/router/index.js:317:13)
at /app/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/app/node_modules/express/lib/router/index.js:335:12)
at next (/app/node_modules/express/lib/router/index.js:275:10)
at launchEditorMiddleware (/app/node_modules/react-dev-utils/errorOverlayMiddleware.js:20:7)
at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/app/node_modules/express/lib/router/index.js:317:13)
at /app/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/app/node_modules/express/lib/router/index.js:335:12)
at next (/app/node_modules/express/lib/router/index.js:275:10)
at handleWebpackInternalMiddleware (/app/node_modules/react-dev-utils/evalSourceMapMiddleware.js:42:7)
at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
my docker file:
# 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 npm uninstall -g jest
RUN npm install -g jest
RUN npm cache clean --force
RUN npm install
RUN npm install --silent
RUN npm install react-scripts#3.4.0 -g --silent
# start app
CMD ["npm", "start"]
and my package.json file
{
"name": "randevou-react",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.4.0",
"#testing-library/user-event": "^7.2.1",
"#types/react-router": "^5.1.4",
"react": "^16.12.0",
"react-datepicker": "^2.12.1",
"react-dom": "^16.12.0",
"react-router-dom": "^5.1.2",
"react-scripts": "^3.4.0",
"typescript": "^3.7.4"
},
"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 run container by typing
docker run -d -v /app -v /app/node_modules -p 3001:3000 --rm randevoureact
on container, my node version is 8.10.0 and my npm version is 5.6.0
i'm totally newbie at dockers and front-end issues
i don't know if it is importat but im using react-router in my react application
can somebody help me :(?

Local npm dependency "does not a contain a package.json file" in docker build, but runs fine with npm start

I have an npm module I'm working on locally that is a dependency in a client app.
Directory structure is basically the following:
/app
/client
/src
App.js
package.json
Dockerfile.dev
/shared
/contexts
package.json
test.js
/hooks
My package.json is the following:
{
"name": "web",
"version": "0.1.0",
"private": true,
"dependencies": {
"contexts": "file:../shared/contexts",
"react": "^16.10.2",
"react-dom": "^16.10.2",
"react-scripts": "3.2.0"
},
"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"
]
}
}
Importing with the following into client/src/App.js:
import { testImport } from 'contexts/test';
Works as expected when I run npm start.
The issue I'm running into is with running:
docker build -t sockpuppet/testapp -f Dockerfile.dev .
It fails and I get an error:
npm ERR! Could not install from "../shared/contexts" as it does not contain a package.json file.
Here is he Dockerfile.dev
FROM node:alpine
WORKDIR '/app'
COPY ./package.json ./
RUN npm install
COPY . .
CMD ["npm", "run", "start"]
How should I be handling local npm dependencies?
Also, adding something like the following to COPY the /shared into the image generates a COPY failed: Forbidden path outside the build context: ../shared/contexts () error:
COPY ../shared ./
Ok, this works. I changed my Dockerfile.dev to the following:
FROM node:alpine
WORKDIR '/app'
COPY ./shared /shared
COPY ./web /app
RUN npm install
CMD ["npm", "run", "start"]
From the base project directory (where /shared and /web reside), I run:
docker build -t sockpuppet/client -f ./web/Dockerfile.dev .

Resources