What is the `PM2` for command `yarn run start`? - node.js

I run the nodejs app with yarn run start , what is the command for pm2 I should use?
pm2 yarn run start give me an error.
My package.json content
"scripts": {
"start": "budo main.js:dist/bundle.js --live --host 0.0.0.0",
"watch": "watchify main.js -v --debug -o dist/bundle.js",
"prep": "yarn && mkdirp dist",
"build": "browserify main.js -o dist/bundle.js",
"lint": "eslint main.js --fix",
"deploy": "yarn build && uglifyjs dist/bundle.js -c -m -o dist/bundle.min.js"
},

The error you're getting is because a bash script (yarn) is being executed with node...
Because pm2's default interpreter is set to node.
To run yarn you'll have to set the interpreter to bash:
shell:
Try the command below:
pm2 start yarn --interpreter bash --name api -- start

For me (on ubuntu 20)
pm2 start yarn --name api -- start
would do the trick. With the bash interpreter flag it would error in pm2.

my pm2 version is 5.2.0
pm2 start "yarn start" --name yourProjec

pm2 start yarn --name api -- start
Won't work for me.
It displays below
0|api | /usr/share/yarn/bin/yarn:2
0|api | argv0=$(echo "$0" | sed -e 's,\\,/,g')
0|api | SyntaxError: missing ) after argument list
It means launching a yarn binary file with a node.js
How about this command.
pm2 start "yarn start" --name api
It work's like charm for me.

Related

nodemon not starting when run in kubernetes environment

I am working on containerizing a nodejs app to be running on GKE.
the scripts section of package.json looks like this
"scripts": {
"dev": "npm-run-all --parallel dev:build-server dev:build-client dev:server",
"dev:server": "nodemon -L --watch build --exec node build/bundle.js",
"dev:build-server": "webpack --config webpack.server.js --watch",
"dev:build-client": "webpack --config webpack.client.js --watch"
},
so when running im using npm run dev to start them all.
this works perfectly while it is running on VM.
But when run as a container in kubernetes the nodemon process won't start.
nor it listens on the port. it gives a 502 status error on browser
but when you ssh to the pod and try running the command it starts the process.listens on 3001 port but obviously it gives routes not found on browser. since they are not linked as expected.
below is the dockerfile
FROM node:10.19-stretch
ENV USER="vue" UID="1001"
RUN apt-get update --fix-missing
RUN apt-get install -yq curl
RUN rm /bin/sh && ln -s /bin/bash /bin/sh && \
mkdir -p /opt/vue && \
addgroup --system -gid $UID $USER && \
adduser --system --uid $UID --gid $UID $USER
WORKDIR /opt/vue
COPY dashboard/. /opt/vue/
RUN npm cache clean --force && \
npm install && \
npm cache verify && \
chown $USER.$USER -R /opt/vue
USER $USER
EXPOSE 3001
# ENTRYPOINT ["/usr/bin/dumb-init","--"]
# CMD ["npm run dev"]
CMD ["npm", "run", "dev" ]
tried using base images (node:10.19-stretch,node:10.19.0-alpine3.11)
some had sugested installing inotify but even that didn't work.
what am I missing. please help.
UPDATE ---
when run either in docker or kubernetes standerout log says this
with no errors(enabled verbose output)
[nodemon] Looking in package.json for nodemonConfig
Usage: nodemon [nodemon options] [script.js] [args]
See "nodemon --help" for more.
[nodemon] exiting
after some peer reviewing and posting this on other networks found the correct answer.
issue behind was obvious.
in the scripts section the all dev:* scripts were running parallel. so when the two build steps were running the nodemon service was trying to start the service. and since the build/bundle.json is not there it gives a file not found. but the nodemon thinks of it as invalid arguments and gives the command help.
so what I did was adding the build steps in the docker file by running
npm run dev
and then on the CMD adding the
npm run prod:server
which actually speeded up the startupt time too.
here is the dockerfile part that was changed.
RUN npm cache clean --force && \
npm install && \
npm cache verify && \
chown $USER.$USER -R /opt/vue
USER $USER
EXPOSE 3001
CMD ["npm", "run", "prod:server" ]
also I had removed nodemon as its not needed. since files will not be changed anyway and they are too bulky
"scripts": {
"dev": "npm-run-all --parallel dev:build-server dev:build-client",
"prod:server": "node build/bundle.js",
"dev:build-server": "webpack --config webpack.server.js --watch",
"dev:build-client": "webpack --config webpack.client.js --watch"
},
finally.. !! viola.. it worked.
Thanks everyone who put their precious time on this.

Run node server with PM2

How could I run node server.js -p by pm2?
Scripts of my package.json is like below,
"scripts": {
"dev": "node server.js",
"start": "node server.js -p"
},
When I execute npm start everything work truly. But I want to run this command with pm2.
To do it when I run pm2 start npm -- start, the process will add to the list of the pm2 but my app not run!
the correct command is
pm2 start server.js
or if you want to pass -p to your app and a name
pm2 start server.js --name "my-server" -- -p

How to start a package.json script in pm2

