在heroku上部署打字稿应用程序,执行错误

时间:2019-07-28 06:48:00

标签: node.js typescript heroku

从一开始,我就使用Nodejs / Typescript制作服务器。

我正在尝试部署到heroku,并且看到heroku使用了npm run build命令,因此当我从github推入heroku分支时,应该构建我的打字稿。

  "scripts": {
    "start": "node ./build/index.js",
    "build": "npx tsc",
    "local": "./node_modules/.bin/env-cmd -f ./config/dev.env tsnd --respawn app/main.ts --experimental-modules"
  },

当我部署时,没有任何错误,它已构建,然后服务器启动。但是当我用邮递员执行一些请求时,服务器崩溃了

2019-07-28T06:30:07.845261+00:00 app[api]: Scaled to web@1:Free by user ****@****.com
2019-07-28T06:30:11.641982+00:00 heroku[web.1]: Starting process with command `npm start`
2019-07-28T06:30:12.000000+00:00 app[api]: Build succeeded
2019-07-28T06:30:13.889373+00:00 heroku[web.1]: State changed from starting to crashed
2019-07-28T06:30:13.897111+00:00 heroku[web.1]: State changed from crashed to starting
2019-07-28T06:30:13.700404+00:00 app[web.1]:
2019-07-28T06:30:13.700432+00:00 app[web.1]: > NodeJsTest@1.0.0 start /app
2019-07-28T06:30:13.700434+00:00 app[web.1]: > set NODE_ENV=production && node build/index.js
2019-07-28T06:30:13.700435+00:00 app[web.1]:
2019-07-28T06:30:13.803076+00:00 app[web.1]: internal/modules/cjs/loader.js:638
2019-07-28T06:30:13.803079+00:00 app[web.1]: throw err;
2019-07-28T06:30:13.803081+00:00 app[web.1]: ^
2019-07-28T06:30:13.803082+00:00 app[web.1]:
2019-07-28T06:30:13.803084+00:00 app[web.1]: Error: Cannot find module '/app/build/index.js'

Heroku在app / build内部进行搜索,但是该构建位于根目录上,因此应该就像我在npm start ./build/index.js中传递的内容而不是./app/build/index.js

如何传递绝对路径(我尝试了--prefix)?

编辑1: =>

package.json

  "scripts": {
    "start": "node ../build/index.js",
    "build": "tsc",
    "test": "./node_modules/.bin/env-cmd -f ./config/test.env jest --watch",
    "postinstall": "npm run build",
    "local": "./node_modules/.bin/env-cmd -f ./config/dev.env tsnd --respawn app/main.ts --experimental-modules"
  },

tsconfig

"outDir": "./build",  //which is root/build

日志

2019-07-28T07:21:53.000000+00:00 app[api]: Build started by user *********@ymail.com
2019-07-28T07:22:31.835544+00:00 app[api]: Deploy f0745bf9 by user *********@ymail.com
2019-07-28T07:22:31.835544+00:00 app[api]: Release v15 created by user *********@ymail.com
2019-07-28T07:22:32.307702+00:00 heroku[web.1]: State changed from crashed to starting
2019-07-28T07:22:32.000000+00:00 app[api]: Build succeeded
2019-07-28T07:22:35.686869+00:00 heroku[web.1]: Starting process with command `npm start`
2019-07-28T07:22:37.742582+00:00 heroku[web.1]: State changed from starting to crashed
2019-07-28T07:22:37.568640+00:00 app[web.1]: 
2019-07-28T07:22:37.568660+00:00 app[web.1]: > NodeJsTest@1.0.0 start /app
2019-07-28T07:22:37.568661+00:00 app[web.1]: > node ../build/index.js
2019-07-28T07:22:37.568663+00:00 app[web.1]:
2019-07-28T07:22:37.659479+00:00 app[web.1]: internal/modules/cjs/loader.js:638
2019-07-28T07:22:37.659483+00:00 app[web.1]: throw err;
2019-07-28T07:22:37.659484+00:00 app[web.1]: ^
2019-07-28T07:22:37.659486+00:00 app[web.1]:
2019-07-28T07:22:37.659488+00:00 app[web.1]: Error: Cannot find module '/build/index.js'
2019-07-28T07:22:37.659490+00:00 app[web.1]: at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
2019-07-28T07:22:37.659491+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:562:25)
2019-07-28T07:22:37.659496+00:00 app[web.1]: at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
2019-07-28T07:22:37.659498+00:00 app[web.1]: at startup (internal/bootstrap/node.js:283:19)
2019-07-28T07:22:37.659500+00:00 app[web.1]: at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
2019-07-28T07:22:37.664783+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2019-07-28T07:22:37.665151+00:00 app[web.1]: npm ERR! errno 1
2019-07-28T07:22:37.666157+00:00 app[web.1]: npm ERR! NodeJsTest@1.0.0 start: `node ../build/index.js`
2019-07-28T07:22:37.666264+00:00 app[web.1]: npm ERR! Exit status 1
2019-07-28T07:22:37.666520+00:00 app[web.1]: npm ERR!
2019-07-28T07:22:37.666699+00:00 app[web.1]: npm ERR! Failed at the NodeJsTest@1.0.0 start script.
2019-07-28T07:22:37.666855+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2019-07-28T07:22:37.671035+00:00 app[web.1]:
2019-07-28T07:22:37.671225+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2019-07-28T07:22:37.671348+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2019-07-28T07_22_37_667Z-debug.log
2019-07-28T07:22:37.723807+00:00 heroku[web.1]: Process exited with status 1
2019-07-28T07:22:39.173088+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=POST path="/%20/user/signup" host=nodejstest-cg.herokuapp.com request_id=c2a706e6-c1bf-420e-ab0b-19b659382a65 fwd="222.224.213.226" dyno= connect= service= status=503 bytes= protocol=https
2019-07-28T07:31:43.547336+00:00 heroku[web.1]: State changed from crashed to starting
2019-07-28T07:31:46.909924+00:00 heroku[web.1]: Starting process with command `npm start`
2019-07-28T07:31:49.187641+00:00 heroku[web.1]: State changed from starting to crashed
2019-07-28T07:31:49.015302+00:00 app[web.1]:
2019-07-28T07:31:49.015323+00:00 app[web.1]: > NodeJsTest@1.0.0 start /app
2019-07-28T07:31:49.015326+00:00 app[web.1]: > node ../build/index.js
2019-07-28T07:53:09.288766+00:00 app[web.1]: npm ERR! errno 1
2019-07-28T07:53:09.289903+00:00 app[web.1]: npm ERR! NodeJsTest@1.0.0 start: `node ../build/index.js`
2019-07-28T07:53:09.290066+00:00 app[web.1]: npm ERR! Exit status 1
2019-07-28T07:53:09.290318+00:00 app[web.1]: npm ERR!
2019-07-28T07:53:09.290501+00:00 app[web.1]: npm ERR! Failed at the NodeJsTest@1.0.0 start script.
2019-07-28T07:53:09.290663+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2019-07-28T07:53:09.308332+00:00 app[web.1]:
2019-07-28T07:53:09.308545+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2019-07-28T07:53:09.308683+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2019-07-28T07_53_09_291Z-debug.log
2019-07-28T07:53:09.362302+00:00 heroku[web.1]: Process exited with status 1

编辑2 =>

package.json

  "scripts": {
    "start": "node ./build/index.js",
    "build": "tsc",
    "test": "./node_modules/.bin/env-cmd -f ./config/test.env jest --watch",
    "postinstall": "npm run build",
    "local": "./node_modules/.bin/env-cmd -f ./config/dev.env tsnd --respawn app/main.ts --experimental-modules"
  },

日志:

2019-07-28T09:02:50.000000+00:00 app[api]: Build started by user **********@ymail.com
2019-07-28T09:04:05.823031+00:00 heroku[web.1]: State changed from crashed to starting
2019-07-28T09:04:05.656792+00:00 app[api]: Deploy 2df838c6 by user **********@ymail.com
2019-07-28T09:04:05.656792+00:00 app[api]: Release v17 created by user **********@ymail.com
2019-07-28T09:04:05.000000+00:00 app[api]: Build succeeded
2019-07-28T09:04:09.723166+00:00 heroku[web.1]: Starting process with command `npm start`
2019-07-28T09:04:12.659767+00:00 heroku[web.1]: Process exited with status 1
2019-07-28T09:04:12.676681+00:00 heroku[web.1]: State changed from starting to crashed
2019-07-28T09:04:12.436114+00:00 app[web.1]:
2019-07-28T09:04:12.436142+00:00 app[web.1]: > NodeJsTest@1.0.0 start /app
2019-07-28T09:04:12.436144+00:00 app[web.1]: > node ./build/index.js
2019-07-28T09:04:12.436146+00:00 app[web.1]:
2019-07-28T09:04:12.582525+00:00 app[web.1]: internal/modules/cjs/loader.js:638
2019-07-28T09:04:12.582529+00:00 app[web.1]: throw err;
2019-07-28T09:04:12.582530+00:00 app[web.1]: ^
2019-07-28T09:04:12.582532+00:00 app[web.1]:
2019-07-28T09:04:12.582534+00:00 app[web.1]: Error: Cannot find module '/app/build/index.js'
2019-07-28T09:04:12.582539+00:00 app[web.1]: at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
2019-07-28T09:04:12.582542+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:562:25)
2019-07-28T09:04:12.582543+00:00 app[web.1]: at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
2019-07-28T09:04:12.582546+00:00 app[web.1]: at startup (internal/bootstrap/node.js:283:19)
2019-07-28T09:04:12.582548+00:00 app[web.1]: at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
2019-07-28T09:04:12.592947+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2019-07-28T09:04:12.593499+00:00 app[web.1]: npm ERR! errno 1
2019-07-28T09:04:12.594999+00:00 app[web.1]: npm ERR! NodeJsTest@1.0.0 start: `node ./build/index.js`
2019-07-28T09:04:12.595105+00:00 app[web.1]: npm ERR! Exit status 1
2019-07-28T09:04:12.595301+00:00 app[web.1]: npm ERR!
2019-07-28T09:04:12.595420+00:00 app[web.1]: npm ERR! Failed at the NodeJsTest@1.0.0 start script.
2019-07-28T09:04:12.595588+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2019-07-28T09:04:12.603105+00:00 app[web.1]:
2019-07-28T09:04:12.603332+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2019-07-28T09:04:12.603425+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2019-07-28T09_04_12_596Z-debug.log
2019-07-28T09:04:13.058556+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=POST path="/%20/user/signup" host=nodejstest-cg.herokuapp.com request_id=c9a53a6d-a237-4da9-9439-697758c980cb fwd="222.224.213.226" dyno= connect= service= status=503 bytes= protocol=https

1 个答案:

答案 0 :(得分:0)

当我使用TypeScript部署React项目时遇到了一些问题。因此,我找到了一种解决问题的方法是使用Webpack。 Webpack将从您的Typescript文件中创建一个main.js文件,因此Heroku可以知道您部署了什么。

您可以在此处找到更多信息:enter link description here