Typescript React / NestJS 应用程序失败 @Heroku 构建 - "error TS2307: Cannot find module"

Typescript React / NestJS app fails @Heroku build - "error TS2307: Cannot find module"

我有一个 Typescript React/Express 应用程序,它在本地和@Heroku 上都运行良好。这周开始学习Nest.JS,所以把Express端换成了它。该应用程序在本地运行良好,但 Heroku 构建失败。我浏览了许多主题和指南,但未能找到问题所在,此时我什至无法确定问题是在 React 方面还是在 Nest.JS 方面。

Heroku 构建进展顺利,在某些时候它退出并显示以下内容:

remote: client/src/components/App.tsx:1:19 - error TS2307: Cannot find module 'react' or its corresponding type declarations.
remote: client/src/components/main/Navbar.tsx:3:20 - error TS2307: Cannot find module 'styled-components' or its corresponding type declarations.

基本上每个单独的 react、styled-component、router、helmet 等导入都会返回相同的错误消息。我想这与路径和类似的东西有关,我可以想象解决方案很简单,但我看不到。 :/

反应tsconfig.json

{
  "compilerOptions": {
    "baseUrl": "./src",
    "target": "es6",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx"
  },
  "include": [
    "src"
  ]
}

nestjs tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "moduleResolution": "node",
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": ".",
    "incremental": true,
    "jsx": "react-jsx"
  },
  "exclude": [
    "node_modules",
    "dist"
  ]
}

反应package.json

{
  "name": "project-002",
  "description": "project-002",
  "proxy": "http://127.0.0.1:3001",
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.8",
    "@testing-library/react": "^11.2.3",
    "@testing-library/user-event": "^12.6.0",
    "@types/node": "^14.14.20",
    "@types/react": "^17.0.0",
    "@types/react-dom": "^17.0.0",
    "@types/react-helmet": "^6.1.0",
    "@types/react-router-dom": "^5.1.7",
    "@types/styled-components": "^5.1.7",
    "axios": "^0.21.1",
    "dom": "0.0.3",
    "package": "^1.0.1",
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "react-ga": "^3.3.0",
    "react-helmet": "^6.1.0",
    "react-router-dom": "^5.2.0",
    "react-scripts": "^4.0.1",
    "styled-components": "^5.1.1",
    "typescript": "^4.1.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"
    ]
  }
}

nestjs package.json

{
  "name": "project-002",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "private": true,
  "engines": {
    "node": "14.4.0"
  },
  "license": "UNLICENSED",
  "scripts": {
    "prebuild": "rimraf dist",
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "node dist/main.js",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/src/main.js",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json"
  },
  "dependencies": {
    "@nestjs/common": "^7.5.1",
    "@nestjs/config": "^0.6.1",
    "@nestjs/core": "^7.5.1",
    "@nestjs/mapped-types": "^0.2.0",
    "@nestjs/platform-express": "^7.5.1",
    "compression": "^1.7.4",
    "helmet": "^4.3.1",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^6.6.3"
  },
  "devDependencies": {
    "@nestjs/cli": "^7.5.1",
    "@nestjs/schematics": "^7.1.3",
    "@nestjs/testing": "^7.5.1",
    "@types/express": "^4.17.8",
    "@types/jest": "^26.0.15",
    "@types/node": "^14.14.6",
    "@types/supertest": "^2.0.10",
    "@typescript-eslint/eslint-plugin": "^4.6.1",
    "@typescript-eslint/parser": "^4.6.1",
    "eslint": "^7.12.1",
    "eslint-config-prettier": "7.1.0",
    "eslint-plugin-prettier": "^3.1.4",
    "jest": "^26.6.3",
    "prettier": "^2.1.2",
    "supertest": "^6.0.0",
    "ts-jest": "^26.4.3",
    "ts-loader": "^8.0.8",
    "ts-node": "^9.0.0",
    "tsconfig-paths": "^3.9.0",
    "typescript": "^4.0.5"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".*\.spec\.ts$",
    "transform": {
      "^.+\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

知道应该做什么吗?

好的,我发现了我的错误。我会把线程留在上面以防将来有人需要。

我使用了这个脚本:

"heroku-postbuild": "npm install && npm install --only=dev --no-shrinkwrap && npm run build"

但我需要这个:

"heroku-postbuild": "cd client && npm install && npm install --only=dev --no-shrinkwrap && npm run build"

目前看起来一切都很好。