Pm2: Unable to use TypeScript

Created on 1 Mar 2017  路  25Comments  路  Source: Unitech/pm2

Expected behaviour

pm2 start x.ts should work. (Maybe TypeScript should be installed on default so everything works out of the box)

Actual behaviour

I was not able to start a ts file. js works fine.

Both ts-node and index have status "errored"

pm2 list:
bildschirmfoto 2017-03-01 um 16 45 24

I did pm2 install typescript and pm2 install node-ts

Here is the log:
bildschirmfoto 2017-03-01 um 16 47 59

When i try: pm2 install @types/node it says:

Error: Cannot find module '/home/mick/.pm2/node_modules/node/package.json'
    at Function.Module._resolveFilename (module.js:470:15)
    at Function.Module._load (module.js:418:25)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at startModule (/usr/lib/node_modules/pm2/lib/API/Modules/Modularizer.js:33:22)
    at /usr/lib/node_modules/pm2/lib/API/Modules/Modularizer.js:158:9
    at /usr/lib/node_modules/pm2/lib/Configuration.js:64:14
    at FSReqWrap.oncomplete (fs.js:114:15)

Software versions used

OS         : Ubuntu Server 16.04.2 LTS
node.js    : v7.6.0
PM2        : 2.4.2

PM2 Log output

2017-03-01 07:42:02: App [index] with id [1] and pid [50857], exited with code [1] via signal [SIGINT]
2017-03-01 07:42:02: Starting execution sequence in -fork mode- for app name:index id:1
2017-03-01 07:42:02: App name:index id:1 online
2017-03-01 07:42:03: App [index] with id [1] and pid [50889], exited with code [1] via signal [SIGINT]
2017-03-01 07:42:03: Starting execution sequence in -fork mode- for app name:index id:1
2017-03-01 07:42:03: App name:index id:1 online
2017-03-01 07:42:03: App [index] with id [1] and pid [50921], exited with code [1] via signal [SIGINT]
2017-03-01 07:42:03: Starting execution sequence in -fork mode- for app name:index id:1
2017-03-01 07:42:03: App name:index id:1 online
2017-03-01 07:42:04: App [index] with id [1] and pid [50953], exited with code [1] via signal [SIGINT]
2017-03-01 07:42:04: Starting execution sequence in -fork mode- for app name:index id:1
2017-03-01 07:42:04: App name:index id:1 online
2017-03-01 07:42:05: App [index] with id [1] and pid [50985], exited with code [1] via signal [SIGINT]
2017-03-01 07:42:05: Starting execution sequence in -fork mode- for app name:index id:1
2017-03-01 07:42:05: App name:index id:1 online
2017-03-01 07:42:06: App [index] with id [1] and pid [51027], exited with code [1] via signal [SIGINT]
2017-03-01 07:42:06: Starting execution sequence in -fork mode- for app name:index id:1
2017-03-01 07:42:06: App name:index id:1 online
2017-03-01 07:42:07: App [index] with id [1] and pid [51059], exited with code [1] via signal [SIGINT]
2017-03-01 07:42:07: Starting execution sequence in -fork mode- for app name:index id:1
2017-03-01 07:42:07: App name:index id:1 online
2017-03-01 07:42:08: App [index] with id [1] and pid [51091], exited with code [1] via signal [SIGINT]
2017-03-01 07:42:08: Starting execution sequence in -fork mode- for app name:index id:1
2017-03-01 07:42:08: App name:index id:1 online
2017-03-01 07:42:08: App [index] with id [1] and pid [51123], exited with code [1] via signal [SIGINT]
2017-03-01 07:42:08: Starting execution sequence in -fork mode- for app name:index id:1
2017-03-01 07:42:08: App name:index id:1 online
2017-03-01 07:42:09: App [index] with id [1] and pid [51155], exited with code [1] via signal [SIGINT]
2017-03-01 07:42:09: Starting execution sequence in -fork mode- for app name:index id:1
2017-03-01 07:42:09: App name:index id:1 online
2017-03-01 07:42:10: App [index] with id [1] and pid [51187], exited with code [1] via signal [SIGINT]
2017-03-01 07:42:10: Starting execution sequence in -fork mode- for app name:index id:1
2017-03-01 07:42:10: App name:index id:1 online
2017-03-01 07:42:11: App [index] with id [1] and pid [51219], exited with code [1] via signal [SIGINT]
2017-03-01 07:42:11: Starting execution sequence in -fork mode- for app name:index id:1
2017-03-01 07:42:11: App name:index id:1 online
2017-03-01 07:42:11: App [index] with id [1] and pid [51251], exited with code [1] via signal [SIGINT]
2017-03-01 07:42:11: Starting execution sequence in -fork mode- for app name:index id:1
2017-03-01 07:42:11: App name:index id:1 online
2017-03-01 07:42:12: App [index] with id [1] and pid [51283], exited with code [1] via signal [SIGINT]
2017-03-01 07:42:12: Starting execution sequence in -fork mode- for app name:index id:1
2017-03-01 07:42:12: App name:index id:1 online
2017-03-01 07:42:13: App [index] with id [1] and pid [51315], exited with code [1] via signal [SIGINT]
2017-03-01 07:42:13: Starting execution sequence in -fork mode- for app name:index id:1
2017-03-01 07:42:13: App name:index id:1 online
2017-03-01 07:42:14: App [index] with id [1] and pid [51347], exited with code [1] via signal [SIGINT]
2017-03-01 07:42:14: Starting execution sequence in -fork mode- for app name:index id:1
2017-03-01 07:42:14: App name:index id:1 online
2017-03-01 07:42:15: App [index] with id [1] and pid [51379], exited with code [1] via signal [SIGINT]
2017-03-01 07:42:15: Script /home/mick/opnsrc/server/index.ts had too many unstable restarts (16). Stopped. "errored"
In progress

