无法在Heroku上编译打字稿

时间:2018-11-14 16:22:57

标签: node.js typescript express heroku typescript-typings

我正在尝试使用express向heroku部署一个typescript nodejs应用程序,代码被正确推送,heroku安装依赖项,然后运行tsc,但由于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"]
}

1 个答案:

答案 0 :(得分:0)

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

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

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