如果我在Heroku上将NODE_ENV变量设置为生产,则找不到用于构建NESTJS api的NX命令

wi3ka0sx  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(113)

我的项目使用NX mono存储库。它包含以下三个应用程序:

  • 网络应用程序(NextJS),
  • API服务(NestJS)
  • 移动的应用程序(React Native)

我试图将后端代码部署到Heroku,但当我将NODE_ENV变量设置为production时,我遇到了找不到包的问题。所有NX包都默认存储为dev依赖项。如果我将NODE_ENV设置为development,它将正常工作。

**问题:**解决此问题的理想方法是什么?我不想将NODE_ENV设置为development,因为否则会从devDependencies安装无用的软件包,从而降低部署速度。

任何帮助都将不胜感激。
这是我的package.json的样子。

"scripts": {
    "api": "nx serve api",
    "build:api": "nx build api",
    "api:prod": "node dist/apps/api/main.js",
},
"dependencies": {
    "@emotion/react": "^11.10.4",
    "@emotion/styled": "^11.10.4",
    "@hookform/resolvers": "^2.9.7",
    "@mui/icons-material": "^5.10.3",
    "@mui/material": "^5.10.4",
    "@nestjs/common": "^9.0.0",
    "@nestjs/config": "^2.2.0",
    "@nestjs/core": "^9.0.0",
    "@nestjs/jwt": "^9.0.0",
    "@nestjs/mongoose": "^9.2.0",
    "@nestjs/passport": "^9.0.0",
    "@nestjs/platform-express": "^9.0.0",
    "@react-native-community/clipboard": "^1.5.1",
    "@react-navigation/bottom-tabs": "^6.3.3",
    "@react-navigation/material-top-tabs": "^6.2.4",
    "@react-navigation/native": "^6.0.11",
    "@react-navigation/native-stack": "^6.7.0",
    "@react-three/drei": "^9.34.3",
    "@react-three/fiber": "^8.8.9",
    "@tanstack/react-query": "^4.2.3",
    "@trycourier/courier": "^3.15.0",
    "@twotalltotems/react-native-otp-input": "^1.3.11",
    "axios": "^0.27.2",
    "bcryptjs": "^2.4.3",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.13.2",
    "cookies": "^0.8.0",
    "core-js": "^3.6.5",
    "http-proxy": "^1.18.1",
    "jwt-decode": "^3.1.2",
    "lodash": "^4.17.21",
    "material-ui-phone-number": "^3.0.0",
    "mongoose": "^6.5.2",
    "mui-tel-input": "^2.0.1",
    "native-base": "^3.4.13",
    "next": "12.2.3",
    "npm": "^8.19.2",
    "passport": "^0.6.0",
    "passport-jwt": "^4.0.0",
    "passport-local": "^1.0.0",
    "patch-package": "^6.4.7",
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "react-hook-form": "^7.34.2",
    "react-is": "18.2.0",
    "react-native": "0.69.3",
    "react-native-gesture-handler": "^2.6.0",
    "react-native-pager-view": "^6.0.1",
    "react-native-phone-number-input": "^2.1.0",
    "react-native-reanimated": "^2.10.0",
    "react-native-safe-area-context": "^4.3.1",
    "react-native-screens": "^3.15.0",
    "react-native-snackbar": "^2.4.0",
    "react-native-tab-view": "^3.2.1",
    "react-native-vector-icons": "^9.2.0",
    "react-native-webview": "^11.23.1",
    "react-otp-input": "^2.4.0",
    "reflect-metadata": "^0.1.13",
    "regenerator-runtime": "0.13.7",
    "rxjs": "^7.0.0",
    "styled-components": "5.3.5",
    "three": "^0.145.0",
    "tslib": "^2.3.0",
    "twilio": "^3.81.0",
    "yup": "^0.32.11"
  },
  "devDependencies": {
    "@commitlint/cli": "^17.0.3",
    "@commitlint/config-conventional": "^17.0.3",
    "@nestjs/schematics": "^9.0.0",
    "@nestjs/testing": "^9.0.0",
    "@nrwl/cli": "14.5.4",
    "@nrwl/cypress": "14.5.4",
    "@nrwl/detox": "14.5.6",
    "@nrwl/eslint-plugin-nx": "14.5.4",
    "@nrwl/jest": "14.5.4",
    "@nrwl/linter": "14.5.4",
    "@nrwl/nest": "14.5.4",
    "@nrwl/next": "^14.5.4",
    "@nrwl/node": "14.5.4",
    "@nrwl/react": "14.5.4",
    "@nrwl/react-native": "^14.5.6",
    "@nrwl/web": "14.5.4",
    "@nrwl/workspace": "14.5.4",
    "@react-native-async-storage/async-storage": "1.17.7",
    "@react-native-community/cli": "8.0.4",
    "@react-native-community/cli-platform-android": "8.0.4",
    "@react-native-community/cli-platform-ios": "8.0.4",
    "@testing-library/dom": "^8.18.1",
    "@testing-library/jest-dom": "5.16.4",
    "@testing-library/jest-native": "4.0.5",
    "@testing-library/react": "13.3.0",
    "@testing-library/react-native": "11.0.0",
    "@testing-library/user-event": "^14.4.3",
    "@types/bcryptjs": "^2.4.2",
    "@types/cookies": "^0.7.7",
    "@types/jest": "27.4.1",
    "@types/lodash": "^4.14.182",
    "@types/node": "16.11.7",
    "@types/passport-jwt": "^3.0.6",
    "@types/passport-local": "^1.0.34",
    "@types/react": "18.0.15",
    "@types/react-dom": "18.0.6",
    "@types/react-is": "17.0.3",
    "@types/react-native": "0.69.3",
    "@types/react-native-vector-icons": "^6.4.12",
    "@types/styled-components": "5.1.25",
    "@types/three": "^0.144.0",
    "@typescript-eslint/eslint-plugin": "^5.29.0",
    "@typescript-eslint/parser": "^5.29.0",
    "babel-jest": "27.5.1",
    "babel-plugin-styled-components": "1.10.7",
    "cypress": "^10.2.0",
    "detox": "19.7.1",
    "eslint": "~8.15.0",
    "eslint-config-next": "12.2.3",
    "eslint-config-prettier": "8.1.0",
    "eslint-plugin-cypress": "^2.10.3",
    "eslint-plugin-import": "2.26.0",
    "eslint-plugin-jsx-a11y": "6.6.1",
    "eslint-plugin-react": "7.30.1",
    "eslint-plugin-react-hooks": "4.6.0",
    "husky": "^8.0.0",
    "jest": "27.5.1",
    "jest-circus": "27.5.1",
    "jest-react-native": "18.0.0",
    "lint-staged": "^13.0.3",
    "metro": "0.71.3",
    "metro-babel-register": "0.71.3",
    "metro-react-native-babel-preset": "0.71.3",
    "metro-resolver": "0.71.3",
    "msw": "^0.47.2",
    "nx": "14.5.4",
    "prettier": "^2.6.2",
    "prettier-plugin-organize-imports": "^3.1.0",
    "react-devtools": "^4.26.0",
    "react-native-config": "1.4.6",
    "react-native-svg": "12.4.3",
    "react-native-svg-transformer": "1.0.0",
    "react-test-renderer": "18.2.0",
    "ts-jest": "27.1.4",
    "ts-node": "~10.8.0",
    "typescript": "~4.7.2"
  },

这是我的过程文件:

web: yarn build:api && yarn api:prod
shstlldc

shstlldc1#

Nx是作为devDependency安装的。Heroku在build步骤之后擦除devDependencies,这样您就不会对最终的环境有额外的依赖。您应该做的是在package.json中有一个build脚本,用于构建启动服务器所需的一切,然后有一个start脚本,用于启动应用程序。或者在Procfile中设置web属性,就像您当前对build:api && api:prod所做的那样。但是,此启动脚本依赖于任何devDependencies。它应该能够仅使用生产相关性运行。

相关问题