Reputation: 529
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?
Upvotes: 1
Views: 12344
Reputation: 36836
Check pm2 logs by command: pm2 logs
If there is no errors, than problem not with your app.
Upvotes: 3
Reputation: 7004
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
.
Upvotes: 1