Not able to run typescript node.js app with ts-node with pm2
Asked Answered
C

4

8

I have created a basic node app with typescript top of it. I am using ts-node to do so and it's working totally fine with nodemon. But I need to move it to the server now I am stuck. PM2 is showing error all the time. I have gone through GitHub and other answers on StackOverflow. Nothing helped me here. please help.

I have tried installing typescript and ts-node with PM2. But It did not work for me. I also have tried running file directly, not worked. I am clueless now how should I fix this.

 "scripts": {
    "start": "nodemon -x ts-node src/server.ts"
  },

It works fine with simple npm run start command

madbo@DESKTOP-CS5UFKE MINGW64 /e/shailesh/nodejs/NodeType
$ npm run start

> [email protected] start E:\shailesh\nodejs\NodeType
> nodemon -x ts-node src/server.ts

[nodemon] 1.18.5
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `ts-node src/server.ts`
24 Mar 22:33:23 - listening on port 3000
Mongoose default connection is open to  mongodb://localhost:27017/todo 😍

What I have tried so far that didn't work *( PM2 is globally installed) *

pm2 start ts-node -- --type-check -r tsconfig-paths/register src/server.ts

It gave me this error

madbo@DESKTOP-CS5UFKE MINGW64 /e/shailesh/nodejs/NodeType
$ pm2 start ts-node -- --type-check -r tsconfig-paths/register src/server.ts
[PM2][ERROR] script not found : E:\shailesh\nodejs\NodeType\ts-node
script not found : E:\shailesh\nodejs\NodeType\ts-node
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ App name β”‚ id β”‚ version β”‚ mode β”‚ pid β”‚ status β”‚ restart β”‚ uptime β”‚ cpu β”‚ mem β”‚ user β”‚ watching β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
 Use `pm2 show <id|name>` to get more details about an app

I have also used the following

pm2 start npm  -- ts-node src/server.ts

and got

$ pm2 start npm  -- ts-node src/server.ts
[PM2] Applying action restartProcessId on app [npm](ids: 0)
[PM2] [npm](0) βœ“
[PM2] Process successfully started
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ App name β”‚ id β”‚ version β”‚ mode β”‚ pid   β”‚ status β”‚ restart β”‚ uptime β”‚ cpu β”‚ mem       β”‚ user  β”‚ watching β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ npm      β”‚ 0  β”‚ N/A     β”‚ fork β”‚ 11300 β”‚ online β”‚ 15      β”‚ 0s     β”‚ 0%  β”‚ 21.5 MB   β”‚ madbo β”‚ disabled β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
 Use `pm2 show <id|name>` to get more details about an app

madbo@DESKTOP-CS5UFKE MINGW64 /e/shailesh/nodejs/NodeType
$ pm2 status
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ App name β”‚ id β”‚ version β”‚ mode β”‚ pid β”‚ status  β”‚ restart β”‚ uptime β”‚ cpu β”‚ mem    β”‚ user  β”‚ watching β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ npm      β”‚ 0  β”‚ N/A     β”‚ fork β”‚ 868 β”‚ stopped β”‚ 24      β”‚ 0      β”‚ 0%  β”‚ 0 B    β”‚ madbo β”‚ disabled β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
 Use `pm2 show <id|name>` to get more details about an app

Please help me fixing this

I want it to be working on a server with pm2 on the server. I will be great full if anyone of you can fix my problem. Thanks

Clearstory answered 24/3, 2019 at 17:17 Comment(2)
Did you figure out how to solve this? – Equisetum
Nope, I used the compiled version of my code. and that is something recommended. – Clearstory
O
15

Step 1

create a file run-ts.sh with

ts-node -T index.ts

Step 2

run this command

pm2 start run-ts.sh

And with that you will have run your Typescript application.

Opine answered 27/11, 2019 at 21:6 Comment(1)
tried all sorts of solutions and finally this worked for me on node 18. Thanks! – Slier
E
3

I had same issue and below sweet solution I got, It may help you.

package.json

"scripts": {
      "start:local": "nodemon --exec ts-node local.ts",
      "start": "node index.js"
}

To run npm run start with pm2

pm2 start npm  -- run start

To run npm run start:local with pm2

pm2 start npm  -- run start:local

Note: ts-node and pm2 installed globally

Elevenses answered 2/3, 2023 at 12:21 Comment(0)
G
1

It's very simple guys.

Just use tsc and pm2 watch combination with single & to run both commands.

"scripts": {
    "serve": "tsc src/app.ts -w & pm2 start dist/app.js --watch"
},
Gossoon answered 2/12, 2019 at 11:20 Comment(0)
K
0

Alternatively, you can do this:

"scripts": {
    "start": "pm2 start 'npx ts-node src/server.ts'"
}

Or a slightly fancier, if you need the logging:

"scripts": {
    "start": "pm2 start 'npx ts-node src/server.ts' --log logs.log --time"
}
Knockwurst answered 11/5 at 21:1 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.