Use of pm2 with npm run - node.js

I havea react frontendapp for which I defined a number of build and run tasks in package.json as shown in the following snippet:
"scripts": {
"start": "env-cmd -f .env.dev react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"build:dev": "env-cmd -f .env.dev npm run build",
"build:test": "env-cmd -f .env.test npm run build",
"build:prep": "env-cmd -f .env.prep npm run build",
"build:prod": "env-cmd -f .env.prod npm run build",
"start-dev-server": "env-cmd -f .env.dev node server/server.js",
"start-test-server": "env-cmd -f .env.test node server/server.js",
"start-prep-server": "env-cmd -f .env.prep node server/server.js",
"start-server": "env-cmd -f .env.prod node server/server.js"
},
My goal is to run the app by using the pm2 tool based on a run configuration which should execute "npm run start-server". This execution should internaly run the last line in the above snippet which is "env-cmd -f .env.prod node server/server.js"
I wrote a pm2 config file, namely my_config.json, for to realizing above sketched scenario as follows:
my_config.json:
{
"apps": [
{
"name": "ReactFrontEndApp",
"script": "npm",
"args": "run start-server"
}
]
}
Finally, I issue the following command in the command console:
pm2 start my_config.json
When above pm2 command is issued my application is listed under pm2 list command output. Nevertheless, the pm2 tool also starts popping up command-consoles one after another without an end (the previos one fades away and then the next one appears). I tested my application at https://localhost:3000 in the browser meanwhile but the browser does not bring anything my app at all (i.e. not found). As a consequence, I have to stop my application by using pm2 stop command.
QUESTION: Is there something I might be missing in the my_config.json file, which might be causing this successive command console opennings? What could be wrong?

I guess that your server fail to load somehow, maybe pm2 change relevant environment variables.
what is the output of pm2 logs 0? what happened when you run npm run start-server manually? what happened when run pm2 start npm -- run start-server manually?

Related

wait-on not moving onto the next command for my electron app using reactJs

I am new to electronjs and I am working on an electron app using ReactJs and I am trying to open the dev server using concurrently and wait-on.
Here is my scripts section of package.json
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"electron:serve": "concurrently -k \"cross-env BROWSER=none npm start\" \"npm run electron:start\"",
"electron:build": "npm run build && electron-builder -c.extraMetadata.main=build/main.js",
"electron:start": "wait-on tcp:3000 && electron .",
},
And when I run npm run electron:serve, the I'm getting this on my terminal
The command doesn't seem to move on to the next section. And when I run electron:start on a new terminal I'm getting this
I had no problem running this on my old laptop two months back and it is not working now. It works fine when I run npm start and electron ..
I've been stuck on this for a while now and any help would be appreciated.
I will make a guess here. According to the discussions on this issue on GitHub, Node 17 introduced a breaking change that might trigger the error you get:
Error connecting to TCP host:localhost port:3000 Error: connect ECONNREFUSED ::1:3000
Adding 127.0.0.1 to your script should work:
"electron:start": "wait-on tcp:127.0.0.1:3000 && electron ."

npm start json-server in background

I'm trying to have my npm start script run json-server in the background in parallel to then running react-scripts to start UI. How would I go about doing this? I've tried various things with single & and prestart, etc but everything results in json-server starting to watch my db file but then react-scripts doesn't run.
The main 2 which I thought should do the trick are below, but neither work.
"scripts": {
"prestart" : "json-server --watch ../sample-db.json --port 3010 --delay 800&",
"start": "set HTTPS=true&&react-scripts start",
and
"scripts": {
"start": "json-server --watch ../sample-db.json --port 3010 --delay 800&set HTTPS=true&&react-scripts start",
Not quite a duplicate, but as indicated by #Max this has a few options:
How can I run multiple npm scripts in parallel?
So here's one way:
"scripts": {
"start": "set HTTPS=true&&react-scripts start",
"mockserver": "json-server --watch ../sample-db.json --port 3010 --delay 800",
"startdev": "concurrently --kill-others \"npm start\" \"npm run mockserver\"",
}
And I see that on windows I need to use "start" which was why single ampersand wasn't working.

nodemon in npm script triggered multiple times

My npm scripts:
"build": "tsc -w -p ./src/server",
"run": "nodemon --watch ./dist/server ./dist/server/app.js",
"start": "concurrently --kill-others \"npm:build\" \"npm:run\""
From VSCode's terminal I can start the app using the start task.
But nodemon starts twice, and restarts multiple times when a file is saved. I assumed it's because the build task hasn't completed yet.
How can I make these work in series, so the one waits for the other? I do not want to use polling.
I'm using Ubuntu 18, node 10.15.0, npm 6.5.0.
Example of my configuration which works well:
package.json:
"start:dev": "nodemon --config nodemon.json ./dist/src/index.js",
nodemon.json:
{
"watch": ["src"],
"ext": "ts",
"ignore": ["src/**/*.spec.ts"],
"exec": "ts-node ./src/index.ts"
}

NPM project.json script section tasks are not executing correctly

This is part of my project.json
"scripts": {
"prestart": "babel-node buildScripts/startMsg.js",
"start": "npm-run-all --parallel security-check open:src lint:watch",
"open:src": "babel-node buildScripts/srcServer.js",
"lint": "esw webpack.config.* src buildScripts --color",
"lint:watch": "npm run lint -- --watch",
"security-check": "nsp check",
"localtunnel": "lt --port 3000",
"share": "npm-run-all --parallel open:src localtunnel"
}
When I run my tasks manually like:
"nsp check" or "npm run lint -- --watch"
it works fine I see output in the console.
Problem is when I'm running my whole app "npm start" or "npm run lint:watch" these tasks are seems not running although I'm not getting any errors in the console and app seems to run fine I'm getting expected content.
What could be the problem here? and what is the difference if run nsp check or npm run security-check shouldn't the same thing happened?
Example when what shows my console when I execute these two above:
console output

Concurrently does not modify NODE_ENV variable

So I'm working on a project using webpack and wanted to create a script on my package.json to run both dev and production mode from there. I'm a windows user and always use Concurrently to run multiple terminal tasks at the same time.
I set my package.json scripts like this:
"scripts": {
"start": "concurrently \"set NODE_ENV=\" \"webpack --watch\"",
"build": "concurrently \"set NODE_ENV=production\" \"webpack\""
},
The output of this in the terminal is:
set NODE_ENV= exited with code 0
Webpack is watching the files…
...
So basically webpack is working properly, but the variable is not being created/deleted. Both commands are failing.
If I run directly
set NODE_ENV=production
it works, so I'm a bit confused...
Any ideas?
Thanks a lot!
Change:
"start": "concurrently \"set NODE_ENV=\" \"webpack --watch\"",
"build": "concurrently \"set NODE_ENV=production\" \"webpack\""
to:
"start": "NODE_ENV= webpack --watch",
"build": "NODE_ENV=production webpack"
You cannot change the environment in one process and expect it to be changed in another started in parallel. You can only change the env of child processes and only on their startup. Child process always inherits the environment from the parent.
If the above doesn't work on Windows then use cross-env:
npm install --save-dev cross-env
and in package.json use:
"start": "cross-env NODE_ENV= webpack --watch",
"build": "cross-env NODE_ENV=production webpack"

Resources