Getting errors when trying to start app via yarn & pm2 - node.js

Receiving an error when attempting to start node (next.js) app via yarn and pm2. My google fu didn't turn up anything useful. I suspect the issue is with nvm, though I don't have enough experience to understand how to fix it.
Ubuntu 20.04
nvm 0.38.0
node v14.5.0
yarn 1.22.10
pm2 4.5.6
ecosystem.config.js
module.exports = {
apps: [
{
name: "next",
script: "yarn",
interpreter: "bash",
args: "start:next",
instances: 1,
env: {
NODE_ENV: "development",
},
env_staging: {
NODE_ENV: "production",
},
env_production: {
NODE_ENV: "production",
}
}
]
}
Starting file via:
pm2 start ecosystem.config.js --env staging
or
pm2 start yarn --interpreter bash --name next -- start:next
Receiving errors:
[TAILING] Tailing last 15 lines for [all] processes (change the value with --lines option)
/home/deploy/.pm2/pm2.log last 15 lines:
PM2 | 2021-04-08T18:33:28: PM2 log: App [express:0] online
PM2 | 2021-04-08T18:33:28: PM2 log: App [express:0] exited with code [2] via signal [SIGINT]
PM2 | 2021-04-08T18:33:28: PM2 log: App [express:0] starting in -fork mode-
PM2 | 2021-04-08T18:33:28: PM2 log: App [express:0] online
PM2 | 2021-04-08T18:33:28: PM2 log: App [express:0] exited with code [2] via signal [SIGINT]
PM2 | 2021-04-08T18:33:28: PM2 log: App [express:0] starting in -fork mode-
PM2 | 2021-04-08T18:33:28: PM2 log: App [express:0] online
PM2 | 2021-04-08T18:33:28: PM2 log: App [express:0] exited with code [2] via signal [SIGINT]
PM2 | 2021-04-08T18:33:28: PM2 log: App [express:0] starting in -fork mode-
PM2 | 2021-04-08T18:33:28: PM2 log: App [express:0] online
PM2 | 2021-04-08T18:33:28: PM2 log: App [express:0] exited with code [2] via signal [SIGINT]
PM2 | 2021-04-08T18:33:28: PM2 log: App [express:0] starting in -fork mode-
PM2 | 2021-04-08T18:33:28: PM2 log: App [express:0] online
PM2 | 2021-04-08T18:33:28: PM2 log: App [express:0] exited with code [2] via signal [SIGINT]
PM2 | 2021-04-08T18:33:28: PM2 log: Script /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn had too many unstable restarts (16). Stopped. "errored"
/home/deploy/.pm2/logs/express-out.log last 15 lines:
/home/deploy/.pm2/logs/express-error.log last 15 lines:
0|express | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 3: /bin: Is a directory
0|express | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 4: /bin: Is a directory
0|express | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 5: use strict: command not found
0|express | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 7: var: command not found
0|express | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 8: syntax error near unexpected token `('
0|express | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 8: `var majorVer = parseInt(ver.split('.')[0], 10);'
0|express | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 3: /bin: Is a directory
0|express | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 4: /bin: Is a directory
0|express | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 5: use strict: command not found
0|express | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 7: var: command not found
0|express | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 8: syntax error near unexpected token `('
0|express | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 8: `var majorVer = parseInt(ver.split('.')[0], 10);'
0|express | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 3: /bin: Is a directory
0|express | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 4: /bin: Is a directory
0|express | /home/deploy/.nvm/versions/node/v14.5.0/bin/yarn: line 5: use strict: command not found

--interpreter bash is your problem. yarn is not a bash script, rather what appears to be a JS program. Do this:
pm2 start yarn --name appname -- start:next

I found the only solution was to run it by defining a specific port, in my case 3000 thanks to the comment of Brandon Aguilar. That's the command
pm2 start yarn --name appname -- start -p 3000
It seems that by default it runs it to port 80

Related

PM2 cluster mode multi instance not working (with Next.js server) [duplicate]

