簡體   English   中英

如何將 nodejs express typescript 應用程序部署到 Heroku?

[英]How can i deploy nodejs express typescript app to Heroku?

我在將 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

我將 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

終於它現在工作了。 我從 package.json 中刪除了"type": "module",

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM