0

I'm having this error, when I try to access my api on heroku

I have project with nodejs, typescript, typeORM and using database with Postgresql.

link for my project: https://github.com/WesleyIsr4/tarefas_api/tree/main/tasks_api

2021-03-24T23:42:04.841420+00:00 heroku[web.1]: Starting process with command `npm start`
2021-03-24T23:42:06.805718+00:00 app[web.1]: 
2021-03-24T23:42:06.805731+00:00 app[web.1]: > tasks_api@0.0.1 start /app
2021-03-24T23:42:06.805731+00:00 app[web.1]: > node src/index.ts
2021-03-24T23:42:06.805731+00:00 app[web.1]: 
2021-03-24T23:42:06.871406+00:00 app[web.1]: (node:21) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
2021-03-24T23:42:06.871407+00:00 app[web.1]: (Use `node --trace-warnings ...` to show where the warning was created)
2021-03-24T23:42:06.874166+00:00 app[web.1]: /app/src/index.ts:1
2021-03-24T23:42:06.874166+00:00 app[web.1]: import "reflect-metadata";
2021-03-24T23:42:06.874167+00:00 app[web.1]: ^^^^^^
2021-03-24T23:42:06.874167+00:00 app[web.1]: 
2021-03-24T23:42:06.874168+00:00 app[web.1]: SyntaxError: Cannot use import statement outside a module
2021-03-24T23:42:06.874168+00:00 app[web.1]: at wrapSafe (internal/modules/cjs/loader.js:979:16)
2021-03-24T23:42:06.874169+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:1027:27)
2021-03-24T23:42:06.874169+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
2021-03-24T23:42:06.874170+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:928:32)
2021-03-24T23:42:06.874170+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:769:14)
2021-03-24T23:42:06.874171+00:00 app[web.1]: at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
2021-03-24T23:42:06.874171+00:00 app[web.1]: at internal/main/run_main_module.js:17:47
2021-03-24T23:42:06.889434+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2021-03-24T23:42:06.889834+00:00 app[web.1]: npm ERR! errno 1
2021-03-24T23:42:06.896276+00:00 app[web.1]: npm ERR! tasks_api@0.0.1 start: `node src/index.ts`
2021-03-24T23:42:06.896419+00:00 app[web.1]: npm ERR! Exit status 1
2021-03-24T23:42:06.896567+00:00 app[web.1]: npm ERR!
2021-03-24T23:42:06.896657+00:00 app[web.1]: npm ERR! Failed at the tasks_api@0.0.1 start script.
2021-03-24T23:42:06.896771+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2021-03-24T23:42:06.905764+00:00 app[web.1]: 
2021-03-24T23:42:06.906023+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2021-03-24T23:42:06.906187+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2021-03-24T23_42_06_897Z-debug.log
2021-03-24T23:42:06.952487+00:00 heroku[web.1]: Process exited with status 1
2021-03-24T23:42:07.022497+00:00 heroku[web.1]: State changed from starting to crashed
2021-03-24T23:42:07.716132+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=taskedapi.herokuapp.com request_id=cb3a7f47-103e-408d-874b-c0e3466eead5 fwd="177.37.210.66" dyno= connect= service= status=503 bytes= protocol=https
2021-03-24T23:42:08.350324+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=taskedapi.herokuapp.com request_id=ead9cb26-9b21-4d6e-9630-70b13306d777 fwd="177.37.210.66" dyno= connect= service= status=503 bytes= protocol=https

2 Answers2

0

Node can't run typescript (.ts) files. You're going to need to either:

  • use something like ts-node to compile the files
  • compile your .ts files to .js as part of your deployment steps
Jim
  • 3,476
  • 4
  • 23
  • 33
0

For everybody parachuted here...

In your package.json you have to change your scripts to:

  "scripts": {
    "build": "tsc",
    "start": "node dist/server.js",
    ...other scripts may be put here too
  },

When deployed, heroku will execute build script, transpiling your typescript to javascript, then it will execute start script. Note that start script will run your .js file, not the original .ts.

It is important to say that start script must point to your server.js file found in dist folder(created during deployiment), not to server.ts from your src folder.

Heroku docs say you have to put a main key in package.json too as shown:

  "main": "server.js",
  "license": "MIT",
  "scripts": {
  ...

My answer may be late for the OP, but complement @Jim's answer