我刚接触docker并希望在docker中构建一个react app(使用typescript),需要使用semantic-ui-react。我按照此处的指示进行构建:https://mherman.org/blog/dockerizing-a-react-app/
我使用npm i -g semantic-ui-react
添加了semantic ui,这是我的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;
当我打开http://localhost:3001时,我遇到了这个错误:
./src/App.tsx
Module not found: Can't resolve 'semantic-ui-react' in '/app/src'
我曾尝试使用npm构建docker镜像,后来尝试使用yarn,希望能解决问题。
到目前为止我使用的构建命令如下:
docker-compose build --no-cache && docker-compose up -d --force-recreate
docker-compose build --no-cache
docker-compose up -d --force-recreate --build
这是我的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"]
我的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
我正在使用Ubuntu 18.04。
在我的Dockerfile中,我添加了cat package.json
以查看是否已添加了semantic ui,并且已经添加成功。
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"
]
}
}
我从终端执行了yarn start
,没有错误,当我在浏览器中打开项目时,它可以正常工作。
为什么Docker找不到Semantic UI模块?