【问题标题】:Unable to compile typescript on heroku无法在heroku上编译打字稿
【发布时间】:2019-04-17 16:21:37
【问题描述】:

我正在尝试使用 express to 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"]
}

【问题讨论】:

  • 标准故障排除步骤:启用traceResolution 编译器选项并查看输出。

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


【解决方案1】:

发现问题,原来我没有注意文件名,即大小写。

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

VSCode/Windows 是自动处理大小写敏感的,所以它从不抛出错误,但是当部署到 heroku 时,服务器对文件名非常严格。

【讨论】:

    猜你喜欢
    • 2016-11-11
    • 1970-01-01
    • 2017-12-23
    • 1970-01-01
    • 2013-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-16
    相关资源
    最近更新 更多