Nodejs was running on PM2 for a long time. And there is a corn which clears PM2 logs everyday
0 0 * * * find /home/user/.pm2/logs* -mtime +2 -exec rm -rf {} \;
Below error occurred for 1000 times and then pm2 stopped working and then when I reloaded the instance it was working fine as usual.
What could be the reason for this error?
/home/user/.nvm/versions/node/v12.16.1/lib/node_modules/pm2/lib/ProcessContainer.js:167
throw err;
^
[Error: ENOENT: no such file or directory, open '/home/user/.pm2/logs/out.log'] {
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: '/home/user/.pm2/logs/out.log'
}
2020-09-09T00:55:33: PM2 log: App name:app id:1 disconnected
2020-09-09T00:55:33: PM2 log: App exited with code [1] via signal [SIGINT]
2020-09-09T00:55:33: PM2 log: App starting in -cluster mode-
2020-09-09T00:55:33: PM2 log: App online
/home/user/.nvm/versions/node/v12.16.1/lib/node_modules/pm2/lib/ProcessContainer.js:167
throw err;
^
[Error: ENOENT: no such file or directory, open '/home/user/.pm2/logs/out.log'] {
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: '/home/user/.pm2/logs/out.log'
}
2020-09-09T00:55:33: PM2 log: App name:app id:1 disconnected
2020-09-09T00:55:33: PM2 log: App exited with code [1] via signal [SIGINT]
2020-09-09T00:55:33: PM2 log: App starting in -cluster mode-
2020-09-09T00:55:33: PM2 log: App online
/home/user/.nvm/versions/node/v12.16.1/lib/node_modules/pm2/lib/ProcessContainer.js:167
throw err;
^
[Error: ENOENT: no such file or directory, open '/home/user/.pm2/logs/out.log'] {
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: '/home/user/.pm2/logs/out.log'
}
You're trying to clean up logfiles by just using rm . You Can't Do Thatâ„¢. On Linux and other UNIX-derived OSs, the program writing the log file holds it open even when you rm it from a directory. The file doesn't actually disappear until the program writing it closes it (or terminates). This is true for all sorts of software, not just pm2.
If all you want is to clear out the log files and start over, the command pm2 flush is for you.
If you want to save old logs for a limited period of time, you should investigate pm2's log rotation addon.
Commands like this may do the trick for you
# install the pm2 addon ... notice it says pm2 install, not npm install
pm2 install pm2-logrotate
# rotate when a logfile fills to ten megabytes
pm2 set pm2-logrotate:max_size 10M
# gzip compress the rotated logs to save space
pm2 set pm2-logrotate:compress true
# force rotation at 00:00 each day even if logfile is not full
pm2 set pm2-logrotate:rotateInterval '0 0 * * *'
You can say pm2 config pm2-logrotate to get it to show you its current settings.
Can you try the below two steps and check this if it works.
pm2 start bin/www -i 0 // this will start a cluster and the main app
pm2 stop 0 // this will allow the cluster to keep running
Adding
out_file: "/dev/null",
error_file: "/dev/null"
to the ecosystem.config.json file worked for me. It is basically disabling the logs. This is good for testing if the reason is in the logging functionality.
This is a workaround. If it works the the rotation of logging suggested in the answer of #O.Jones should work too.

PM2 with NodeJs crashing then restart every 1 second

I have a NodeJs+Express+Mongodb app and I'm trying to run it on my vps. There was a crashing problem with mongo so I removed "mongo.conf" file. Usually my app start crashing after few minutes. But after this change, everything was good for 24h then my server crash and restart every 1 second :
pm2.log (loop)
2021-04-25T09:46:58: PM2 log: App [server:1] exited with code [1] via signal [SIGINT]
2021-04-25T09:46:58: PM2 log: App [server:1] starting in -fork mode-
2021-04-25T09:46:58: PM2 log: App [server:1] online
2021-04-25T09:47:00: PM2 log: App [server:1] exited with code [1] via signal [SIGINT]
2021-04-25T09:47:00: PM2 log: App [server:1] starting in -fork mode-
2021-04-25T09:47:00: PM2 log: App [server:1] online
2021-04-25T09:47:02: PM2 log: App [server:1] exited with code [1] via signal [SIGINT]
2021-04-25T09:47:02: PM2 log: App [server:1] starting in -fork mode-
2021-04-25T09:47:02: PM2 log: App [server:1] online
2021-04-25T09:47:34: PM2 log: App [server:1] exited with code [1] via signal [SIGINT]
2021-04-25T09:47:34: PM2 log: App [server:1] starting in -fork mode-
2021-04-25T09:47:34: PM2 log: App [server:1] online
server-error.log (loop)
Deprecation warning: use moment.updateLocale(localeName, config) to change an existing locale. moment.defineLocale(localeName, config) should only be used for creating a new locale See http://momentjs.com/guides/#/warnings/define-locale/ for more info.
(node:1022) Warning: Accessing non-existent property 'MongoError' of module exports inside circular dependency
(Use `node --trace-warnings ...` to show where the warning was created)
TypeError: Cannot read property 'db' of undefined
at /home/debian/app/server.js:987:21
at /home/debian/app/node_modules/mongodb/lib/utils.js:684:9
at /home/debian/app/node_modules/mongodb/lib/mongo_client.js:226:23
at connectCallback (/home/debian/app/node_modules/mongodb/lib/operations/connect.js:366:5)
at /home/debian/app/node_modules/mongodb/lib/operations/connect.js:599:14
at Object.connectHandler [as callback] (/home/debian/app/node_modules/mongodb/lib/core/sdam/topology.js:285:11)
at Timeout._onTimeout (/home/debian/app/node_modules/mongodb/lib/core/sdam/topology.js:443:25)
at listOnTimeout (node:internal/timers:557:17)
at processTimers (node:internal/timers:500:7)
I don't understand what's wrong. Everything works fine on local. Every page works great online and suddenly everything crash and start reboot in loop ?
If that is an environmental variable not seen by your server application, try restart by refreshing with the following
pm2 restart server --update-env

