Heroku 构建在 package.json 文件中找不到 `@types`

kev*_*vin 2 heroku node.js express typescript

Heroku 构建错误

\n
- TSError: \xe2\xa8\xaf Unable to compile TypeScript:\n- src/server.ts(1,38): error TS7016: Could not find a declaration file for module \'express\'. \'/app/node_modules/express/index.js\' implicitly has an \'any\' type.\n- Try `npm i --save-dev @types/express` if it exists or add a new declaration (.d.ts) file containing `declare module \'express\';`\n- src/server.ts(10,14): error TS2580: Cannot find name \'process\'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.\n
Run Code Online (Sandbox Code Playgroud)\n

试图弄清楚如何让我的代码达到 Heroku 的状态已经被耽搁了一段时间,最新的错误如上面所示,我不确定需要更改什么才能使其正常工作。我一直在遵循指南,并在我的 package.json 脚本中有@types/express、等。@types/node然而,它并没有读取它们。不知道从这里去哪里,我们将不胜感激。

\n

tsconfig.json

\n
- TSError: \xe2\xa8\xaf Unable to compile TypeScript:\n- src/server.ts(1,38): error TS7016: Could not find a declaration file for module \'express\'. \'/app/node_modules/express/index.js\' implicitly has an \'any\' type.\n- Try `npm i --save-dev @types/express` if it exists or add a new declaration (.d.ts) file containing `declare module \'express\';`\n- src/server.ts(10,14): error TS2580: Cannot find name \'process\'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.\n
Run Code Online (Sandbox Code Playgroud)\n

过程文件

\n
web:ts-node/src/server.ts \n
Run Code Online (Sandbox Code Playgroud)\n

src/服务器.ts

\n
import express, { Application } from \'express\';\nimport { routes } from \'./routes\';\n\n// Boot express\nexport const app: Application = express();\n\n// Application routing\nroutes(app);\n\nconst port = process.env.PORT || 5000;\n\n// Start server\napp.listen(port, () => console.log(`Server is listening on port ${port}!`));\n
Run Code Online (Sandbox Code Playgroud)\n

包.json

\n
{\n  "compilerOptions": {\n    "target": "es2017",\n    "module": "commonjs",\n    "sourceMap": true,\n    "outDir": "./build",\n    "rootDir": "./src",\n    "strict": true,\n    "noImplicitAny": true,\n    "moduleResolution": "node",\n    "baseUrl": "./src",\n    "esModuleInterop": true,\n    "skipLibCheck": true,\n    "forceConsistentCasingInFileNames": true\n  },\n  "lib": ["es2015"],\n  "include": ["src/**/*.ts"],\n  "exclude": ["node_modules", "src/**/*.test.ts"]\n}\n
Run Code Online (Sandbox Code Playgroud)\n

kev*_*vin 7

弄清楚了。

Heroku 在构建项目时会修剪开发依赖项。由于我的所有@types位置都devDependencies无法访问,因此导致了最初的帖子中显示的错误。

包.json

  // i also added engine specifications. 
  // the docs suggest matching your configurations as closely to your dev 
  // environment as possible for obvious reasons.  
  "engines": {
    "node": "15.x",
    "npm": "7.x"
  },
...
  "dependencies": {
    "@types/cors": "^2.8.12",
    "@types/express": "^4.17.13",
    "@types/node": "^16.7.6",
    "axios": "^0.21.1",
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "ts-node": "^10.2.1",
    "typescript": "^4.4.2"
  }
Run Code Online (Sandbox Code Playgroud)