R10 Boot Timeout error while running app on heroku - node.js

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

Related

Execute multiple JS file on NPM script NodeJS

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",

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

NPM command for run Node server after webpack finish building

I want to run a command like: npm run start:dev to make my node server runs but after webpack finished building bundles.
My current npm scripts are:
"scripts": {
"start": "node server/server.js",
"start:dev": "npm run build:prod & npm start",
"lint": "eslint *",
"build:dev": "webpack",
"build:prod": "webpack -p --env production",
"dev-server": "webpack-dev-server",
"test": "cross-env NODE_ENV=test jest --config=jest.config.json"
},
The current command will kick both operations together.
Typo: you don't actually want to use &, you want &&:
"start:dev": "npm run build:prod && npm start",
A single ampersand will background the run:build job and cause start to run concurrently.

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.

Webpack dev server start using production.webpack.config.js

I want to start my webpack deb server using my production.webpack.config.js file . please anyone help me
"scripts": {
"start": "webpack-dev-server --progress --inline --hot",
"build": "NODE_ENV=production node_modules/.bin/webpack -p --progress --colors"
},
guys i find my answer and I think this answer help everyone to work multiple configuration file in webpack . in your package.json file add this line
"scripts": {
"start": "webpack-dev-server webpack -p --config webpack.production.config.js --progress --inline --hot",
"build": "NODE_ENV=production node_modules/.bin/webpack -p --config webpack.production.config.js --progress --colors"
},
then problem solved

Resources