NodeJS and Webpack on Heroku - node.js

I am simply trying to deploy a very straight forward nodejs server and webpack application to heroku but i get the following error from Heroku:
2018-03-26T12:02:19.952603+00:00 app[web.1]: webpack: Compiled successfully.
2018-03-26T11:52:19.406889+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2018-03-26T11:52:19.406889+00:00 heroku[web.1]: Stopping process with SIGKILL
2018-03-26T11:52:19.486385+00:00 heroku[web.1]: Process exited with status 137
2018-03-26T11:52:19.497230+00:00 heroku[web.1]: State changed from starting to crashed
Seems like there is something wrong with the port but i am not sure what else i could do. Here is what my node looks like:
/* jshint esversion: 6 */
var express = require('express');
var path = require('path');
var serveStatic = require('serve-static');
app = express();
app.use(serveStatic(__dirname + "/dist"));
var port = process.env.PORT || 5000;
app.listen(port);
and my package.json:
{
"name": "xxx",
"version": "1.0.0",
"description": "xxxx",
"main": "index.html",
"scripts": {
"start": "npm run serve",
"serve": "webpack-dev-server --colors --watch --inline --progress --content-base /src --config config/webpack.dev.js",
"test:js": "eslint src/js/**",
"test:scss": "stylelint src/scss/**/*.scss",
"test": "npm run test:js && npm run test:scss",
"build": "npm run build:dev",
"build:dev": "ENV=dev webpack --config config/webpack.dev.js",
"build:prod": "npm run clean && ENV=prod webpack --config config/webpack.prod.js",
"clean": "rimraf -- dist",
"clean:all": "rimraf -- node_modules dist"
},
"license": "UNLICENSED",
"repository": {
"type": "git",
"url": "git+https://github.com/xxx/xxxx.git"
},
"keywords": [
"design",
"site",
"front-end"
],
"author": "xxxx",
"dependencies": {
...
},
"devDependencies": {
...
}
}

Related

yarn watch dev not compiling assets with error The toast encountered an error. Please make sure that the app id is set correctly, Windows 8.1

Upon running npm install --global yarn command, npm installs correctly. However, on running the yarn watch command assets give an error.
The following message is been displayed
I have tried to reinstall and install Laravel, but the same issue persists. windows 8.1
All the notifications settings are turned on
PS D:\Coding\pizza> yarn watch
yarn run v1.22.19
$ npx mix watch
● Mix █████████████████████████ emitting (95%)
emit
● Mix █████████████████████████ done (99%) plugins
WebpackBar:done
✔ Mix
Compiled successfully in 1.82s
Laravel Mix v6.0.49
✔ Compiled Successfully in 1674ms
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┬───────────┐
│ File │ Size │
├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼───────────┤
│ /public/js/app.js │ 6.42 KiB │
│ public/css/app.css │ 284 bytes │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┴───────────┘
● Mix █████████████████████████ (99%)
webpack compiled successfully
The toast encountered an error.
Please make sure that the app id is set correctly.
Command Line: D:\Coding\pizza\node_modules\node-notifier\vendor\snoreToast\snoretoast-x64.exe -appID "Laravel Mix" -pipeName \\.\pipe\notifierPipe-448b4cdb-0a86-4988-8e64-fdd29d2a12d4 -p D:\Coding\pizza\node_modules\laravel-mix\icons\laravel.png -m "Build successful" -t "Laravel Mix"
Server.js
const express = require('express')
const app = express()
const ejs = require('ejs')
const path = require('path')
const expressLayout = require ('express-ejs-layouts')
const PORT = process.env.PORT || 3300
//assests
app.use(express.static('public'))
app.get('/', (req, res)=> {
res.render('home.ejs')
})
//set template engine
app.use(expressLayout)
app.set('views',path.join(__dirname, '/resources/views'))
app.set('view-engine','ejs')
app.listen(PORT, () => {
console.log(`Listening on port ${PORT}`)
})
package.json
{
"name": "pizza",
"version": "1.0.0",
"description": "This is comprensive assesment",
"main": "index.js",
"author": "Sundaram",
"license": "MIT",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon server.js",
"serve": "node server.js",
"development": "mix",
"watch": "npx mix watch",
"watch-poll": "mix watch -- --watch-options-poll=1000",
"hot": "mix watch --hot",
"production": "mix --production"
},
"dependencies": {
"cross-env": "^7.0.3",
"ejs": "^3.1.8",
"express": "^4.18.2",
"express-ejs-layouts": "^2.5.1",
"install": "^0.13.0",
"tailwindcss": "^3.2.2"
},
"devDependencies": {
"laravel-mix": "^6.0.0-beta.17",
"nodemon": "^2.0.20",
"resolve-url-loader": "^3.1.0",
"sass": "^1.56.0",
"sass-loader": "^8.0.2"
}
}
output of the file should be "Compiled succesfully without any error"
how to over this error and compile succesfully this .