I want to demonise my express js graphql api server. In windows local dev, I can start my server by running this command and it works fine:
yarn dev
This start command is defined in my package.json like this:
"scripts": {
"dev": "cross-env NODE_ENV=development DEBUG=express:* nodemon --exec babel-node src/index.js"
},
When I try to start this in pm2 in my linux server, I get a success like this:
latheesan#app:~/apps/tweet/server$ pm2 start yarn -- dev
[PM2] Starting /usr/bin/yarn in fork_mode (1 instance)
[PM2] Done.
However, when I type pm2 status it says error and also the display looks really odd:
I am running this on Ubuntu 16.04.
If I don't use the pm2 and just start the app in my ubuntu server with yarn dev - it runs fine.
Any ideas?
Pm2 now support npm
$ pm2 start --interpreter babel-node server.js
(or)
$ pm2 start npm --start
(or)
$ pm2 start npm --name "myAPP" --start
(or)
$ pm2 start npm --name "{app_name}" --run "{script_name}"
I have now resolved this issue.
Install the babel-node globally via: npm install -g babel-cli
Then create the pm2 config in json: pm2.json
{
"apps": [
{
"name": "Tweet GraphQL Server",
"exec_interpreter": "babel-node",
"script": "index.js",
"merge_logs": true,
"cwd": "./src",
"env": {
"NODE_ENV": "production"
}
}
]
}
Now I can run this command to start the pm2 process: pm2 start pm2.json
i do this in my app like this:
in package.json:
"scripts": {
"start": "... start application script ...",
"start:dev": "... start application script as development mode ...",
"pm2": "pm2 start npm --name \"CustomeNameForPM2\" -- run start --watch",
"pm2:dev": "pm2 start npm --name \"CustomeNameForPM2\" -- run start:dev --watch"
}
now you can run pm2 easy with npm run pm2 or npm run pm2:dev
but if you like to do something better, you can read pm2 documentation and use pm2 ecosystem config file

Why pm2 NodeJS app starts but not accessible on EC2

I have a Nodejs app running on AWS EC2. My package.json has the following instructions:
"scripts": {
"build": "babel src -s -D -d dist --presets es2015,stage-0",
"start": "node dist/index.js",
"prestart": "npm run build",
..
So when connected to EC2 I (after install and cd to proj folder) I do PORT=8080 npm start The app starts fine - but messages in the console and is acessinble via my EC2 addres :8080. Also if I run PORT=8080 node dist/index.js
- also good.
But since I would like to use monitoring, restarting of the script by pm2 I try do following:
pm2 start dist/index.js -- PORT=8080
or
PORT=8080 pm2 start dist/index.js
I see that pm2 has the app started,
but it's not acessible on AWS address :8080
What do I do wrong?

How-to run a node.js docker instance dropping into a shell that is auto-tailing logs

SO...
I am trying to create an elegant docker / node setup for my team on a greenfield prototype project. My team will need to have Node / NPM and Docker CLI installed beforehand, but afterwards I will be using NPM to manage everything and previously had...
"scripts": {
"docker": "npm run docker-build && npm run docker-start",
"docker-build": "docker build -t docker_foo .",
"docker-start": "docker run -it -p 8080:8080 --rm docker_foo",
"start": "node server.js"
}
...and the Dockerfile contains the CMD...
# Other stuff...
EXPOSE 8080
CMD ["npm", "start"]
...that will eventually start the node server. This works really well for seeing logs and cleaning up containers, but I want to make it better. I would like to instead start the container in the background using the -d option and attach to the container instead with an initial command tailing the logs to simulate the same behavior except that when the user terminates the process, they are still in the container so they can evaluate the container's current state. This led me to have...
"scripts": {
"docker": "npm run docker-build && npm run docker-start && npm run docker-attach",
"docker-build": "docker build -t docker_foo .",
"docker-start": "docker run -d -p 8080:8080 --name docker_foo docker_foo",
"docker-attach": "docker exec -it docker_foo /bin/ash",
"docker-clean": "npm run docker-clean-containers && npm run docker-clean-images",
"docker-clean-containers": "docker ps -a -q | xargs docker rm -f -v",
"docker-clean-images": "docker images -f 'dangling=true' -q | xargs docker rmi",
"start": "node server.js"
}
...but I am having some trouble finding where the node server logs are stored either on the container or on my local host to enable this desired workflow. Is there some way to re-direct the STD out and err to a location inside the container for historical purposes and a way to expand my attach command above to initially be tailing those logs?
What I ended up doing is simply starting the node server in the background...
"scripts": {
"prebuild": "npm run clean",
"build": "docker build -t docker_foo .",
"clean": "npm run clean:containers && npm run clean:images && npm run clean:volumes",
"clean:containers": "docker ps -a -q | xargs docker rm -f -v",
"clean:images": "docker images -f 'dangling=true' -q | xargs docker rmi",
"clean:volumes": "docker volume ls -qf dangling=true | xargs docker volume rm",
"prerun": "npm run build",
"run": "docker run -d -p 8080:8080 --name docker_foo -v $(pwd)/app:/usr/src/docker_foo/app -v $(pwd)/test:/usr/src/docker_foo/test docker_foo",
"start": "nodemon --watch app server.js",
"logs": "docker logs -f admin_api",
"pretest": "npm run build",
"test": "docker run -p 8080:8080 --name docker_foo -it --rm docker_foo node_modules/.bin/istanbul cover node_modules/.bin/_mocha",
"prewatch": "npm run run",
"watch": "docker exec -it docker_foo node_modules/.bin/nodemon --exec \"node_modules/.bin/_mocha -w\""
}
...so I could run "npm run run" and it would run my docker instance in the background with the service filesystem overlayed with my own hosts' being watched by nodemon for file changes for rapid application development and then I could run "npm run logs" afterwards to tail the logs during development.
Alternatively, I can now run "npm run watch" to have the docker image / container / volume cleaned, re-built and ran with nodemon watching to restart the server and mocha watching the app and test directories to re-run the tests and constantly output the results to the window.

Resources