Node.js 如何将nodejs express typescript应用程序部署到Heroku?
我在将nodejs应用程序部署到Heroku时遇到问题。 我知道我不能用index.ts推送我的应用程序,我必须将我的代码解析为js,所以我编写了命令“postinstall”:“tsc”,但它看起来不起作用Node.js 如何将nodejs express typescript应用程序部署到Heroku?,node.js,typescript,heroku,Node.js,Typescript,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
当我尝试将我的应用程序推送到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
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
mypackage.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
我已经阅读了本网站的教程:
我已将typescript安装到dependencies,应用程序已成功上载,但当我进入我的页面时,发现应用程序错误:(在heroku日志中--tail是信息:
> 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
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
我已将typescript安装到dependencies,应用程序已成功上载,但当我进入我的页面时,我得到应用程序错误:(在heroku日志中--tail是信息:
> 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
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”
,现在终于可以工作了。我从package.json中删除了“type”:“module”,
{
"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"
}
}