what would be the right way to run and express generated app with pm2 on linux - pm2

how would be the right way to run an express-generator created with pm2 ?
which would be the right adjustments i should make to run with pm2?
As it says in his documentation I should run the app like :
DEBUG=myapp:* npm start
Thats ok when i run with npm but what should I change to run it with pm2?
in the package.js file lays..
"scripts": {
"start": "node ./bin/www",
"test": "mocha --timeout 10000"
},
I have changed this to like, but is not added in pm2 monit:
"scripts": {
"start": "pm2 start ./bin/www",
"test": "mocha --timeout 10000"
},

Ok I have done like this:
In terminal run the app like
pm2 start ./bin/www
where resides de app.
that way it will run a your app as a "www" and you can monitor with pm2 monit.

Related

Having issues with NPM start throwing [ERR_INVALID_ARG_VALUE] (also having weird pc issues)

i created a new folder since i was having issues and maybe thought it was the directory.
so i did the following steps:
node - v
result: v19.6.0
npx create-react-app test
result: created the proper files.
cd test
result: as expected
npm start
result: Error When i do npm start
i now tried it on a older file that i know used to work before i did npm audit fix --force today and this is what i got: Error
note: i have had multiple issues with my computer it is a M1 macbook pro 16 inch bought brand new from apple in march 2022. i was doing my bootcamp and i kept running into path directory issues for some reason. so i factory reset. i still have some issues and i have to sudo everything.
looking for some help and some guidance thank you.
With everything I tried uninstalling node. Didn't work
I tried
npm audit fix --force
didn't work
what worked was changing my package.json
"scripts": {
"start": "react-scripts --openssl-legacy-provider start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
}
I have added "--openssl-legacy-provider" flag between react-scripts and start

How to start verdaccio by pm2?

pm2 start verdaccio failed, the status is stopped.
$ npm i -g pm2 verdaccio
$ pm2 start verdaccio
I've have been running verdaccio in pm2 for 1 year by know and I use this after installing it globally via npm.
Start
pm2 start `which verdaccio`
Restart
pm2 restart verdaccio
And that works pretty well for me.
The problem is pm2 finding verdaccio in the first place. That's why using which solves it, but that is not always possible in Windows.
I've managed to run verdaccio in pm2 on Windows, without even installing it globally (so that I could commit my configs, plugin packages, etc), with this:
// ecosystem.config.js
module.exports = {
apps : [{
name: 'verdaccio',
script: './node_modules/verdaccio/build/lib/cli', // local package, that pm2 can run with "node <script>"
args : '--config ./config.yaml', // local confis
node_args: '-r esm', // verdaccio's package didn't contain "type": "module"
exec_mode: 'fork' // verdaccio does not currently support PM2's cluster mode
}]
}
PS.: these are my dependencies:
// package.json
"dependencies": {
"esm": "^3.2.25",
"verdaccio": "^5.10.2",
"verdaccio-activedirectory": "^1.0.2",
"verdaccio-simplegroup": "^1.0.2"
},
for someone still stumbling upon this problem. you can try deleting the process then starting it again. I solved it this way.

Why does pm2 start my ghost blog in developement?

I am not able to understand why pm2 starts my ghost blog in developement instead of production.
I can run this
npm start --production and everything is fine like I want it. But if I try to use pm2
pm2 start index.js it starts my blog in developement which I don't want to. I must be blind but can not see in the docs how I can force pm2 to start in production mode.
I only have success starting the app with npm like this:
npm start --production
I tried with a config file ecosystem.config.js and to start it like this:
pm2 start ecosystem.config.js or
pm2 start ecosystem.config.js --env production but it seems to start in developement. Here is my config file.
module.exports = {
apps : [
{
name : "asle",
script : "index.js",
env: {
COMMON_VARIABLE: "true"
},
env_production : {
NODE_ENV: "production"
}
}
]
}
Because ghost blog always runs in development mode by default. If you want to run it with pm2 in production use following command
NODE_ENV=production pm2 start index.js
You can also read in my blog post: https://drifts.io/how-to-setup-ghost-blog-on-vps/#step5installpm2processmanager
Also dont forget to use pm2 startup and save to make sure it persistent over reboots.
Have you tried to create an ecosystem file to declare how you want to launch in production ?
http://pm2.keymetrics.io/docs/usage/application-declaration/
If yes can you show it ?

Watch and reaload for polymer serve / polyserve?

I'm trying to figure out a way to make polyserve / polymer-cli to reload my browser each time a "watched" file changes but I haven't found anything apart of adding Livereload to the HTMLs files that I think is going to be a mess due that when developing web components I load separate HTML files.
For auto reloading using Polymer CLI as a server in your project you should have node / yarn installed. Then you need to install browser-sync locally.
npm install -D browser-sync
or
yarn add browser-sync --dev
Your package.json file will need to look something like:
{
"name": "MY-ELEMENT",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"start": "npm run serve | npm run watch",
"serve": "polymer serve --port 8080",
"test": "polymer test",
"watch": "browser-sync start --proxy localhost:8080 --open --startPath \"components/MY-ELEMENT\" --files \"**/*.html\""
},
"devDependencies": {
"browser-sync": "^2.18.13"
}
}
(If its a regular Polymer app you should remove the --startPath argument as that is for Polymer component development)
I'm using polyserve-watch which watches, serves and reloads webcomponents using polyserve and browser-sync``.
Best regards

Node package.json "bin" value, command not working

I used nodejs on my vserver to make a tiny script to manage users in a db.
In package.json I added the "bin" and set it to my script. My attempt was to make a command available on the whole server so I dont need to go to the directory where the script lies and write "node usermanager.js".
I used npm link and it seemed to work fine:
/home/sl4yer/bin/cl9wnhook -> /home/sl4yer/lib/node_modules/cl9wnhook_usermanager/usermanager.js
/home/sl4yer/lib/node_modules/cl9wnhook_usermanager -> /home/sl4yer/cl9wnHook/usermanager
package.json btw is:
{
"name": "cl9wnhook_usermanager",
"version": "1.0.0",
"description": "User manager for cl9wnHook",
"main": "usermanager.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"bin": {
"cl9wnhook": "./usermanager.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"commander": "^2.9.0",
"js-sha512": "^0.2.2",
"readline-sync": "^1.4.5"
}
}
so using the command "cl9wnhook" should work.
But when I call it, I get:
[sl4yer#lynx usermanager]$ cl9wnhook
: No such file or directory
Any idea?
sudo npm link
I did it, and successfully.
Try adding
#!/usr/bin/env node
on the top of your usermanager.js file. It should work.
Pack the package
npm pack
Then install it globally to run it from any folder.
npm install --global <package_file>.tgz
your bin in your package.json like this:
{
"bin": {
"app": "bin/app"
},
}
but first in your app file you should add at the top this #!/usr/bin/env node
Note
This will help you
Really things can mess up from time to time when working with NPM or Yarn. In these cases first do a complete clean install.
rm -rf ./node_modules
yarn install
npm install
If that doesn't solve your problem then check the bin property in package.json, if it is mapping to the cli file correctly?
You can check what is packed by running yarn pack.