Error deploying Mern Stack application on Heroku

I am trying to deploy a full-stack app on heroku, i have tried couple of things but none of them worked... when i entered "heroku logs" to follow up with the errors it should me code: 'MODULE_NOT_FOUND even though the node_module's can be found in both the Root of the project and the Frontend file as well , how do i fix this error and where does it come from ?
Here's a picture of my projects structure:
server.js:
require('dotenv').config();
const path = require('path');
const express = require("express");
const productRoutes = require("./routes/productRoutes");
const connectDB = require("./config/db");
connectDB();
const app = express();
app.use(express.json());
//--------------- deployment -------
app.use("/api/products", productRoutes);
if(process.env.NODE_ENV === "production") {
app.use(express.static(path.join('../frontend/build')));
app.get('*',(req,res) => {
res.sendFile(path.resolve(__dirname,'../frontend/build','index.html'))
})
} else {
app.get("/", (req, res) => {
res.send("Api running");
})
}
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
.env:
PORT=8080
MONGO_URI=*****
NODE_ENV=production
Package.json:
{
"name": "mern-shopping-build",
"version": "1.0.0",
"description": "Shopping list built with MERN stack",
"main": "server.js",
"engines": {
"node": "16.4.1",
"npm": "7.18.1"
},
"scripts": {
"build": "cd frontend && npm run dev",
"install-client": "cd frontend && npm install",
"heroku-postbuild": "npm run install-client && npm run build",
"start": "node server.js",
"server": "nodemon backend/server.js",
"client": "npm start && cd frontend",
"dev": "concurrently --kill-others-on-fail \"npm run server\" \"npm start --prefix frontend\""
},
"keywords": [],
"author": "Karim ",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^10.0.0",
"express": "^4.17.1",
"mongoose": "^5.13.2"
},
"devDependencies": {
"concurrently": "^6.2.0",
"nodemon": "^2.0.9"
}
}
ProcFile:
web node server.js
I Would appreciate any kind of feedback or help, Thank you!
A Procfile is not technically required to deploy simple apps written in most Heroku-supported languages—the platform automatically detects the language and creates a default web process type to boot the application server.
Just make sure to point your root package.json main key to backend/server.js.
Most likely it would work otherwise share your Build Logs to check it further.
{
"main": "backend/server.js"
}

Unable to start node application inside docker

While creating my node app inside docker I am getting below error
docker container run -it --rm --volume $(pwd):/usr/src/app -p 7007:3000 sample-app-dev:latest
docker: Error response from daemon: OCI runtime create failed: container_linux.go:344: starting container process caused "exec: \"nodemon server.js\": executable f
My Dockerfile looks like below
FROM node:12
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
ENV PATH="/usr/src/app:${PATH}"
ENTRYPOINT ["nodemon server.js"]
Package.json
{
"name": "nodedocker",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"nodemon": "^2.0.4"
}
}
server.js
const express = require( "express" );
const app = express();
const port = 3000;
app.get( "/", ( req, res ) => {
res.send( "Hello World from express JS!!" );
} );
const hobbies = [
"Swimming", "Diving", "Jogging", "Cooking", "Singing" ] ;
app.get("/hobbies", (req, res) => {
res.send(hobbies);
});
const famous_programming = [
"python", "java", "c", "c++", "JS" ] ;
app.get("/famous_programming", (req, res) => {
message = famous_programming.includes("node")? "Yaaay, Node.js" : "Yup! Node.js is a framework from JS" ;
res.send(message);
});
app.listen( port, () => {
console.log( `Node JS started on http://localhost:${port}` )
} );
I am not sure what else I am missing here, any inputs greatly appreciated.
Thank you.
You could configure the package to run nodemon server.js on start, then specify npm start in the entrypoint:
Dockerfile:
FROM node:12
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
ENV PATH="/usr/src/app:${PATH}"
ENTRYPOINT ["npm", "start"]
package.json:
{
"name": "nodedocker",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"nodemon": "^2.0.4"
}
}
Test:
$ docker container run -it --rm q63534772
> nodedocker#1.0.0 start /usr/src/app
> nodemon server.js
[nodemon] 2.0.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node server.js`
Node JS started on http://localhost:3000

npm start does nothing while the command works properly when using NodeJS with Express

UPDATE:
I had nvm for windows installed and forgot about it. It was projecting the contents of its node installation to the program files folder.
ORIGINAL:
So I have a very simple project copied from freecodecamp
index.js
const express = require('express');
const app = express();
app.get('/', (req, res) => res.send('Hello World!'));
app.listen(3000, () => console.log('Example app listening on port 3000!'));
package.json
{
"name": "test-app", "version": "1.0.0", "description": "", "main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
},
"keywords": [], "author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
}
}
Running node index.js starts the server and operates as expected, but if I run npm start it just returns:
> test-app#1.0.0 start D:\temp\test-app
> node index.js
Microsoft Windows [Version 10.0.17134.885]
(c) 2018 Microsoft Corporation. All rights reserved.
and exits out.
I would expect that npm start does the same thing as node index.js since it uses the same command, as described here.
What did I do wrong?
I have tried:
running npm cache clean --force
removing node_modules and running npm install
removing all traces of node and reinstalling a different version
EDIT: project structure looks like
D:\temp\test-app
├── node_modules
├── index.js
├── package-lock.json
└── package.json
Just rename your index.js file to server.js and remove the "start": script entirely. Then npm start will work normally.
{
"name": "test-app", "version": "1.0.0", "description": "", "main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [], "author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
}
}
Please try with npm run start instead of npm start in the same directory as your package.json

Heroku keep crashing after Starting process with command `npm start'

