Heroku 部署 Typescript
Posted
技术标签:
【中文标题】Heroku 部署 Typescript【英文标题】:Heroku deployment Typescript 【发布时间】:2021-02-17 01:59:20 【问题描述】:我正在运行一个应用程序,该应用程序在本地运行时没有任何错误,但是当我在 Heroku 上部署它时,它会部署并最终导致应用程序崩溃。
我的 package.json。
"name": "app-server",
"main": "index.ts",
"scripts":
"start": "nodemon index.ts"
,
"engines":
"node": "10.16.x",
"npm": "6.x"
,
"license": "ISC",
"dependencies":
"@types/mongoose": "^5.7.7",
"@types/node": "^13.9.2",
"@types/react-router-dom": "^5.1.3",
"config": "^3.3.2",
"cors": "^2.8.5",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"moment": "^2.29.1",
"mongodb": "^3.5.5",
"mongoose": "^5.10.9",
"nodemon": "^2.0.5",
"react-router-dom": "^5.1.2",
"socket.io": "^2.3.0",
"ts-node": "^8.10.2",
"typescript": "^3.9.7"
,
"devDependencies":
"@types/config": "0.0.36",
"@types/cors": "^2.8.6",
"@types/express": "^4.17.3",
"@types/jsonwebtoken": "^8.3.9"
我的 tsconfig.json
"compilerOptions":
"target": "es2018",
"module": "commonjs",
"lib": ["es2018", "esnext.asynciterable"],
"skipLibCheck": true,
"strict": true,
"strictFunctionTypes": false,
"strictPropertyInitialization": false,
"esModuleInterop": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true
过程文件
web: npm start
Heroku 的构建日志
Node.js app detected
-----> Creating runtime environment
NPM_CONFIG_LOGLEVEL=error
NODE_ENV=production
NODE_MODULES_CACHE=true
NODE_VERBOSE=false
-----> Installing binaries
engines.node (package.json): 10.16.x
engines.npm (package.json): 6.x
Resolving node version 10.16.x...
Downloading and installing node 10.16.3...
Bootstrapping npm 6.x (replacing 6.9.0)...
npm 6.x installed
-----> Restoring cache
- node_modules
-----> Installing dependencies
Installing node modules
> nodemon@2.0.5 postinstall
> node bin/postinstall || exit 0
-----> Build
-----> Caching build
- node_modules
-----> Pruning devDependencies
removed 14 packages and audited 295 packages in 2.153s
-----> Build succeeded!
-----> Discovering process types
Procfile declares types -> web
-----> Launching...
来自 Heroku 的错误日志
2020-11-04T09:55:14.920223+00:00 app[web.1]: /app/node_modules/ts-node/src/index.ts:434
2020-11-04T09:55:14.920224+00:00 app[web.1]: return new TSError(diagnosticText, diagnosticCodes)
2020-11-04T09:55:14.920225+00:00 app[web.1]: ^
2020-11-04T09:55:14.920420+00:00 app[web.1]: TSError: ⨯ Unable to compile TypeScript:
2020-11-04T09:55:14.920422+00:00 app[web.1]: index.ts(1,21): error TS7016: Could not find a declaration file for module 'express'. '/app/node_modules/express/index.js' implicitly has an 'any' type.
2020-11-04T09:55:14.920422+00:00 app[web.1]: Try `npm install @types/express` if it exists or add a new declaration (.d.ts) file containing `declare module 'express';`
2020-11-04T09:55:14.920423+00:00 app[web.1]: index.ts(8,20): error TS7016: Could not find a declaration file for module 'config'. '/app/node_modules/config/lib/config.js' implicitly has an 'any' type.
2020-11-04T09:55:14.920423+00:00 app[web.1]: Try `npm install @types/config` if it exists or add a new declaration (.d.ts) file containing `declare module 'config';`
2020-11-04T09:55:14.920424+00:00 app[web.1]: index.ts(9,18): error TS7016: Could not find a declaration file for module 'cors'. '/app/node_modules/cors/lib/index.js' implicitly has an 'any' type.
2020-11-04T09:55:14.920424+00:00 app[web.1]: Try `npm install @types/cors` if it exists or add a new declaration (.d.ts) file containing `declare module 'cors';`
2020-11-04T09:55:14.920425+00:00 app[web.1]:
任何人,请告诉我需要更改哪些配置才能正确部署
【问题讨论】:
将 build.log 添加到您的描述中。 【参考方案1】:首先,Heroku 去除了开发依赖——这就是你的部署不起作用的原因。您可以将所有内容移至依赖项来解决这个问题——但我真的不推荐这样做。
您应该从start
命令中删除nodemon
并使用node index.js
。但要使其正常工作,您需要将 typescript 编译为 javascript。您可以使用tsc
命令来完成。
总结一下,你需要:
-
创建一个运行
tsc
的构建命令。
"scripts":
"build": "tsc"
您也可以使用
postinstall
代替 heroku 调用的 build。
-
更新启动命令以使用
JS
而不是TS
。
"scripts":
"start": "node index.js"
阅读更多:Deploying Typescript Node.js applications to Heroku
【讨论】:
以上是关于Heroku 部署 Typescript的主要内容,如果未能解决你的问题,请参考以下文章
MERN 应用程序在部署到 Heroku 时无法运行“heroku-postbuild”脚本
Heroku/Django 部署:为啥我在成功部署和静态收集时收到错误 500?
如何更新使用 GitHub“部署到 Heroku”按钮创建的 Heroku 应用程序?