what is the cause of this error message "Error: Cannot find module '../models'" from nodejs+elasrticsearch github repo

I'm trying to setup node.js and Elasticsearch together in a docker container,
I've been following this post to the letter,until to the section of launching the application
when I type
docker-compose up
I keep getting this node.js error that is saying models module is not found
tqd-node | { Error: Cannot find module '../models'
this is the first few lines of the error message
tqd-elasticsearch is up-to-date
Starting tqd-node ... done
Attaching to tqd-elasticsearch, tqd-node
tqd-elasticsearch | WARNING: no logs are available with the 'none' log driver
tqd-node |
tqd-node | > nodejselastic#1.0.0 start /usr/src/app
tqd-node | > pm2-runtime start ./src/main.js --name node_app
tqd-node |
tqd-node | 2019-12-14T23:09:56: PM2 log: Launching in no daemon mode
tqd-node | 2019-12-14T23:09:56: PM2 log: App [node_app:0] starting in -fork mode-
tqd-node | 2019-12-14T23:09:56: PM2 log: App [node_app:0] online
tqd-node | { Error: Cannot find module '../models'
tqd-node | at Function.Module._resolveFilename (internal/modules/cjs/loader.js:582:15)
tqd-node | at Module.Hook._require.Module.require (/usr/local/lib/node_modules/pm2/node_modules/require-in-the-middle/index.js:51:29)
so I downloaded the full code from this GitHub repository(the-quotes-database)
but I still got the same error
I did try to change the path in controller file here
const model = require("../models");
I thought maybe it needs the full c:// path, but that didn't change the error message.
when I do hover over models in VSC editor it shows that it is the correct path.
so my question is : is this a bug in the repo or is the issue with my windows docker configuration?
I successfully managed to create simple python containers with docker,
so I'm not sure what is the root cause of this error and how to fix it.
update: I will answer here in case anyone finds a similar issue.
I installed docker and downloaded the files and repeated the steps on my Ubuntu laptop and it worked!
so the downloaded files were not prepared to work on Windows, it is not a bug in the repo if you have Linux.

Why pm2 logs not showing real time in Node js?

I have tried used pm2 modules and use pm2 logs, early pm2 logs are real time and show me everything console it but from today show only at least 15 lines?( can change this line more or less but not show real time "pm2 logs --lines 1000" show at least 1000 but I want show real time logs )
when run "pm2 logs" showing :
[TAILING] Tailing last 15 lines for [all] processes (change the value with --lines option)
C:\Users\user.pm2\pm2.log last 15 lines:
PM2 | Error while getting network statistics Error: not supported
PM2 | at process.nextTick (D:\wamp64\www...\pm2\node_modules\systeminformation\lib\filesystem.js:418:21)
PM2 | at process._tickCallback (internal/process/next_tick.js:61:11)
PM2 | Error while getting network statistics Error: not supported
PM2 | at process.nextTick (C:\Users\user\AppData\Roaming\npm\node_modules\pm2\node_modules\systeminformation\lib\filesystem.js:418:21)
PM2 | at process._tickCallback (internal/process/next_tick.js:61:11)
PM2 | Error while getting network statistics Error: not supported
PM2 | at process.nextTick (D:\wamp64\www...\pm2\node_modules\systeminformation\lib\filesystem.js:418:21)
PM2 | at process._tickCallback (internal/process/next_tick.js:61:11)
PM2 | Error while getting network statistics Error: not supported
PM2 | at process.nextTick (C:\Users\user\AppData\Roaming\npm\node_modules\pm2\node_modules\systeminformation\lib\filesystem.js:418:21)
PM2 | at process._tickCallback (internal/process/next_tick.js:61:11)
PM2 | Error while getting network statistics Error: not supported
PM2 | at process.nextTick (D:\wamp64\www...\pm2\node_modules\systeminformation\lib\filesystem.js:418:21)
PM2 | at process._tickCallback (internal/process/next_tick.js:61:11)
Why show this error and how resolve it? and why not showing real time logs for me?
all work done :
kill pm2, restart it, update it, update node modules, flush pm2
Thank's

pm2 --ignore-watch not working

I have been using pm2 for some time now. Recently, I needed to add a custom log directory to my Express4 project called "actionLog". Since it is a directory that gets updated with log files and I don't want pm2 to restart the app on log file changes, I wanted pm2 to ignore watching that directory. After updating pm2 to latest, here is the command I used:
pm2 start app.js --watch --ignore-watch="actionLog"
I get the following error streaming through the pm2 logs:
PM2 Error: watch ENOSPC
PM2 at exports._errnoException (util.js:746:11)
PM2 at FSWatcher.start (fs.js:1172:11)
PM2 at Object.fs.watch (fs.js:1198:11)
PM2 at createFsWatchInstance (/usr/local/lib/node_modules/pm2/node_modules/chokidar/lib/nodefs-handler.js:37:15)
PM2 at setFsWatchListener (/usr/local/lib/node_modules/pm2/node_modules/chokidar/lib/nodefs-handler.js:80:15)
PM2 at EventEmitter.NodeFsHandler._watchWithNodeFs (/usr/local/lib/node_modules/pm2/node_modules/chokidar/lib/nodefs-handler.js:228:14)
PM2 at EventEmitter.NodeFsHandler._handleFile (/usr/local/lib/node_modules/pm2/node_modules/chokidar/lib/nodefs-handler.js:255:21)
PM2 at EventEmitter.<anonymous> (/usr/local/lib/node_modules/pm2/node_modules/chokidar/lib/nodefs-handler.js:468:21)
PM2 at FSReqWrap.oncomplete (fs.js:95:15)
PM2 Error: watch ENOSPC
PM2 at exports._errnoException (util.js:746:11)
PM2 at FSWatcher.start (fs.js:1172:11)
PM2 at Object.fs.watch (fs.js:1198:11)
PM2 at createFsWatchInstance (/usr/local/lib/node_modules/pm2/node_modules/chokidar/lib/nodefs-handler.js:37:15)
PM2 at setFsWatchListener (/usr/local/lib/node_modules/pm2/node_modules/chokidar/lib/nodefs-handler.js:80:15)
PM2 at EventEmitter.NodeFsHandler._watchWithNodeFs (/usr/local/lib/node_modules/pm2/node_modules/chokidar/lib/nodefs-handler.js:228:14)
PM2 at EventEmitter.NodeFsHandler._handleFile (/usr/local/lib/node_modules/pm2/node_modules/chokidar/lib/nodefs-handler.js:255:21)
PM2 at EventEmitter.<anonymous> (/usr/local/lib/node_modules/pm2/node_modules/chokidar/lib/nodefs-handler.js:468:21)
PM2 at FSReqWrap.oncomplete (fs.js:95:15)
PM2 Error: watch ENOSPC
I have also tried using the command:
pm2 start bin/www --watch --ignore-watch="actionLog"
This also generated the same error.
Once I have the correct ignore-watch parameter, I will update the json config file I use for starting pm2. At the moment, using this config file with the ignore-watch is also causing errors, but instead of a detailed stack trace as above, I only see the following in the pm2 logs:
PM2: 2016-02-23 04:05:34: Starting execution sequence in -fork mode- for app name:aadm id:2
PM2: 2016-02-23 04:05:34: App name:aadm id:2 online
PM2: 2016-02-23 04:05:35: Change detected on path actionLog/userAction.log for app aadm - restarting
PM2: 2016-02-23 04:05:35: Stopping app:aadm id:2
PM2: 2016-02-23 04:05:35: App name:aadm id:2 exited with code SIGTERM
PM2: 2016-02-23 04:05:35: Process with pid 5102 killed
PM2: 2016-02-23 04:05:35: Starting execution sequence in -fork mode- for app name:aadm id:2
PM2: 2016-02-23 04:05:35: App name:aadm id:2 online
I have looked at some of the reports of ignore-watch problems such as:
https://github.com/Unitech/PM2/issues/1288
https://github.com/Unitech/PM2/issues/918
https://github.com/Unitech/PM2/issues/1275
How to auto reload project nodejs use pm2
Express 4 + pm2 watch not working
Unfortunately, I am still stuck. Any ideas?
You have two options:
Increase the systems inotify max watch limit:
echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p
or
Try to decrease you watch count by additionally ignoring node_modules:
pm2 start bin/www --watch --ignore-watch="actionLog node_modules"
If you don't have sudo rights option 2 is for you.
Turns out even though the errors were triggered by ignore-watch, they were not caused by it. The following command fixed the issue:
echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p
I found this solution here: https://stackoverflow.com/a/32600959/2234029
I also tried "npm dedupe" -as suggested on that thread- which didn't help.
I had the same problem.
It was driving me crazy, then I discovered there were many sessions of PM2 running in my system.
A command like ps aux | grep pm2 shows you all instances running.
So I killed manually each instance (shown by the command above) one by one with:
kill -9 <pid1> <pid2> <pid3> <...>
At last I restarted the process:
pm2 start ecosystem.config.js
and then everything went working smoothly as written in my config file (without any "watch" option).
The actual question is: why those processes where running on my server, but I can't answer this.
I think it is ignore_watch ie with an underline

Resources