【问题标题】:How can i deploy nodejs express typescript app to Heroku?如何将 nodejs express typescript 应用程序部署到 Heroku?
【发布时间】:2021-06-24 21:03:34
【问题描述】:

我在将我的 nodejs 应用程序部署到 Heroku 时遇到问题。 我知道我不能用 index.ts 推送我的应用程序我必须将我的代码解析为 js,所以我编写了命令“postinstall”:“tsc”,但它看起来不起作用
当我尝试将我的应用程序推送到 Heroku 时,我得到了以下信息:

 > server@1.0.0 postinstall /tmp/build_82163065
remote:        > tsc
remote:
remote:        sh: 1: tsc: not found
remote:        npm ERR! code ELIFECYCLE
remote:        npm ERR! syscall spawn
remote:        npm ERR! file sh
remote:        npm ERR! errno ENOENT
remote:        npm ERR! server@1.0.0 postinstall: `tsc`
remote:        npm ERR! spawn ENOENT
remote:        npm ERR!
remote:        npm ERR! Failed at the server@1.0.0 postinstall script.
remote:        npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
remote:        
remote:        npm ERR! A complete log of this run can be found in:
remote:        npm ERR!     /tmp/npmcache.T0pSN/_logs/2021-03-28T16_47_55_171Z-debug.log
remote: 
remote: -----> Build failed
remote:        
remote:        We're sorry this build is failing! You can troubleshoot common issues here:
remote:        https://devcenter.heroku.com/articles/troubleshooting-node-deploys
remote:
remote:        If you're stuck, please submit a ticket so we can help:
remote:        https://help.heroku.com/
remote:
remote:        Love,
remote:        Heroku
remote:
remote:  !     Push rejected, failed to compile Node.js app.
remote:
remote:  !     Push failed

我的 package.json

{
  "name": "server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "engines": {
    "node": "14.15.1"
  },
  "scripts": {
    "start": "node dist/index.js",
    "postinstall": "tsc"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/cors": "^2.8.10",
    "@types/express": "^4.17.11",
    "@types/mongoose": "^5.10.4",
    "@types/node": "^14.14.35",
    "ts-node": "^9.1.1",
    "ts-node-dev": "^1.1.6"
  },
  "dependencies": {
    "@types/dotenv": "^8.2.0",
    "body-parser": "^1.19.0",
    "cors": "^2.8.5",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "mongoose": "^5.12.2"
  }
}

过程文件:

web: node dist/index.js

我已阅读此站点的教程: https://medium.com/developer-rants/deploying-typescript-node-js-applications-to-heroku-81dd75424ce0

【问题讨论】:

    标签: node.js typescript heroku


    【解决方案1】:

    我将 typescript 安装到依赖项并且应用程序已成功上传,但是当我进入我的页面时,我得到应用程序错误:(在 heroku 日志中 --tail 是信息:

    ReferenceError: exports is not defined
    at file:///app/dist/index.js:24:23
    at ModuleJob.run (internal/modules/esm/module_job.js:152:23)
    at async Loader.import (internal/modules/esm/loader.js:166:24)
    at async Object.loadESM (internal/process/esm_loader.js:68:5)
    Process exited with status 1
    State changed from starting to crashed
    at=error code=H10 desc="App crashed" method=GET path="/" host=memories258.herokuapp.com request_id=6fed263e-7425-4c59-ac9a-8ad5f3fdbf88 fwd="193.192.179.52" dyno= connect= service= status=503 bytes= protocol=https
    at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=memories258.herokuapp.com request_id=e45d9b0b-0974-44f5-9e36-c67d8989e446 fwd="193.192.179.52" dyno= connect= service= status=503 bytes= protocol=https
    
    

    【讨论】:

      【解决方案2】:

      现在终于可以工作了。我从 package.json 中删除了 "type": "module",

      【讨论】:

        猜你喜欢
        • 2020-04-22
        • 2017-11-06
        • 1970-01-01
        • 1970-01-01
        • 2017-05-06
        • 2021-09-02
        • 1970-01-01
        • 2017-07-29
        • 2020-02-23
        相关资源
        最近更新 更多