I am newbie. I am following Tutorial from https://www.sitepoint.com/building-facebook-chat-bot-node-heroku/ to make sample facebook messenger bot.
Just when I deployed it first time in heroku I got error
Here is my package.json
{
"name": "spbot",
"version": "1.0.0",
"description": "Testbot",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node app.js"
},
"author": "Munkh",
"license": "ISC",
"dependencies": {
"body-parser": "^1.17.1",
"express": "^4.15.2",
"mongoose": "^4.9.1",
"request": "^2.81.0"
}
}
here is app.js
var express = require("express");
var request = require("request");
var bodyParser = require("body-parser");
var app = express();
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.listen((process.env.PORT || 5000));
// Server index page
app.get("/", function (req, res) {
res.send("Deployed!");
});
// Facebook Webhook
// Used for verification
app.get("/webhook", function (req, res) {
if (req.query["hub.verify_token"] === "this_is_my_token") {
console.log("Verified webhook");
res.status(200).send(req.query["hub.challenge"]);
} else {
console.error("Verification failed. The tokens do not match.");
res.sendStatus(403);
}
});
I got error like this
2017-03-20T02:11:15.997279+00:00 heroku[web.1]: State changed from crashed to starting
2017-03-20T02:11:17.849748+00:00 heroku[web.1]: Starting process with command `npm start`
2017-03-20T02:11:20.455381+00:00 app[web.1]:
2017-03-20T02:11:20.455396+00:00 app[web.1]: > spbot#1.0.0 start /app
2017-03-20T02:11:20.455397+00:00 app[web.1]: > node app.js
2017-03-20T02:11:20.455397+00:00 app[web.1]:
2017-03-20T02:11:20.978077+00:00 heroku[web.1]: Process exited with status 0
2017-03-20T02:11:20.994169+00:00 heroku[web.1]: State changed from starting to crashed
2017-03-20T02:11:20.994986+00:00 heroku[web.1]: State changed from crashed to starting
2017-03-20T02:11:23.337790+00:00 heroku[web.1]: Starting process with command `npm start`
2017-03-20T02:11:27.660508+00:00 app[web.1]:
2017-03-20T02:11:27.660527+00:00 app[web.1]: > node app.js
2017-03-20T02:11:27.660526+00:00 app[web.1]: > spbot#1.0.0 start /app
2017-03-20T02:11:27.660528+00:00 app[web.1]:
2017-03-20T02:11:27.896343+00:00 heroku[web.1]: State changed from starting to crashed
2017-03-20T02:11:27.881546+00:00 heroku[web.1]: Process exited with status 0
Because error doesn't specify what went wrong and just keep crashing, I can't find a solution. My apologies if it is simple question.
You have to specify the node.js and npm version which you are using in package.json, as mentioned below.
Specify the versions which you are using in your development.
{
"name": "spbot",
"version": "1.0.0",
"description": "Testbot",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node app.js"
},
"author": "Munkh",
"license": "ISC",
"dependencies": {
"body-parser": "^1.17.1",
"express": "^4.15.2",
"mongoose": "^4.9.1",
"request": "^2.81.0"
},
"engines": {
"node": "7.0.0",
"npm": "3.10.8"
}
}

Resources