【问题标题】:How do I update the typescript compiler on my heroku server?如何更新我的 heroku 服务器上的 typescript 编译器?
【发布时间】:2019-12-05 09:47:09
【问题描述】:

我的 typescript 项目不会在我的 heroku 服务器上构建。

-----> Build
       Running build

       > back-end@1.0.0 build /tmp/build_32c5a59b6806145803516c50f84fc693
       > tsc && node build/index.js

       error TS5023: Unknown compiler option 'esModuleInterop'.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! back-end@1.0.0 build: `tsc && node build/index.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the back-end@1.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR!     /tmp/npmcache.DGTha/_logs/2019-07-27T07_56_31_141Z-debug.log
-----> Build failed

我能够在本地构建和运行应用程序。我本地的 tsc 版本是 3.5.1

但是在我的 heroku bash 中我明白了。

~ $ tsc -v
message TS6029: Version 1.5.3

即使我在 heroku 上的 package.json 看起来像这样:

~ $ cat package.json
{
  "name": "back-end",
  "version": "1.0.0",
  "description": "",
  "main": "dist/index.js",
  "scripts": {
    "precompile": "rimraf build",
    "compile": "tsc && node build/index.js",
    "dev": "nodemon --watch src/ --exec \"npm run compile\" --verbose -e ts",
    "start": "tsc && node build/index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@types/cookie-parser": "^1.4.1",
    "@types/cors": "^2.8.5",
    "@types/express": "^4.17.0",
    "@types/jsonwebtoken": "^8.3.2",
    "@types/mongoose": "^5.5.11",
    "@types/node": "^12.6.8",
    "@types/passport-local": "^1.0.33",
    "axios": "^0.19.0",
    "body-parser": "^1.18.3",
    "cookie-parser": "^1.4.3",
    "cors": "^2.8.5",
    "crypto": "^1.0.1",
    "express": "^4.16.4",
    "express-jwt": "^5.3.1",
    "express-session": "^1.15.6",
    "jsonwebtoken": "^8.4.0",
    "mixer-client-node": "^2.7.1",
    "mongoose": "^5.4.1",
    "passport": "^0.4.0",
    "passport-local": "^1.0.0",
    "passport-mixer": "^1.0.1",
    "tsc": "^1.20150623.0",
    "uuid": "^3.3.2",
    "ws": "^7.0.1"
  },
  "eslintConfig": {
    "parser": "@typescript-eslint/parser",
    "plugins": [
      "@typescript-eslint"
    ],
    "extends": [
      "plugin:@typescript-eslint/recommended",
      "prettier/@typescript-eslint",
      "plugin:prettier/recommended"
    ],
    "rules": {
      "@typescript-eslint/indent": [
        "error",
        2
      ],
      "linebreak-style": 0
    }
  },
  "devDependencies": {
    "@babel/cli": "^7.5.5",
    "@babel/core": "^7.5.5",
    "@typescript-eslint/eslint-plugin": "^1.13.0",
    "@typescript-eslint/parser": "^1.13.0",
    "concurrently": "^4.1.0",
    "eslint": "^5.12.0",
    "eslint-config-airbnb": "^17.1.1",
    "eslint-config-prettier": "^6.0.0",
    "eslint-plugin-import": "^2.14.0",
    "eslint-plugin-jsx-a11y": "^6.1.2",
    "eslint-plugin-prettier": "^3.1.0",
    "eslint-plugin-react": "^7.12.3",
    "eslint-watch": "^4.0.2",
    "prettier": "^1.18.2",
    "rimraf": "^2.6.3",
    "typescript": "^3.5.3"
  }
}

通知"tsc": "^1.20150623.0"。正如你所期望的,这与我的本地 package.json 完全相同。

但是当我在本地机器上运行 tsc -v 时,我得到了这个:

> tsc -v

Version 3.5.3

我尝试通过运行npm uninstall -g tsc 来解决此问题,然后通过heroku bash 重新安装。这并没有解决问题。

为什么我的 Heroku 服务器上的 tsc 版本如此过时?为什么默认全局安装 tsc? (我必须使用 npm 脚本来检查我机器上的版本)。如何在 heroku 上更新 tsc 并解决此问题?

【问题讨论】:

    标签: typescript heroku


    【解决方案1】:

    我在使用 TypeScript 部署 React 项目时遇到了一些问题。所以我找到了解决我的问题的方法是使用 Webpack。 Webpack 将从你的 Typescript 文件生成一个 main.js 文件,这样 Heroku 就可以知道你部署了什么。

    您可以在这里找到更多信息:enter link description here

    【讨论】:

      【解决方案2】:

      我发现我做错了什么。

      首先,tsc 不是一个有效的 npm 模块,不要安装它。

      相反,tsc bash 命令来自 typescript 模块。

      如果你的 Heroku 服务器上的 npm 模块版本有问题,请尝试禁用 node_modules 缓存

      https://devcenter.heroku.com/articles/nodejs-support#cache-behavior

      这为我解决了这个问题。

      【讨论】:

        【解决方案3】:

        tsc doesn't seem to be an NPM package,所以你可以完全删除它。目前只是一个混乱。我想你想要typescript

        您的package.json 中确实有typescript,但它列在devDependencies 下,而不是dependencies。默认you can't use devDependencies at runtime:

        运行安装后,build steps Heroku 将在部署应用程序之前剥离devDependencies 下声明的包。

        如果您在运行时确实需要tsc,请将其移至dependencies

        【讨论】:

        • 你是对的!我发现了这一点,我会更新我的问题。但是,我尝试将 Typescript 移至依赖项,但这并没有解决问题。我这样做是为了在构建脚本中运行tsc,所以它不应该有所作为。在我的 Heroku 服务器上仍然存在这个问题,Typescript 已过时。
        【解决方案4】:

        正如您所注意到的,您可能已经全局安装了 tsc,这将忽略您在 package.json 中定义的内容。要专门使用您在项目中安装的那个,请尝试运行

        npx tsc -v

        这将使用您本地安装的 typescript 和 tsc。当然,你可以使用npx tsc 来编译你的项目,使用你在你的包中指定的 tsc 版本。

        【讨论】:

        • 这很有帮助(我不知道 npx 命令),但它并没有解决我的问题。 npx tsc -v 仍然显示 message TS6029: Version 1.5.3 并且我的项目在将 npm 构建脚本更改为具有 npx tsc 时无法编译请记住,此问题仅存在于我的 heroku 服务器上
        猜你喜欢
        • 1970-01-01
        • 2021-04-16
        • 1970-01-01
        • 1970-01-01
        • 2016-06-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-17
        相关资源
        最近更新 更多