Heroku 部署 Typescript

Heroku deployment Typescript

我是 运行 一个在本地运行没有任何错误的应用程序,但是当我将它部署到 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]: 

任何人,请告诉我需要更改哪些配置才能正确部署

首先,Heroku 剥离了开发依赖性——这就是您的部署无法正常工作的原因。您可以将所有内容移动到依赖项来解决这个问题——但我真的不推荐这样做。

您应该从 start 命令中删除 nodemon 并使用 node index.js。但要让它工作,你需要将 typescript 编译为 javascript。您可以使用 tsc 命令来完成。

综上所述,您需要:

  1. 创建构建命令,运行 tsc
{
    "scripts": {
        "build": "tsc"
    }
}

You can also use postinstall instead of build, which heroku calls.

  1. 更新启动命令以使用 JS 而不是 TS
{
    "scripts": {
        "start": "node index.js"
    }
}

阅读更多:Deploying Typescript Node.js applications to Heroku