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

标签 node.js typescript express heroku

Heroku 构建错误

- TSError: ⨯ Unable to compile TypeScript:
- 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.
- Try `npm i --save-dev @types/express` if it exists or add a new declaration (.d.ts) file containing `declare module 'express';`
- 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`.

试图找出如何让我的代码符合 heroku 的要求已经有一段时间了,最​​新的错误如上所示,我不确定我需要更改什么才能使其正常工作。我一直在遵循指南,在我的 package.json 脚本中有一个 @types/express@types/node 等。但是,它没有阅读它们。不知道从这里去哪里,帮助将不胜感激。

tsconfig.json

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

简介

web:ts-node/src/server.ts 

src/server.ts

import express, { Application } from 'express';
import { routes } from './routes';

// Boot express
export const app: Application = express();

// Application routing
routes(app);

const port = process.env.PORT || 5000;

// Start server
app.listen(port, () => console.log(`Server is listening on port ${port}!`));

package.json

{
  "name": "my-app",
  "version": "1.0.0",
  "main": "src/server.ts",
  "type": "commonjs",
  "scripts": {
    "start": "ts-node src/server.ts",
    "dev": "nodemon -x ts-node src/server.ts",
    "build": "tsc",
    "test": "jest --coverage"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "husky": {
    "hooks": {
      "pre-commit": "pretty-quick --staged"
    }
  },
  "devDependencies": {
    "@types/cors": "^2.8.12",
    "@types/express": "^4.17.13",
    "@types/node": "^16.7.6",
    "@types/jest": "^26.0.24",
    "@types/supertest": "^2.0.11",
    "@typescript-eslint/eslint-plugin": "^4.29.0",
    "@typescript-eslint/parser": "^4.29.0",
    "eslint": "^7.32.0",
    "eslint-config-standard": "^16.0.3",
    "eslint-plugin-import": "^2.23.4",
    "eslint-plugin-node": "^11.1.0",
    "eslint-plugin-promise": "^5.1.0",
    "husky": "^7.0.1",
    "jest": "^27.0.6",
    "nodemon": "^2.0.12",
    "prettier": "^2.3.2",
    "pretty-quick": "^3.1.1",
    "supertest": "^6.1.4",
    "ts-jest": "^27.0.4"
  },
  "dependencies": {
    "axios": "^0.21.1",
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "ts-node": "^10.1.0",
    "typescript": "^4.3.5"
  },
  "description": ""
}

最佳答案

想通了。

Heroku 在构建您的项目时会删除开发依赖项。由于我所有的 @types 都位于 devDependencies 中,因此无法访问它们并导致初始帖子中显示的错误。

package.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"
  }

关于node.js - Heroku 构建在 package.json 文件中找不到 `@types`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68975342/

相关文章:

node.js - 通过公共(public)Apache服务器访问服务器局域网中的node.js、mongo DB

node.js - 当我们已经有了 fs.read 和 fs.write 时,为什么还要使用 fs.open 呢?

typescript - 强制对传递给 TypeScript 函数的变量进行过度属性检查

json - tsconfig.json : Build:No inputs were found in config file

javascript - twilio 中的 statusCallback 未触发

node.js - MongoDB + NodeJS Web 应用程序的初始数据库结构/数据

mysql - Grunt 部署/bin/sh : mysql: command not found

typescript - Angular2,通过字符串/名称手动解析类型

mongodb - 无法使用 Node.js Express MongoDB Mongoose CoffeeScript 进行 POST

node.js - 我们如何使用 jenkins 和 nodejs 设置并行运行测试?