Most helpful comment

Because you need to install the ts-node module to interpret typescript, we made an alias so when you just run pm2 install typescript, it install ts-node where we need it.

All 25 comments

Because you need to install the ts-node module to interpret typescript, we made an alias so when you just run pm2 install typescript, it install ts-node where we need it.

As i described above i did install typescript with pm2 install typescript.

Are you sure that your app is working outside PM2, from your second screen we can see an error : Unable to compile to typescript

Seems like ts-node cant find typescript. Not sure why TypeScript compiler is running anyway, but it cant find the definition files. And as i described above i was not able to install them by pm2 install @types/node

Because they aren't installed at the same place, there is a custom logic for transpiler, is ts-node not standalone anymore ?

I don't know i tried reinstalling everything several times. Can someone reproduce?

This could be a TypeScript or npm issue: TypeScript requires alot of devDependencies e.g. the needed @types/node but none of them gets installed neither global or in pm2.

I tried global node-ts and tsc and both had the same error. I downgraded to nodejs 6.10.0 and it worked. Still the error persists for pm2.

Could you list me exactly what need to be installed (the minimum) to run a typescript file ?

typescript:

  1. npm install -g typescript
  2. tsc filename.ts (converts to js)

ts-node:

  1. npm install -g ts-node
  2. ts-node index.ts

We should be using ts-node (check here and here).
could you check that inside the node_modules of the pm2 installation (check it with which pm2), there is ts-node somewhere ?

Yes it is i checked everything alot of times. It has TypeScript dependency which has @types dependencies which seem not to be installed. Thats why it cant find node functions like require.

I opened an issue here:
https://github.com/Microsoft/TypeScript/issues/14390

I feel like npm is not installing ANY devDependencies. That would explain everything. Not sure if this is a global OS configuration or maybe new node 7 default. Can't find alot of informations tho.

Cumbersome workaround: Go into node_modules/typescript/folder and do npm i -> Installs all devDependencies

If so pm2 should beneath typescript and ts-node install @types/node

Any solution to this?

Our team would like to minimize the number of steps needed to deploy a typescript application. Initially, we thought specifying the script would be enough. We've also tried using ts-node as the interpreter. We've found either approach does not work on Windows using version 2.7.1. It sounds like others are experiencing the same issues. Happy to provide additional details if needed.

Error: spawn C:\Users\...\AppData\Roaming\npm\node_modules\pm2\node_modules\.bin\ts-node ENOENT

I was facing exact same error. Problem was confusion between global pm2 with local pm2 installed in node_modules of my project. So I had to do
./node_modules/.bin/pm2 install typescript and NOT pm2 install typescript to solve it.

Also that involves using ./node_modules/.bin/pm2 start src/server.ts --watch in your start script in package.json.

Close this one cause duplicate of :https://github.com/Unitech/pm2/issues/3312
Feel free to reopen if necessary.

I have the same problem.
error_pm2

Text from log file:

App [index:0] online
2018-12-15T16:48:38: PM2 error: Error: spawn C:Users\sbilyk\AppData\Roaming\npmnode_modules\pm2node_modules.bints-node ENOENT
at Process.ChildProcess._handle.onexit (internal/child_process.js:232:19)
at onErrorNT (internal/child_process.js:407:16)
at process._tickCallback (internal/process/next_tick.js:63:19)

PM2 works as expected with pure js projects, but does not work with typescript. I have tried to install globally and locally typescript and node-ts and other. It just does not work.
What I'm doing wrong?
@wallet77 Can you please reopen this issue?

pm2 install typescript && pm2 install ts-node

@Himself65 I've tried it

@SerhiiBilyk
you can see my repo
https://github.com/Himself65/Astoria

@Himself65 thank you. I think problem exists because I'm using tsconfig-paths/register

  "scripts": {
    "start": "cross-env NODE_ENV=local ts-node -r tsconfig-paths/register index.ts",
    "start:dev": "cross-env NODE_ENV=dev ts-node -r tsconfig-paths/register index.ts",
    "start:prod": "cross-env NODE_ENV=prod ts-node index.ts",
    "test": "jest --detectOpenHandles controllers/",
    "prettier": "prettier --write **/*.ts"
  },

Still have next error:

[PM2] Spawning PM2 daemon with pm2_home=C:Users\sbilyk.pm2
[PM2] PM2 Successfully daemonized
[PM2][Module] Starting NPM module ts-node
[PM2][WARN] Applications bin not running, starting...
[PM2] App [ts-node] launched (1 instances)
[PM2][Module] Starting NPM module node-ts
[PM2][WARN] Applications node-ts not running, starting...
[PM2] App [node-ts] launched (1 instances)
[PM2][ERROR] File ecosystem.config.js not found

This works for me:
$ npm install typescript --ugv
$ npm install ts-node --ugv

I've had no luck with any of the solutions mentioned above. Perhaps this newer issue is why we are seeing this issue again: https://github.com/Unitech/pm2/issues/4142

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"
},
Was this page helpful?
0 / 5 - 0 ratings