繁体   English   中英

无法在Heroku上编译打字稿

[英]Unable to compile typescript on heroku

我正在尝试使用Express向Heroku部署一个Typescript Node.js应用程序,代码被正确推送,Heroku安装依赖项,然后运行tsc但由于src/controller/adminTypes.ts:3:34 - error TS2307: Cannot find module '../repo/adminTypes'.崩溃src/controller/adminTypes.ts:3:34 - error TS2307: Cannot find module '../repo/adminTypes'.

我检查了发生导入的行,它看起来像这样

import { insertAdminTypes } from "../repo/adminTypes";

等效的输出是这样的

export const insertAdminTypes = async queryObj => {
  // code
};

VSCode可以完美地解析路径,当我尝试在本地运行tsc时,一切都可以正常编译,并且在dist文件夹中获得了已编译的文件。 似乎只有heroku会为在线路径抛出错误。 并非每个文件都发生这种情况,仅针对少数几个文件会引发上述TS2307错误。

这些是以下配置文件。

package.json

{
  "name": "express_server",
  "version": "0.0.1",
  "license": "UNLICENSED",
  "main": "./src/server.ts",
  "scripts": {
    "build-ts": "tsc",
    "start": "npx nodemon",
    "prod": "npx run build-ts",
    "postinstall": "npm run build-ts"
  },
  "dependencies": {
    "@types/body-parser": "^1.17.0",
    "@types/dotenv": "^6.1.0",
    "@types/express": "^4.16.0",
    "@types/jquery": "^3.3.22",
    "@types/node": "^10.12.2",
    "@types/underscore": "^1.8.9",
    "bcrypt": "^3.0.2",
    "body-parser": "^1.18.3",
    "dotenv": "^6.1.0",
    "express": "^4.16.4",
    "express-handlebars": "^3.0.0",
    "jsonwebtoken": "^8.3.0",
    "mongodb": "^3.1.9",
    "mysql": "^2.16.0",
    "nodemon": "^1.18.6",
    "reflect-metadata": "^0.1.12",
    "ts-node": "^7.0.1",
    "typeorm": "^0.2.8",
    "typescript": "^3.1.6",
    "underscore": "^1.9.1"
  },
  "devDependencies": {
    "husky": "^1.1.3",
    "lint-staged": "^8.0.4",
    "prettier": "^1.15.2",
    "pretty-quick": "^1.8.0",
    "tslint": "^5.11.0",
    "tslint-config-prettier": "^1.15.0",
    "tslint-plugin-prettier": "^2.0.1"
  },
  "husky": {
    "hooks": {
      "pre-commit": "pretty-quick --staged"
    }
  },
  "lint-staged": {
    "linters": {
      "*.{ts}": [
        "npx tslint --fix",
        "git add"
      ]
    },
    "ignore": [
      "./public",
      "./environment",
      "./node_modules"
    ]
  }
}

nodemon.json

{
  "ignore": ["**/*.test.ts", "**/*.spec.ts", ".git", "node_modules"],
  "watch": ["src"],
  "exec": "npx ts-node ./src/server.ts",
  "ext": "ts"
}

tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "esModuleInterop": true,
    "target": "es6",
    "noImplicitAny": false,
    "moduleResolution": "node",
    "sourceMap": true,
    "outDir": "dist",
    "pretty": true,
    "baseUrl": ".",
    "alwaysStrict": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "paths": {
      "*": ["node_modules/*", "src/types/*"]
    }
  },
  "include": ["src/**/*.ts"],
  "exclude": ["node_modules"]
}

发现了问题,事实证明我没有注意文件名,即大小写。

例如,如上所述抛出的错误Cannot find module '../repo/adminTypes'是由于实际文件名是AdminTypes ,大写A

VSCode / Windows可以自动区分大小写,因此它从不会抛出错误,但是在部署到heroku时,服务器对文件名非常严格。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM