Execute multiple JS file on NPM script NodeJS - node.js

I have a NodeJS app for HTTP call managment
I must execute one JS file (./src/starter.js) before real app starts (./src/boot/index.js)
This is my package.json file:
"engines": {
"node": ">=16.0.0",
"npm": ">=8.0.0"
},
"scripts": {
"dev": "nodemon",
"build": "npm-run-all clean transpile",
"server": "cross-env NODE_ENV=production node --max-old-space-size=8192 ./dist/boot/index.js",
"server:stage": "cross-env NODE_ENV=stage node --max-old-space-size=8192 ./dist/boot/index.js",
"server:test": "cross-env NODE_ENV=test node --max-old-space-size=8192 ./dist/boot/index.js",
"server:production": "cross-env NODE_ENV=production node --max-old-space-size=8192 ./dist/boot/index.js",
"transpile": "babel ./src --out-dir dist",
"clean": "rimraf dist",
"build:css": "node-sass --include-path scss scss/app.scss public/css/app.css --output-style compressed",
"watch:css": "nodemon -e scss -x \"npm run build:css\"",
"watch:dev": "cross-env NODE_ENV=development babel-node --max-old-space-size=8192 ./src/boot/index.js"
},
[...]
And this is my nodemon.json (Nodemon configuration file):
{
"exec": "npm-run-all --parallel watch:dev watch:css",
"watch": [
"src/*",
"public/*",
"scss/*",
"lowdb"
],
"ignore": [
"docker",
"dist"
]
}
How can I run "./src/starter.js" file before the "dev" script (in terminal: npm run dev)?
Thank you

"dev": "./src/starter.js && nodemon",

Related

npm install command keeps repeating while deploying to heroku

