gpt4 book ai didi

reactjs - Docker 找不到语义-ui-react

转载 作者:行者123 更新时间:2023-12-03 14:22:22 25 4
gpt4 key购买 nike

我是 docker 新手,我想在 docker 内构建一个 React 应用程序(带有 typescript),并且我需要使用语义-ui-react。我按照此处的说明通过应用程序进行构建:https://mherman.org/blog/dockerizing-a-react-app/

我使用npm i -g Semantic-ui-react添加了语义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 以查看是否添加了语义 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 找不到语义 ui 模块?

最佳答案

显然这是 Docker-compose: node_modules not present in a volume after npm install succeeds 的重复项

FrederikNS 的答案也适用于此。我删除了

- '/app/node_modules' 来自 docker-compose.yml 内的卷,现在它可以识别所有模块

关于reactjs - Docker 找不到语义-ui-react,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60772421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com