簡體   English   中英

無法使用 pm2 運行帶有 ts-node 的 typescript node.js 應用程序

[英]Not able to run typescript node.js app with ts-node with pm2

我創建了一個帶有打字稿頂部的基本節點應用程序。 我正在使用 ts-node 這樣做,它與 nodemon 一起工作得很好。 但是現在我卡住了,我需要將它移到服務器上。 PM2一直顯示錯誤。 我已經瀏覽了 GitHub 和 StackOverflow 上的其他答案。 這里沒有任何幫助。 請幫忙。

我試過用 PM2 安裝 typescript 和 ts-node。 但它對我不起作用。 我也試過直接運行文件,沒有用。 我現在不知道該怎么解決這個問題。

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

它適用於簡單的 npm run start 命令

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

> NodeType@1.0.0 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 😍

到目前為止我嘗試過的沒有用*(PM2 是全局安裝的)*

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

它給了我這個錯誤

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

我還使用了以下

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

並得到

$ 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

請幫我解決這個問題

我希望它在服務器上使用 pm2 的服務器上工作。 如果你們中的任何人都可以解決我的問題,我將非常滿意。 謝謝

這是非常簡單的家伙。

只需將tscpm2 watch 與單個&結合使用即可運行這兩個命令。

"scripts": {
    "serve": "tsc src/app.ts -w & pm2 start dist/app.js --watch"
},

第1步

創建一個文件 run-ts.sh

ts-node -T index.ts

第2步

運行這個命令

pm2 start run-ts.sh

這樣你就可以運行你的 Typescript 應用程序了🚀

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM