1

I'm new to node.js and having difficulty to demonize this app using pm2. The app works fine when I go to /srv/myapp and run yarn start but when I try to demonize it by going to /srv/myapp and run pm2 start index.js

I see that the process seem to have started:

[PM2] Starting /srv/myapp/index.js in fork_mode (1 instance)
[PM2] Done.
┌──────────┬────┬──────┬───────┬────────┬─────────┬────────┬─────┬───────────┬──────┬──────────┐
│ App name │ id │ mode │ pid   │ status │ restart │ uptime │ cpu │ mem       │ user │ watching │
├──────────┼────┼──────┼───────┼────────┼─────────┼────────┼─────┼───────────┼──────┼──────────┤
│ index    │ 0  │ fork │ 14528 │ online │ 0       │ 0s     │ 0%  │ 16.0 MB   │ root │ disabled │
└──────────┴────┴──────┴───────┴────────┴─────────┴────────┴─────┴───────────┴──────┴──────────┘
 Use `pm2 show <id|name>` to get more details about an app

But the front end does not respond (instead I get 502 errors and the node server does not listen on port 3000 as expected (fuser 3000/tcp returns no results).

What could be wrong here? How can I fix it?

narad
  • 499
  • 3
  • 6
  • 11
  • Does it work if you run "node index.js"? – Joe May 14 '18 at 15:44
  • No, `node index.js` causes `import bb from 'bluebird';SyntaxError: Unexpected token import ` – narad May 14 '18 at 15:47
  • I suspect that's the root of your problem. Are you using babel or anything? I couldn't find any documentation on `yarn start`, do you know if it's running a custom script to transpile ES6 javascript to ES5 that Node can then run? – Joe May 14 '18 at 15:54
  • Possible duplicate of [Can pm2 run an 'npm start' script](https://stackoverflow.com/questions/31579509/can-pm2-run-an-npm-start-script) – sdgluck May 14 '18 at 16:06
  • (`yarn start` is not the same as `pm2 start`) – sdgluck May 14 '18 at 16:07
  • @sdgluck good point. `pm2 start npm -- start` works for me! – narad May 14 '18 at 16:30

2 Answers2

2

Check pm2 logs by command: pm2 logs If there is no errors, than problem not with your app.

ZiiMakc
  • 31,187
  • 24
  • 65
  • 105
1

pm2 starts your app with node, one problem is it doesn't provide any logging or output like starting an app by other means (such as node index.js does), so it doesn't really let you know if there is anything going wrong.

I've always found it's a good idea to test your app by starting it with node before pm2.

Given node index.js causes the error: import bb from 'bluebird';SyntaxError: Unexpected token import, which makes it sound like you're using ES6 import feature, not supported by Node by default. I suspect Yarn is either enabling these features, or transpiling your ES6 code to ES5 for Node to run when you do the yard start, but I'm not familiar with Yarn to know.

It may be you're pointing at the wrong file, and there's a transpiled file dist.js (as an example) already created that Yarn is happily running with the opaque start command.


Edit: After looking at the source, it looks to be using something called babel-node. See the start script in the package.json:

"scripts": {
  "start": "babel-node index.js",

pm2 doesn't know to run your index.js with this babel-node thing, and even if it did babel-node strongly recommends you do not use babel-node in production (which presumably you are doing with pm2):

Not meant for production use You should not be using babel-node in production. It is unnecessarily heavy, with high memory usage due to the cache being stored in memory. You will also always experience a startup performance penalty as the entire app needs to be compiled on the fly.

Check out the example Node.js server with Babel for an idea of how to use Babel in a production deployment.

And there's also a note about ES6 module loading:

ES6-style module-loading may not function as expected Due to technical limitations ES6-style module-loading is not fully supported in >a babel-node REPL.

I'd strongly recommend you add a build script to produce a 'distribution' transpiled version with babel and deploy that!

"scripts": {
  "start": "babel-node index.js",
  "build": "babel index.js --out-file index-dist.js

Or something along those lines, and run yarn build (or yarn run build?) then pm2 start index-dist.js.

Joe
  • 6,773
  • 2
  • 47
  • 81
  • Yes, the app uses ES6 and babel. But the app does also run using `node start`. You can see the docs here: https://github.com/FreeFeed/freefeed-server . What do you suggest to demonize the app, if pm2 is not suitable here? – narad May 14 '18 at 16:03
  • Also `node start pm2` yeilds: `module.js:540 throw err; Error: Cannot find module '/srv/myapp/pm2'` – narad May 14 '18 at 16:07
  • `node start pm2` doesn't really make sense as there's no pm2 javascript file in your app to run. `pm2` is a tool to run js like node is (and uses node). Using `node` to run `pm2` doesn't really work. See my edit. – Joe May 14 '18 at 16:16
  • Well, I add `"build": "babel index.js --out-file index-dist.js` to package.json scripts and now `yarn start index-dist.js` does start the app but `pm2 start index-dist.js` results in `script not found : /srv/myapp/index-dist.js`. How can I create that? – narad May 14 '18 at 16:26
  • Is there an `index-dist.js` file created if you poke around the file system? It might be created in the root directory or something. – Joe May 14 '18 at 16:35
  • Oh, I'm so sorry, I had not run `yarn run build`. But now when I run `pm2 start index-dist.js` the process starts but I still get 502 in browser. – narad May 14 '18 at 16:47
  • What happens when you run `node index-dist.js`, it should display any errors that occur when you try connect with the browser. – Joe May 15 '18 at 08:40
  • I get `/srv/myapp/app/app.js:2 import http from 'http'; ^^^^^^ SyntaxError: Unexpected token import` – narad May 15 '18 at 12:16