【问题标题】:How to make the host use the production build folder of the app?如何让宿主使用应用的生产构建文件夹?
【发布时间】:2021-01-28 12:40:47
【问题描述】:

我正在尝试将我的应用程序部署到 AWS 弹性 beantalk。我想要实现的是我希望我的所有 api 路由都从构建文件夹中获取,因为我已经编写了这样的配置:

process.env.DEV_PROXY
  ? app.use("/", devProxy)
  : app.use("*", express.static(path.join(__dirname, "..", "build")));

我的环境文件:

PORT=8080

我的 package.json

{
  "name": "phyxable",
  "version": "1.0.0",
  "private": true,
  "dependencies": {
    "@material-ui/core": "^4.11.0",
    "@material-ui/icons": "^4.9.1",
    "antd": "^4.7.0",
    "axios": "^0.20.0",
    "firebase": "^7.20.0",
    "node-sass": "^4.14.1",
    "react": "16.8.6",
    "react-calendar": "^3.1.0",
    "react-dom": "16.8.6",
    "react-google-maps": "^9.4.5",
    "react-jw-player": "^1.19.1",
    "react-markdown": "^4.3.1",
    "react-router-dom": "^5.2.0",
    "react-scripts": "2.1.8",
    "react-transition-group": "4.0.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "server": "node-env-run server --exec nodemon | pino-colada",
    "server:prod": "node server",
    "dev": "run-p server start"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ],
  "engines": {
    "node": "12.0.0"
  },
  "devDependencies": {
    "typescript": "3.3.3"
  }
}

但由于某种原因,当它被部署时,它总是使用开发版本。不使用构建文件夹的原因可能是什么。

【问题讨论】:

  • 也许在您的构建命令中您可以添加以下内容:DEV_PROXY=production yarn run build
  • 您没有在条件语句中检查 DEV_PROXY 的值。为开发或生产检查它的值,然后在部署时将 ENV 值更改为生产
  • 那么喜欢"build": "react-scripts build DEV_PROXY=production yarn run build"这个?

标签: node.js reactjs deployment build create-react-app


【解决方案1】:

我在 Heroku 环境中遇到了类似的问题。我注意到 env 变量没有返回正确的值。

如果我在你的位置,我会尝试 console.log(process.env.DEV_PROXY)。我的猜测是 AWS 中的价值不正确。

【讨论】:

  • 好吧,我什至试图通过只保留app.use("*", express.static(path.join(__dirname, "..", "build"))); in my server but it still didn't make any change. 来删除条件
猜你喜欢
  • 2020-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-25
  • 1970-01-01
相关资源
最近更新 更多