I have a project that I want to deploy to heroku. The folder structure is as follows,
Root > client, server
these folders client and server has their own package.json file, so I wrote an installation command like "install" : "npm install --prefix client && npm install --prefix server".
but when I try to deploy it to heroku the build process keeps running npm install in the cli.
heroku cli while building
Root package.json
"scripts": {
"install": "npm install npm install --prefix server && npm install --prefix client",
"build": "npm run build --prefix client",
"start": "npm run server",
"client": "npm start --prefix client",
"server": "npm start --prefix server",
"server-watch": "npm run watch --prefix server",
"watch": "nodemon server.js --prefix src/server",
"test": "echo \"Error: no test specified\" && exit 1"
},
Client package.json
"scripts": {
"start": "react-scripts start",
"build": "BUILD_PATH='../server/public' react-scripts build ",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
Server package.json
"scripts": {
"install": "npm install",
"start": "node src/server.js",
"watch": "nodemon src/server.js",
"test": "jest",
"test-watch": "jest --watch"
},

How to add environment variable to server, run by cypress testing?

My package.json file is
"scripts": {
"start": "concurrently \"nodemon index.js\" \"PORT=3000 react-scripts start\"",
"build": "react-scripts build",
"server": "NODE_ENV=production nodemon index.js",
"dev": "NODE_ENV=development nodemon index.js",
"test": "react-scripts test",
"eslint": "eslint .",
"cypress:open": "cypress open",
"start:test": "NODE_ENV=test concurrently \"NODE_ENV=test nodemon index.js\" \"NODE_ENV=test PORT=3000 react-scripts start\""
},
my creation of router in node app.js is
if (process.env.NODE_ENV === 'test') {
const testingRouter = require('./controllers/testing')
app.use('/api/testing', testingRouter)
}
but when I run cypress test it complains that there is no such router. How can I make NODE_ENV=test while calling npm run cypress:open
command?
And how can I console.log(process.env.NODE_ENV) to see what if it was passed to cypres process?
solved
"start:test": "cross-env NODE_ENV==test concurrently \"cross-env NODE_ENV=test nodemon index.js\" \"cross-env NODE_ENV=test PORT=3000 react-scripts start\""
and
npm run start:test command in one command prompt, and run cypress in another

How to run one 'npm start' for two different folders in my project directory

I would like to run one npm start command to run both my frontend folder and backend folder. Currently, I have to navigate inside each folder and individually run the command on both to see my app open on the localhost. I have looked into the package 'concurrently' but am having some trouble implementing it in my package.json file.
Here is my package.json file for my frontend folder:
"scripts": {
"start": "set HOST = 'http://localhost' && set PORT=8000 && react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"postbuild": "react-snap"
}
I have tried using this bit to run the backend once I run npm start on the frontend however it did not work:
"start": "set HOST = 'http://localhost' && set PORT=8000 && ../backend/ start & react-scripts start"
Edit:
Still having some troubles after reattempting.
Here are my scripts from my package.json files
Frontend folder
"scripts": {
"start": "set HOST = 'http://localhost' && set PORT=8000 && react-scripts start",
"build": "react-scripts build",
"test": "jest",
"eject": "react-scripts eject",
"postbuild": "react-snap"
},
Backend Folder
"scripts": {
"start": "node ./bin/www"
},
The front end is written in React and the backend in Node. Any help would be greatly appreciated!
Install concurrently:
npm i concurrently --save-dev
Now go to your package.json and add this:
"start": "node index.js",
"client": "npm run start --prefix client",
"dev": "concurrently \"npm run start\" \"npm run client\""
(assuming your client folder contains the front-end server)
then, if you wanna run both servers, just type npm run dev and be happy
I was facing the same issue but it is resolved now. Let's say the server is in the 'Backend' folder and the frontend is in the 'client' folder.
then scripts in server-side package.json should be like:
"scripts": {
"client-install": "npm install --prefix client",
"start": "node server.js",
"server": "nodemon server.js",
"client": "cd ../ && npm start --prefix client",
"dev": "concurrently \"npm run server\" \"npm run client\""
},
and client-side package.json is like:
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"proxy": "http://localhost:5000",
then 'npm run dev' runs concurrntly both the server
The 'concurrently' package can do this
npm i concurrently --save-dev
Then set up npm run dev to do
"dev": "concurrently --kill-others \"npm run start-watch\" \"npm run wp-server\""
Another option is to do (Running in Windows CMD):
"dev": "start npm run start-watch && start npm run wp-server"

R10 Boot Timeout error while running app on heroku

I have express.js application, and I want to run command build before application starts, but I am getting this error "Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch".
Here is my scripts in package.json
"scripts": {
"dev": "concurrently \"webpack --config webpack/client.prod.js --progress --watch\" \"webpack --config webpack/server.prod.js --progress --watch\" \"npm start\"",
"real_dev": "webpack-dev-server --config webpack/client.dev.js",
"build": "npm run build:client && npm run build:server",
"build:server": "cross-env NODE_ENV=production webpack --config webpack/server.prod.js --progress",
"build:client": "cross-env NODE_ENV=production webpack --config webpack/client.prod.js --progress",
"start": "cd server && node bin/server.js",
"lint": "eslint --ext .jsx,.js ./"
},
Try to use port from env with process.env.PORT instead of static port

Node Application Heroku Application Error

I am using heroku/nodejs build pack.
this is how my scripts in package.json look like:
"scripts": {
"build": "webpack --config webpack.prod.js",
"server": "http-server public -p 3000 -a localhost -c 0",
"prod": "npm run build && npm run server",
"dev": "webpack-dev-server --config webpack.dev.js",
"lint": "eslint . --fix"
},
In procfile I have only one line of code:
web: npm run prod
Any ideas why am I seeing application error?
In regards to the error, I think that you should be using the environment variable $PORT i.e. http-server public -p $PORT -c 0 Make sure you have http-server in you deps and not your devDeps.
This might not be causing the error but you shouldn't do your build on run. Instead add the build step to a postinstall NPM script like below.
"scripts": {
"build": "webpack --config webpack.prod.js",
"postinstall": "npm run build",
"server": "http-server public -p $PORT -c 0",
"prod": "npm run server",
"dev": "webpack-dev-server --config webpack.dev.js",
"lint": "eslint . --fix"
},
This will ensure that your build gets run on deployment and not every time the node process starts.

Resources